java - 如何使我的代码中每个方 block 的颜色都不同?

标签 java graphics colors

我的代码可以正常运行,并反复更改代码中方 block 的颜色,但更改时所有方 block 的颜色都是相同的。

主程序:

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.util.Random;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Canvas;
import javax.swing.JPanel;

public class RandomColoredBoxes extends JPanel
{
private Timer timer;
private final static int SLEEP = 110;

public RandomColoredBoxes()
{       
    setBackground(Color.BLACK);
    setVisible(true);

    ActionListener paintCaller = new ActionListener(){
        public void actionPerformed(ActionEvent event)
        {
            repaint();  
        }
    };
    timer = new Timer(SLEEP, paintCaller);
    timer.start();
}   

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

    window.setColor(Color.RED);
    window.setFont(new Font("TAHOMA",Font.BOLD,12));
    window.drawString("Graphics Lab Lab11k ", 20, 40);
    window.drawString("Drawing boxes with nested loops ", 20, 80);

    drawBoxes(window);
}

public void drawBoxes(Graphics window)
{
    int colorValue1 = (int)(Math.random() * 256);
    int colorValue2 = (int)(Math.random() * 256);
    int colorValue3 = (int)(Math.random() * 256);

    Color random = new Color(colorValue1, colorValue2, colorValue3);
    window.setColor(random);
    //for loop to to across the x - getWidth() might be useful
    for(int x = 30; x <= getWidth()- 30; x+=15){
        for(int y = 100; y <= getHeight() - 30; y+= 15){
            window.fillRect(x, y, 8, 8);
        }
    }
        //for loop to go down the y - getHeight() might be useful

            //draw random colored boxes
}
}

还有我的图形运行器类:

import javax.swing.JFrame;

public class GraphicsRunnerRandom extends JFrame
{
private static final int WIDTH = 800;
private static final int HEIGHT = 600;

public GraphicsRunnerRandom()
{
    super("Graphics Runner");
    setSize(WIDTH,HEIGHT);

    getContentPane().add(new RandomColoredBoxes());                 

    //getContentPane().add(new RandomColoredBoxes());

    setVisible(true);
}

public static void main( String args[] )
{
    GraphicsRunnerRandom run = new GraphicsRunnerRandom();
}
}

除了方 block 全部改变之外,其他一切都按其应有的方式运行

最佳答案

而不是这个:

int colorValue1 = (int)(Math.random() * 256);
int colorValue2 = (int)(Math.random() * 256);
int colorValue3 = (int)(Math.random() * 256);

Color random = new Color(colorValue1, colorValue2, colorValue3);
window.setColor(random);
//for loop to to across the x - getWidth() might be useful
for(int x = 30; x <= getWidth()- 30; x+=15){
    for(int y = 100; y <= getHeight() - 30; y+= 15){
        window.fillRect(x, y, 8, 8);
    }
}

试试这个:

//for loop to to across the x - getWidth() might be useful
for(int x = 30; x <= getWidth()- 30; x+=15){
    for(int y = 100; y <= getHeight() - 30; y+= 15){
        int colorValue1 = (int)(Math.random() * 256);
        int colorValue2 = (int)(Math.random() * 256);
        int colorValue3 = (int)(Math.random() * 256);

        Color random = new Color(colorValue1, colorValue2, colorValue3);
        window.setColor(random);

        window.fillRect(x, y, 8, 8);
    }
}

基本区别是从循环内部创建新颜色(并调用 window.setColor) - 这样每次循环开始时,在在框中绘制新的随机颜色之前将被使用。

关于java - 如何使我的代码中每个方 block 的颜色都不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13597454/

相关文章:

java - 什么是 NullPointerException,我该如何解决?

java - 为什么 swing 会绘制两次简单的组件?

python - Cython 使用什么图形库来绘制图形?

graphics - 如何直接访问GPU?

JavaScript 滴管(告诉鼠标光标下像素的颜色)

java - 第二个对话框不会运行 - Java

java - 验证十进制数

colors - sRGB->CIEXYZ->丢弃亮度的结果是否可以转换回 sRGB?

java - 如何在 Android 中将颜色整数转换为十六进制字符串?

java - 随机错误和 boolean 错误