java褪色
来源:爱站网时间:2021-09-16编辑:网友分享
我最近完成了路径查找可视化器的工作。我想知道是否可以使用图形包将颜色从白色开始,然后逐渐淡入它们各自的颜色...
问题描述
我最近完成了路径查找可视化器的工作。我想知道是否可以使用图形包将颜色从白色开始,然后逐渐淡入青色或黑色。现在,我拥有一种颜色可以立即出现的地方,并认为如果颜色能够彼此淡出,它看起来会更好。这是我到目前为止的代码和输出图片
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int x = 0; x
思路一:
这里是一种方法。它使用计时器来定期减少rgb配色方案中的红色分量。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class ColorFadingDemo extends JPanel implements ActionListener {
Color color = new Color(255,0,0);
final static int height = 500;
final static int width = 500;
final static String title = "default title";
JFrame frame = new JFrame(title);
public static void main(String[] args) {
SwingUtilities.invokeLater(
() -> new ColorFadingDemo().start());
}
public void start() {
Timer timer = new Timer(0, this);
timer.setDelay(20);
timer.start();
}
public void actionPerformed(ActionEvent ae) {
int rgb = color.getRGB();
rgb -= 0x10000;
color = new Color(rgb);
repaint();
}
public ColorFadingDemo() {
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.add(this);
setPreferredSize(
new Dimension(width, height));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(color);
g2d.fillRect(100,100,300,300);
g2d.dispose();
}
}
思路二:
简易:
int transparency = 0; // Transparency value;
int R = 255, G = 0, B = 0; //Enter your RGB values
Color c; // The color that you can then use in your paint method
Thread t = new Thread() {
public void run() {
while (transparency
下一篇:If语句中的逻辑错误