如何画出柔和的渐变颜色?像windows的任务栏那样,颜色渐变,而且看上去很舒服,并且有突出的感觉,这是怎么办到的?
解决方案 »
- thinking in java中一个不明白的地方
- 谁能帮我解决JAVA这个问题 编译时没错 但是运行时就出错了
- Servlet的doPost方法中得不到Url中的参数吗?
- 求助!!!!!!!
- 得到了des的密钥就等于可以由加密后的内容得到加密前的内容,是吗?为什么des加密方法还被广泛应用呢?谢谢!
- SimpleDateFormate使用
- 关于网页显示 500 servlet exception ,想要请教各位打算关于设置classpath的问题!
- java如何计算两个时间的差值,如何在两个时间之间相加,减?得到的值要求精确到秒!
- 如何手动清除变量?
- 递归父类所有节点
- java事件处理如何将单击鼠标获得值添加到文本框
- 服务器端获取多个客户端IP
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);
}}
pt1 - 用户空间中第一个指定的 Point
color1 - 第一个指定 Point 处的 Color
pt2 - 用户空间中第二个指定的 Point
color2 - 第二个指定 Point 处的 Color
cyclic - 如果渐变模式在两种颜色之间重复循环,则为 true;否则为 false。如果要做更为复杂的渐变效果需要1.6以上版本操作RadialGradientPaint类;
别老去依赖别人,多练习掌握知识没有捷径.练习才是王道!
建议使用RadialGradientPaint类.
但前提是JDK1.6+