在java中为渐变面板添加颜色?

时间:2010-02-22 07:45:37

标签: java gradient

如何在java中为渐变面板添加新颜色?

1 个答案:

答案 0 :(得分:3)

从JPanel扩展你的面板并像这样覆盖它的paintComponent。

    @Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// Creates a two-stops gradient
GradientPaint p;
p = new GradientPaint(0, 0, new Color(0xFFFFFF),
0, getHeight(), new Color(0xC8D2DE));
// Saves the state
Paint oldPaint = g2.getPaint();
// Paints the background
g2.setPaint(p);
g2.fillRect(0, 0, getWidth(), getHeight());
// Restores the state
g2.setPaint(oldPaint);
// Paints borders, text...
super.paintComponent(g);
}
}

你看到你可以改变现有颜色的颜色对象......

我建议你阅读

  

肮脏的富客户端

从某个地方获取这本书。它有更多有用的信息,您可以使用学习。