读书人

java点染水晶按钮

发布时间: 2012-09-17 12:06:51 作者: rapoo

java渲染水晶按钮

java点染水晶按钮

?

import java.awt.Color;/** * 只要改变getDelColors()中的算法就会出现不同渐变效果!!! 最简单的就是循序等额递减!! * 然后到处使用该Panel替代默认的JPanel,然后将上面的其他控件都设置成透明(setOpaque(false)),则整个系统就会显得很漂亮了! * 其他Swing控件都可以类似重构!!! * 但如果把其他控件都设成透明的,然后最后都加载在该面板上面,则其他控件都无需重构就会出现效果!!比如: * JTree tree= new JTree();  // * tree.setOpaque(false);  //透明! * JScrollPanel scroll = new JScrollPanel(tree);  // * scroll.setOpaque(false);  //透明 * scroll.getViewPort().setOpaque(false); * TestBGPanel panel = new TestBGPanel(new BorderLayout());  //创建渐变效果面板 * panel.add(scroll);  //将滚动框加载进来,则这个树的背景就是渐变效果了,则界面就丰富了!!! * @author xch * */public class TestBGPanel extends JPanel {@Override/** * 关键是重构该方法!! * 也可以重构JPanel的该方法 */public void paint(Graphics g) {Graphics2D g2 = (Graphics2D) g; //int li_beginx = (int) g2.getClipBounds().getX(); //int li_beginy = (int) g2.getClipBounds().getY(); //int li_width = this.getWidth();int li_height = this.getHeight(); //g2.setColor(Color.BLACK); //g2.drawRect(li_beginx, li_beginy, li_width - 1, li_height - 1); //画边框Color bgColor = this.getBackground(); //int[] li_fromcolor = new int[] { bgColor.getRed(), bgColor.getGreen(), bgColor.getBlue() }; //传入的背景颜色的RGB的值!int[][] li_delcolors = getDelColors(li_fromcolor, li_height - 2); //关键计算,根据高度,计算出每帧高度的渐变颜色!!!for (int i = 0; i < li_delcolors.length; i++) {  //循环画出每一帧!!int li_r = li_fromcolor[0] + li_delcolors[i][0]; //Rint li_g = li_fromcolor[1] + li_delcolors[i][1]; //Gint li_b = li_fromcolor[2] + li_delcolors[i][2]; //Bif (li_r > 255) {  //如果溢出255,则当255,否则报错!li_r = 255;}if (li_g > 255) {li_g = 255;}if (li_b > 255) {li_b = 255;}g2.setColor(new Color(li_r, li_g, li_b)); //设置颜色!!g2.fillRect(0 + 1, i + 1, li_width - 2, 1); //}g2.setColor(Color.WHITE); //g2.setFont(new Font("宋体", Font.PLAIN, 12)); //g2.drawString("编辑", 30, 17); //}//颜色递减!!! 即要有一个算法,呈某种递减速度就会出现不同效果!!!//出现光感效果的原理是上半部private int[][] getDelColors(int[] _fromColor, int _height) {int[][] delColor = new int[_height][3]; //int li_half = _height / 2; //一半!if (li_half == 0) {li_half = 1;}int li_d1 = 100 / li_half; //如果是10,则递减10,如果是100,则递减1if (li_d1 == 0) {li_d1 = 1;}int li_prefix = 57; //有个前辍,可以随便设个值,就看你想渐变得白的还是黑的!for (int i = li_half - 1; i >= 0; i--) { //delColor[i][0] = li_prefix + (li_half - 1 - i) * li_d1;delColor[i][1] = li_prefix + (li_half - 1 - i) * li_d1;delColor[i][2] = li_prefix + (li_half - 1 - i) * li_d1;}int li_d2 = (int) ((100 / li_half) * 0.7); //关键这里有个系数变化,才会出现光感,水波等效果if (li_d2 == 0) {li_d2 = 1;}for (int i = li_half; i < _height; i++) {delColor[i][0] = (i - li_half) * li_d2; //delColor[i][1] = (i - li_half) * li_d2; //delColor[i][2] = (i - li_half) * li_d2; //}return delColor;}//主方法public static void main(String[] _args) {JFrame frame = new JFrame("Java渲染水晶按钮"); //frame.setSize(500, 300); //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //frame.getContentPane().setBackground(Color.WHITE); //frame.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); ////一批背景颜色!!Color[] colors = new Color[] { new Color(3, 26, 61), new Color(76, 93, 142), new Color(0, 35, 250), new Color(144, 29, 67), new Color(158, 84, 167), new Color(75, 93, 75), //new Color(126, 125, 76), new Color(75, 75, 49), new Color(55, 55, 55), new Color(101, 40, 34), new Color(75, 48, 132), new Color(35, 65, 105), //};for (int i = 0; i < colors.length; i++) { //根据背景颜色创建一系列面板!!!TestBGPanel panel = new TestBGPanel(); //panel.setPreferredSize(new Dimension(150, 30)); //panel.setBackground(colors[i]); //frame.getContentPane().add(panel); //}frame.setVisible(true); //}}
?

读书人网 >编程

热点推荐