如何画出柔和的渐变颜色?像windows的任务栏那样,颜色渐变,而且看上去很舒服,并且有突出的感觉,这是怎么办到的?

解决方案 »

  1.   

    一个例子,关键在代表渐变画刷的类 GradientPaint 的使用:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.LayoutManager;
    import java.awt.Paint;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.plaf.PanelUI;
    @SuppressWarnings("serial")
    public class ColorfulPanel extends JPanel {    private Color color1 = new Color(116, 149, 226);
        private Color color2 = new Color(199, 212, 247);    private void init() {
            setUI(new PanelUI() {
                public void update(Graphics g, JComponent c) {
                    if (c.isOpaque()) {
                        Graphics2D g2 = (Graphics2D) g;
                        Paint old = g2.getPaint();
                        int w = c.getWidth();
                        int h = c.getHeight();
                        g2.setPaint(new GradientPaint(0, 0, color1, w, h , color2));  //这里是关键
                        g2.fillRect(0, 0, w, h);
                        g2.setPaint(old);
                    }
                    paint(g, c);
                }
            });
        }    public ColorfulPanel() {
            super();
            init();
        }    public ColorfulPanel(Color lt, Color rb) {
            super();
            if (lt != null) this.color1 = lt;
            if (rb != null) this.color2 = rb;
            init();
        }    public ColorfulPanel(boolean isDoubleBuffered) {
            super(isDoubleBuffered);
            init();
        }    public ColorfulPanel(LayoutManager layout) {
            super(layout);
            init();
        }    public ColorfulPanel(LayoutManager layout, boolean isDoubleBuffered) {
            super(layout, isDoubleBuffered);
            init();
        }    public Color getColor1() {
            return this.color1;
        }    public void setColor1(Color c) {
            if (c != null) this.color1 = c;
        }    public Color getColor2() {
            return this.color2;
        }    public void setColor2(Color c) {
            if (c != null) this.color2 = c;
        }    public static void main(String[] args) {
            JFrame frm = new JFrame("Test");
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frm.getContentPane().add(new ColorfulPanel(), BorderLayout.CENTER);
            frm.setBounds(60, 60, 260, 260);
            frm.setVisible(true);
        }}
      

  2.   

    new GradientPaint(Point2D pt1,Color color1,Point2D pt2,Color color2,boolean cyclic);
    pt1 - 用户空间中第一个指定的 Point
    color1 - 第一个指定 Point 处的 Color
    pt2 - 用户空间中第二个指定的 Point
    color2 - 第二个指定 Point 处的 Color
    cyclic - 如果渐变模式在两种颜色之间重复循环,则为 true;否则为 false。如果要做更为复杂的渐变效果需要1.6以上版本操作RadialGradientPaint类;
      

  3.   

    自己看API多练习吧.
    别老去依赖别人,多练习掌握知识没有捷径.练习才是王道!
      

  4.   

    gradient我看了,但我按照自己的想法实现了一遍,差不多效果,但不够理想,我想弄成像突出按钮那种效果
      

  5.   

    如果想使用“按钮”那种等高级效果的话.
    建议使用RadialGradientPaint类.
    但前提是JDK1.6+
      

  6.   

    一般JDK提供的Gradient性能都不怎么好,建议采用JIDE中开源的那个,也可以自己把JDK的Gradient优化一下。