绘制带有嵌套for循环的双色棋盘,其中每个正方形都是它们自己的对象(Java)

时间:2016-09-07 15:20:48

标签: java for-loop nested

我试图使用嵌套的for循环在java中绘制棋盘图案,但我在使用两种不同的颜色时遇到了麻烦。我知道之前已经问过这个问题,但是它没有被问到板上的两种不同颜色,而不仅仅是使用背景颜色。我计划使用单个正方形作为阵列来保持棋盘位置,所以我确实需要制作每个单独的正方形。放下嵌套for循环的冰来创建每个方块会更好吗,还是应该坚持使用那条捷径?如果我坚持下去,嵌套循环将如何格式化(每种颜色一个)?

1 个答案:

答案 0 :(得分:1)

创建检查器图块时,我会传入int表示x坐标,y坐标表示如下:

        import java.awt.Color;
        import java.awt.Graphics;

        public class CheckerTile {

            public static final int WIDTH = 100; //width of each tile
            public static final int HEIGHT = 100; //height of each tile, most likely same as width so its a square

            public static int currentId = 0; //variable to reference unique id for each tile

            private int id; //current id of tile
            private int x; //x coordinate
            private int y; //y coordinate
            private int width; //width of tile
            private int height; //height of tile

            //Default constructor to take x and y coordinate
            public CheckerTile( int x, int y ) {
                this.id = currentId++;
                this.x = x;
                this.y = y;
                width = WIDTH;
                height = HEIGHT;
            }

            public int getId()
            {
                return id;
            }

            //draws the tile on the panel.
            public void draw(Graphics g)
            {
                //if the checkerTile's id is divisible by 2, draw it red, otherwise draw it black.
                g.setColor( id % 2 == 0 ? Color.RED : Color.black);
                g.fillRect(x, y, width, height);
            }

        }

通过这种方式,我们可以在电路板上绘制瓷砖。现在,在创建每个对象时,我们增加一个currentId变量,以便我们稍后可以使用模数运算符为每个对象着色。

我假设您正在使用Swing,因此我决定添加draw(Graphics g)方法,因此在重新绘制java时,它将使用该Graphics对象。如果你使用的是不同的库,那么你将不得不研究如何在板上绘制它。

现在在JPanel中,它看起来像这样:

    //Creates the JPanel, which needs to be added to JFrame object in main
    import java.awt.BorderLayout;
    import java.awt.Graphics;

    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class CheckerBoard extends JPanel {

        CheckerTile[][] checkerTiles; //2-dimension array of checkerTiles

        public CheckerBoard() {
            super();
            this.setSize(800,800);

            checkerTiles = new CheckerTile[9][9];

            //This creates the checkerTiles.
            for(int i = 0; i < 9; i++)
            {
                for( int j = 0; j < 9; j++)
                {
                    checkerTiles[i][j] = new CheckerTile( j * CheckerTile.WIDTH, i * CheckerTile.HEIGHT );
                }
            }


            this.setVisible(true);

            //Repaint right away to show results.
            repaint();

        }

        //We need to override this to paint the tiles on the board.
        @Override
        public void paintComponent(Graphics g)
        {
            for(int i = 0; i < checkerTiles.length; i++)
            {
                for(int j = 0; j < checkerTiles[i].length; j++)
                {
                    //call the draw method on each tile.
                    checkerTiles[i][j].draw(g);
                }
            }
        }

        //A demo of adding the panel to a frame and showing the tiles.
        public static void main(String[] args)
        {
            //Create the JFrame and add the CheckerBoard we made to it.
            JFrame frame = new JFrame();
            frame.setSize(800,800);
            frame.setLayout(new BorderLayout());
            frame.add(new CheckerBoard(), BorderLayout.CENTER);
            frame.setVisible(true);

        }

    }