开发桌面程序,好看的GUI肯定是比较重要的啦。但是那么多API函数要使用,阿拉丁说:“如果不好好总结一下的话,是不会有什么进步的”。
正好参考了网络上的一些资料,自定义了一个简单的按钮。下面就好好总结一下吧。
其实我认为吧,重绘是通往自定义好看界面的必由之路,在java里也就是重载paint,paintComponent等函数。
可以说java的GUI编程是比较便捷的,因为java为我们提供了一个超级好的类——Graphics2D。
嗯,言归正传,我们这里要简单实现以下JButton的重绘。
代码如下:
MyCrystalBtn.java
package bear.java.gui.btn;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
public class MyCrystalBtn extends JButton implements MouseListener{
private static final long serialVersionUID = 1L;
private int textHeight = 17;
private int colorPrefix = 60;
private String text = "开始";
public MyCrystalBtn()
{
this.addMouseListener(this);
}
public MyCrystalBtn(String title)
{
this();
this.text = title;
}
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int startX = (int)g2d.getClipBounds().getX();
int startY = (int)g2d.getClipBounds().getY();
int width = this.getWidth();
int height = this.getHeight();
g2d.setColor(Color.BLACK);
g2d.drawRect(startX, startY, width-1, height-1);
Color bgColor = this.getBackground();
int[] baseColor = new int[] {bgColor.getRed(), bgColor.getGreen(), bgColor.getBlue()};
int[][] offsetColor = getOffsetColor(baseColor, height-2);
for(int i = 0 ; i < offsetColor.length ; i++)
{
int cR = baseColor[0] + offsetColor[i][0];
int cG = baseColor[1] + offsetColor[i][1];
int cB = baseColor[2] + offsetColor[i][2];
cR = ( cR > 255 ) ? 255 : cR;
cG = ( cG > 255 ) ? 255 : cG;
cB = ( cB > 255 ) ? 255 : cB;
g2d.setColor(new Color(cR, cG, cB));
g2d.fillRect(1, i+1, width-2, 1);
}
g2d.setColor(Color.WHITE);
g2d.setFont(new Font("宋体", Font.PLAIN, 12));
g2d.drawString(text, (width-text.length()*12)/2, textHeight);
//super.paint(g);
}
private int[][] getOffsetColor(int [] baseColor, int h)
{
int[][] offsetColor = new int[h][3];
int half = h/2;
if(0 == half)
half = 1;
int d1 = 100 / half;
if(0 == d1)
d1 = 1;
for(int i = half-1 ; i >= 0 ; i --)
{
offsetColor[i][0] = colorPrefix + (half - 1 - i) * d1;
offsetColor[i][1] = colorPrefix + (half - 1 - i) * d1;
offsetColor[i][2] = colorPrefix + (half - 1 - i) * d1;
}
int d2 = (int)((100 / half) * 0.6);
if(0 == d2)
d2 = 1;
for(int i = half ; i < h ; i ++)
{
offsetColor[i][0] = colorPrefix + (i - half) * d2;
offsetColor[i][1] = colorPrefix + (i - half) * d2;
offsetColor[i][2] = colorPrefix + (i - half) * d2;
}
return offsetColor;
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
this.colorPrefix = 0;
this.textHeight = 17;
this.repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
this.colorPrefix = 25;
this.textHeight = 16;
this.repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
this.colorPrefix = 25;
this.textHeight = 16;
this.repaint();
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
this.colorPrefix = 60;
this.textHeight = 17;
this.repaint();
}
}
MyCrystalBtnTest.java //测试类
package bear.java.gui.btn;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class MyCrystalBtnTest {
public static void main(String[] args)
{
JFrame frame = new JFrame("Crystal Button Show");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.white);
frame.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
MyCrystalBtn mcb = new MyCrystalBtn("查看什么");
mcb.setPreferredSize(new Dimension(150, 30));
mcb.setBackground(new Color(39, 75, 249));
frame.getContentPane().add(mcb);
frame.setVisible(true);
}
}
参考资料:
java渲染水晶按钮 http://chenyu-hz.iteye.com/blog/1493188