自定义仿Win7的水晶按钮
效果图
完整工程下载http://download.csdn.net/detail/jasper_success/4488514
关键代码
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
AlphaComposite composite = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, alpha);
g2.setComposite(composite);
// 绘制边框
if (this.isMouseEntered) {
Util.drawButtonBackground(g2, this, new Color(253, 236, 219),
new Color(253, 223, 187), new Color(255, 206, 105),
new Color(255, 255, 222));
}
// 按钮被按下的效果
if (clicked) {
Util.drawButtonBackground(g2, this, new Color(255, 199, 99),
new Color(253, 236, 219), new Color(253, 236, 219),
new Color(255, 199, 99));
}
composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f);
g2.setComposite(composite);
if (frameType == FRAME_AROUND) {
g.setColor(Config.COLOR_TOOLBAR_FRAME);
g.drawRoundRect(1, 1, getWidth() - 4, getHeight() - 2, 3, 3);
} else if (frameType == FRAME_RIGHT) {
g.setColor(Config.COLOR_TOOLBAR_FRAME);
g.drawLine(getWidth() - 1, 1, getWidth() - 1, getHeight() - 2);
}
// 消除锯齿
Util.setFractionalmetricsOn(g2);
// 文字风格
g.setColor(Config.COLOR_UI_TEXT);
g.setFont(Config.FONT_UI_TEXT);
// 绘制图标
if (getImage() != null) {
g.drawImage(getImage(),
(getWidth() - getImage().getWidth(this)) / 2, 5 + mbStatus,
this);
g.drawString(getName(), (getWidth() - getName().length()
* Config.FONT_UI_TEXT.getSize()) / 2,
getImage().getHeight(this) + mbStatus);
} else {
g.drawString(getName(), (getWidth() - getName().length()
* Config.FONT_UI_TEXT.getSize()) / 2, 10 + mbStatus);
}
}