简体   繁体   中英

Color fading in java

I have finished working on a path finding visualizer recently. I was wondering if it was possible to use the graphics package for the colors to start off as white and then fade to their respective color like cyan or black. Right now I have it where the color just appears instantly and would think it would look nicer if the colors were able to fade from one another. Here is the code I have so far and a picture of the output

Path finding Visualizer

public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int x = 0; x < cells; x++) { //coloring each node
            for (int y = 0; y < cells; y++) {
                switch (map[x][y].getType()) {
                    case 0: //start node
                        g.setColor(Color.GREEN);
                        break;
                    case 1: //end node
                        g.setColor(Color.RED);
                        break;
                    case 2: //wall node
                        g.setColor(Color.BLACK);
                        break;
                    case 3: //empty node
                        g.setColor(Color.WHITE);
                        break;
                    case 4: //visited nodes
                        g.setColor(Color.CYAN);
                        break;
                    case 5: //path
                        g.setColor(Color.YELLOW);
                        break;
                }
                g.fillRect(x * CSIZE, y * CSIZE, CSIZE, CSIZE);
                g.setColor(Color.BLACK); //grid color
                g.drawRect(x * CSIZE, y * CSIZE, CSIZE, CSIZE);
            }
        }
    }

Here is one way. It uses a timer to periodically decrement the red component in the rgb color scheme.

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();

    }
}

Easy:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM