如何随机重绘三种不同颜色之间的线条

时间:2014-06-21 00:43:38

标签: java colors render

除了颜色外,我让这个程序的每个部分都有效。现在我重新绘制任何随机颜色,我需要它只绘制黄色,绿色或蓝色的线条。有人能引导我走向正确的方向吗?

`package ColoredLines;

import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
import java.awt.Color;

@SuppressWarnings("serial")
public class ColoredLines extends JPanel {
    private final Random rand = new Random();

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

        Random rand = new Random();

        super.paintComponent(g);
        this.setBackground(Color.BLACK);


        for(int i = 10; i < 410; i += 20){
            for(int j = 0; j < 410; j += 410){


                g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
                g.fillRect(i, j, 10, 410);
                }
            }
        }

    }
`

1 个答案:

答案 0 :(得分:0)

制作三种颜色的ArrayList: ArrayList<Color> colors = new ArrayList<Color>();

添加三种颜色:colors.add(new Color(r,g,b)

使用以下方法选择随机颜色:

Random rand = new Random(); Color randomColor = colors[rand.nextInt(2)];

注意:在rand.nextInt(2)中;随机数可以是0,因此可能的数字是三(0,1,2)。