如何在java中用randm颜色填充2D数组?

时间:2017-07-18 21:01:10

标签: java arrays

我一直试图为砖块破坏者游戏生成一个随机颜色的网格。但是根据这段代码,颜色会不断变化。我希望他们随机设置并保持这种状态。

for(int i = 0; i < map.length; i++) {
        for(int j = 0; j < map [0].length; j++) {
            if(map[i][j] > 0) { //make brick if greater than 0, else don't 

                int color = (int) (Math.random() * 256);
                g.setColor(new Color(color, color, color));
                g.fillRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight);


                g.setStroke(new BasicStroke(3));
                g.setColor(Color.black);
                g.drawRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight);
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

每次需要调整组件大小或移动组件时,都会调用repaint()方法来更新其状态。因此,如果您在paintComponent中生成颜色,如下例所示:

public class MyComponent extends JComponent {
    @Override
    protected void paintComponent(Graphics g) {
        // generate colors and draw grid
    }
}

然后,调整大小事件或导致repaint调用的其他事件的颜色会发生变化,因为repaint会调用paintComponent方法。如果要显示相同的颜色,只需从此方法中移出生成颜色代码:

public class MyComponent extends JComponent {

    private final Color[][] gridColors = randomGridColors(5, 5);

    private Color[][] randomGridColors(int rows, int columns) {
        Color[][] gridColors = new Color[rows][columns];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                gridColors [i][j] = randomColor();
            } 
        }
    }

    private Color randomColor() {
        int rgbValue = (int) (Math.random() * 256);
        return new Color(rgbValue, rgbValue, rgbValue);
    }

    @Override
    protected void paintComponent(Graphics g) {
        // draw grid 
    }
}

答案 1 :(得分:0)

您每次绘制砖块时都会创建随机颜色,这可能是每秒多次。 创建一些砖类并仅为每个砖生成一次颜色。 像这样:

public class TryThis {

private static final Logger LOG = Logger.getLogger(TryThis.class.getName());

public static void main(String[] args) {
    SwingComponent panel = new SwingComponent();
    JFrame frame = new JFrame("try me");
    frame.setSize(800, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(panel);
    frame.setVisible(true);
}

static class SwingComponent extends JPanel {

    int W = 10;
    int H = 10;
    int recwidth = 20;
    int recheight = 10;

    Brick[][] bricks = new Brick[H][W];

    public SwingComponent() {
        super();
        for (int y = 0; y < H; y++) {
            for (int x = 0; x < W; x++) {
                bricks[y][x] = new Brick(createRandomColor(), new Rectangle(x * recwidth, y * recheight, recwidth,
                    recheight));
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        for (int y = 0; y < H; y++) {
            for (int x = 0; x < W; x++) {
                bricks[y][x].draw(g);
            }
        }
    }

    Color createRandomColor() {
        return new Color((int) (Math.random() * 256), (int) (Math.random() * 256), (int) (Math.random() * 256));
    }
}

static class Brick {

    Color col;
    Rectangle rec;

    public Brick(Color col, Rectangle rec) {
        this.col = col;
        this.rec = rec;
    }

    public void draw(Graphics g) {
        g.setColor(col);
        g.fillRect(rec.x, rec.y, rec.width, rec.height);
    }
}
}