随机颜色网格

时间:2019-03-19 17:35:54

标签: java random

最后,我应该得到的是一个带有正方形的网格,如果我调整窗口大小,该网格具有稳定的颜色。到目前为止,我得到的网格具有随机的颜色,但是当所有内容重新绘制时,颜色也是如此。我在想,也许一个存储颜色的数组可以工作,但是我真的不知道如何用到目前为止的方法实现它。

public class GridRandomColors extends JFrame {
    private static class Board extends JPanel {
        private Rectangle MAIN_RECT;
        private double BRICK_WIDTH, BRICK_HEIGHT;
        private int COLS = 8;
        private int ROWS = 8;


        public Board() {
            setBackground(Color.gray);
        }

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

        private void drawRectangle(Graphics g) {

            if (getHeight() > getWidth()) {

                MAIN_RECT = new Rectangle(0, 0, getWidth(), getWidth());
                g.fillRect(0, (getHeight()-getWidth())/2, getWidth(), getWidth());

                x = 0;
                y = (getHeight()-getWidth())/2;
            } else {
                x = (getWidth()-getHeight())/2;
                y = 0;
                MAIN_RECT = new Rectangle(0, 0, getHeight(), getHeight());
                g.fillRect((getWidth()-getHeight())/2, 0, getHeight(), getHeight());

            }
            BRICK_WIDTH = (float) MAIN_RECT.getWidth() / COLS;
            BRICK_HEIGHT = (float) MAIN_RECT.getHeight() / ROWS  ;
        }

        double spacing = 0.2;
        private double x;
        private double y;
        private Color color;

        private void drawBricks(Graphics g) {
            Graphics2D brick = (Graphics2D) g.create();

            for (int j = 0; j < ROWS; j++) {
                for (int a = 0; a < COLS; a++) {
                    Random rand = new Random();
                    color = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
                    Color oldColor = g.getColor();
                    brick.setColor(color);

                    Rectangle2D.Double rect = new Rectangle2D.Double(x, y, BRICK_WIDTH - spacing*(COLS-1), BRICK_HEIGHT- spacing*(ROWS-1));

                    brick.fill(rect);
                    brick.setColor(oldColor);
                        x += BRICK_HEIGHT+spacing;
                    }
                if (getHeight() > getWidth()) {
                    x = 0;
                }
                else {
                    x = (getWidth() - getHeight()) / 2;
                }

                y += BRICK_HEIGHT+spacing;
            }
        }
    }

    public GridRandomColors() {

        setDefaultCloseOperation(EXIT_ON_CLOSE);    //mai bine cu exit on close
        setSize(800, 820);
        add(new Board());
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new GridRandomColors().setVisible(true);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用简单的二维数组存储颜色并在构造函数中生成颜色:

private static class Board extends JPanel {
    // ...
    private int COLS = 8;
    private int ROWS = 8;
    private Color[][] colors;

    public Board() {
        // ...
        Random rand = new Random();
        colors = new Color[ROWS][COLS];
        for (int a = 0; a < ROWS; a++) {
            for (int b = 0; b < COLS; b++) {
                colors[a][b] = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
            }
        }
    }

    private void drawBricks(Graphics g) {
        // ...
        for (int j = 0; j < ROWS; j++) {
            for (int a = 0; a < COLS; a++) {
                Color oldColor = g.getColor();
                brick.setColor(colors[j][a]);
                // ...
            }
            // ...
        }
    }
}