Java 我如何使我的代码中的每个方块都是不同的颜色?

Java 我如何使我的代码中的每个方块都是不同的颜色?,java,graphics,colors,Java,Graphics,Colors,我已经让我的代码运行,并在代码中反复更改方块的颜色,但所有方块在更改时都是相同的颜色 主程序: 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

我已经让我的代码运行,并在代码中反复更改方块的颜色,但所有方块在更改时都是相同的颜色

主程序:

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
}
}
除了方块一起变化外,其他一切都按它应该的方式运行,而不是这样:

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);
    }
}
intcolorValue1=(int)(Math.random()*256);
int colorValue2=(int)(Math.random()*256);
int colorValue3=(int)(Math.random()*256);
颜色随机=新颜色(colorValue1、colorValue2、colorValue3);
设置颜色(随机);
//for循环穿过x-getWidth()可能很有用

因为(intx=30;x啊!就在钱的问题上!我想是有原因的,评论是这样列出的……只是没想到而已
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);
    }
}