java awt 窗口渐变动画_使用Java awt的4种颜色渐变

像这样吗

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class ThreeWayGradient {

public static void main(String[] args) {

final BufferedImage image = new BufferedImage(

200, 200, BufferedImage.TYPE_INT_RGB);

Runnable r = new Runnable() {

@Override

public void run() {

Graphics2D g = image.createGraphics();

GradientPaint primary = new GradientPaint(

0f, 0f, Color.WHITE, 200f, 0f, Color.ORANGE);

GradientPaint shade = new GradientPaint(

0f, 0f, new Color(0, 0, 0, 0),

0f, 200f, new Color(0, 0, 0, 255));

g.setPaint(primary);

g.fillRect(0, 0, 200, 200);

g.setPaint(shade);

g.fillRect(0, 0, 200, 200);

JLabel l = new JLabel(new ImageIcon(image));

JOptionPane.showMessageDialog(null, l);

File f = new File(System.getProperty("user.home"),

"ThreeWayGradient.png");

try {

ImageIO.write(image, "png", f);

} catch (IOException ex) {

ex.printStackTrace();

}

}

};

SwingUtilities.invokeLater(r);

}

}

使其成为工厂方法

..因为它更漂亮.

import java.awt.*;

import java.awt.image.BufferedImage;

import javax.swing.*;

public class ThreeWayGradient {

public static BufferedImage getThreeWayGradient(

int size,

Color primaryLeft,

Color primaryRight,

Color shadeColor) {

BufferedImage image = new BufferedImage(

size, size, BufferedImage.TYPE_INT_RGB);

Graphics2D g = image.createGraphics();

GradientPaint primary = new GradientPaint(

0f, 0f, primaryLeft, size, 0f, primaryRight);

int rC = shadeColor.getRed();

int gC = shadeColor.getGreen();

int bC = shadeColor.getBlue();

GradientPaint shade = new GradientPaint(

0f, 0f, new Color(rC, gC, bC, 0),

0f, size, shadeColor);

g.setPaint(primary);

g.fillRect(0, 0, size, size);

g.setPaint(shade);

g.fillRect(0, 0, size, size);

g.dispose();

return image;

}

/**

* Presumed to have a layout that shows multiple components.

*/

public static void addGradient(

JPanel p, int s, Color pL, Color pR, Color sh) {

JLabel l = new JLabel(new ImageIcon(getThreeWayGradient(s, pL, pR, sh)));

p.add(l);

}

public static void main(String[] args) {

Runnable r = new Runnable() {

@Override

public void run() {

JPanel gui = new JPanel(new GridLayout(2,4,1,1));

addGradient(gui,100,Color.YELLOW,Color.RED,Color.GREEN);

addGradient(gui,100,Color.GREEN,Color.YELLOW,Color.RED);

addGradient(gui,100,Color.RED,Color.GREEN,Color.YELLOW);

addGradient(gui,100,Color.BLUE,Color.MAGENTA,Color.PINK);

addGradient(gui,100,Color.WHITE,Color.RED,Color.BLACK);

addGradient(gui,100,Color.RED,Color.GREEN,Color.BLACK);

addGradient(gui,100,Color.BLUE,Color.PINK,Color.BLACK);

addGradient(gui,100,Color.BLUE,Color.CYAN,Color.BLACK);

JOptionPane.showMessageDialog(null, gui);

}

};

SwingUtilities.invokeLater(r);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值