【问题标题】:Color fading in javajava中的颜色渐变
【发布时间】:2020-06-09 13:13:57
【问题描述】:

我最近完成了寻路可视化工具的工作。我想知道是否可以使用图形包使颜色从白色开始,然后逐渐变为它们各自的颜色,如青色或黑色。现在我有它,颜色会立即出现,如果颜色能够相互褪色,我会认为它会更好看。这是我到目前为止的代码和输出的图片

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

【问题讨论】:

  • 如果您还没有这样做,请查看以下搜索结果:google.com/…
  • 顺便说一句,您的探路者没有找到最快的路径。你用的是什么算法?

标签: java graphics colors


【解决方案1】:

这是一种方法。它使用计时器周期性地减少 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();

    }
}

【讨论】:

    【解决方案2】:

    简单:

    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
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 2011-03-06
      相关资源
      最近更新 更多