简体   繁体   English

如何使此 JPanel 中的像素动态变化?

[英]How can I make the pixels in this JPanel dynamically change?

I have designed a program that outputs 1x1 rectangles (That I'll from now on call Pixels) with a color that is randomized between a random variable between Red, Green, and Blue.我设计了一个程序,该程序输出 1x1 矩形(从现在开始我将称之为像素),其颜色在红色、绿色和蓝色之间的随机变量之间随机化。 Now I want to take it one step further.现在我想更进一步。 First off, here's the source:首先,这是来源:

public class GUI extends JPanel {
private final static int MAX_X = 1920;
private final static int MAX_Y = 1080;

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    for (int x = 0; x < MAX_X; x++) {
        for (int y = 0; y < MAX_Y; y++) {
  
            Color randomColor = Color.BLACK; //Black because it wouldn't work uninitialized.
            
            //Slightly inefficient? 
            int number; Random rColor = new Random();
            number = rColor.nextInt(3);
            
             if(number == 0){randomColor = Color.RED;}
             else if(number ==1){randomColor = Color.GREEN;}
             else if(number == 2){randomColor = Color.BLUE;}
            
            
            g.setColor(randomColor); //Red, Blue, or Green. Depends on if number is 0, 1 or 2.
            g.drawRect(x, y, 1, 1); 
        }
           
    
    }
    
}

}

It works, and it works like how I expect.它可以工作,并且就像我期望的那样工作。 Here's the next challenge: How can I make them continuously change?这是下一个挑战:我怎样才能让它们不断变化? What I want is, if a neighboring pixel is blue than have the pixel be green and then any green to red and red to blue.我想要的是,如果相邻像素是蓝色,那么像素是绿色,然后是任何绿色到红色和红色到蓝色。 I'm not sure with how it currently is setup that it'll be possible.我不确定它目前的设置方式是否可行。

Side note: I have a feeling there's a way to minimize those couple lines of code I labeled in a comment as "Slightly Inefficient" into something cleaner like inside parameters.旁注:我感觉有一种方法可以将我在评论中标记为“稍微低效”的那几行代码最小化为更清晰的内部参数。 I've been looking up documentation for a while but I can't find a more functional use for Random.我一直在查找文档一段时间,但我找不到 Random 的更多功能用途。

Entire Code完整代码

you can use a timer to keep changing.您可以使用计时器来不断变化。 for example: modify your gui class with following code and see .例如:使用以下代码修改您的 gui 类并查看 . this timer run each half second and repaint jpanel.you can change 500 to desired milisecond.it will look like this.这个计时器每半秒运行一次并重新绘制 jpanel。你可以将 500 更改为所需的毫秒。它看起来像这样。

you don't need to create a new random number each time .您不需要每次都创建一个新的随机数。 randomnumber.nextInt is enough and you make number as instance randomnumber.nextInt 就足够了,您可以将 number 作为实例

在此处输入图片说明在此处输入图片说明

public class GUI extends JPanel {

    private final static int MAX_X = 200;
    private final static int MAX_Y = 150;
    private final Color RED = Color.RED, GREEN = Color.GREEN, BLUE = Color.BLUE;
    private final Random rColor = new Random();
    private int number;
    private Color randomColor;

    public GUI() {
        new Timer(1, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                repaint();
            }
        }).start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        for (int x = 0; x < MAX_X; x++) {
            for (int y = 0; y < MAX_Y; y++) {

                number = rColor.nextInt(3);

                if (number == 0) { randomColor = RED;
                } else if (number == 1) { randomColor = GREEN;
                } else if (number == 2) {randomColor = BLUE;
                }

                g.setColor(randomColor);
                g.drawRect(x, y, 1, 1);
            }

        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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