主要代码:
@Override
public void paintIcon(Component cmp, Graphics g, int x, int y)
{
Color lowerColor = new Color(235,255,235);
Color highColor = new Color(81,184,77);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
// 画一个渐变圆角矩形
RoundRectangle2D.Float r2d=new RoundRectangle2D.Float(0, 0, btnWidth - 1, btnHeight - 1, 20, 20);
Shape clip=g2d.getClip();
g2d.clip(r2d);
GradientPaint paint = new GradientPaint(0.0F,0.0F,lowerColor,0.0F,btnHeight,highColor,true);
g2d.setPaint(paint);
g2d.fillRect(0,0,btnWidth,btnHeight);
g2d.setClip(clip);
// 画一个黑色边框
paint = new GradientPaint(0,0,new Color(0,0,0), 0,btnHeight-1,new Color(100,100,100));
g2d.setPaint(paint);
g2d.drawRoundRect(0,0,btnWidth-1,btnHeight-1,20,20);
// 画一个白色边框
paint=new GradientPaint(0,1,new Color(0,0,0,50),0,btnHeight-3,new Color(255,255,255,100));
g2d.setPaint(paint);
g2d.drawRoundRect(1,1,btnWidth-3,btnHeight-3,18,18);
FontMetrics fm = g2d.getFontMetrics(boldFont);
int textWidth = fm.stringWidth(btnText);
int textAscent = fm.getAscent();
g2d.setColor(hoverColor);
g2d.setFont(boldFont);
g2d.drawString(btnText, (btnWidth - textWidth) / 2, (btnHeight + textAscent) / 2 - 2);
}
2021-11-20 15:16:41
24KB
java
1