java中的颜色衰退

java中的颜色衰退,java,graphics,colors,Java,Graphics,Colors,我最近已经完成了一个路径查找可视化工具的工作。我想知道是否有可能使用图形包的颜色开始为白色,然后淡入各自的颜色,如青色或黑色。现在我有它的颜色只是立即出现,并会认为它会看起来更好,如果颜色能够从彼此褪色。这是我到目前为止的代码和输出的图片 公共组件(图形g){ 超级组件(g); 对于(int x=0;x

我最近已经完成了一个路径查找可视化工具的工作。我想知道是否有可能使用图形包的颜色开始为白色,然后淡入各自的颜色,如青色或黑色。现在我有它的颜色只是立即出现,并会认为它会看起来更好,如果颜色能够从彼此褪色。这是我到目前为止的代码和输出的图片

公共组件(图形g){
超级组件(g);
对于(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;//透明度值;
int R=255,G=0,B=0//输入您的RGB值
颜色c;//然后可以在绘制方法中使用的颜色
线程t=新线程(){
公开募捐{
而(透明度<255){
int透明度=0;
c=新颜色(R、G、B、透明度);//通过设置自定义颜色,可以定义R、G、B和透明度值
透明度++;
试一试{
int fadeTime=1000;//衰减过程应该持续多长时间
线程。睡眠(255/衰减时间);
}捕获(例外e){}
}
}
};
t、 start();//线程将继续在后台运行,直到透明度达到255

如果您还没有这样做,请查看以下搜索结果:顺便说一句,您的pathfinder没有找到最快的路径。你在用什么算法?
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 < 255) {
         int transparency = 0;
         c = new Color(R,G,B,transparency); // By creeting a custom color you can define the R, G, B and transparency values
         transparency++;

         try {
         int fadeTime = 1000; // How long the fading process should go
            Thread.sleep(255/fadeTime);
         } catch (Exception e) {}

      }
   }
};

t.start(); // The thread will continue to run in the background until the transparency reaches 255