java - 使用嵌套 for 循环绘制两个颜色的棋盘,其中每个方 block 都是它们自己的对象 (Java)

标签 java for-loop nested

我正在尝试使用嵌套 for 循环在 java 中绘制棋盘图案,但我在使用两种不同的颜色时遇到困难。我知道这个问题以前曾被问过,但还没有被问过板上有两种不同的颜色,而不仅仅是使用背景颜色。我计划使用各个方 block 作为数组来保存检查器位置,因此我确实需要制作每个单独的方 block 。放弃嵌套 for 循环来创建每个方 block 会更好,还是应该坚持使用该快捷方式?如果我坚持使用它,嵌套循环将如何格式化(每种颜色一个)?

最佳答案

创建棋盘格图 block 时,我会传入一个 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);
            }

        }

这样我们就可以在棋盘上绘制图 block 了。现在,在创建每个对象时,我们会增加一个 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);

        }

    }

关于java - 使用嵌套 for 循环绘制两个颜色的棋盘,其中每个方 block 都是它们自己的对象 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39373789/

相关文章:

java - 颜色键的 LookAndFeel 独立引用

java - .exe java文件如何在没有java软件的情况下在新安装的操作系统中执行

Java For 循环 - 那个时刻的变量 - JButton 的二维数组

swift - 访问 Swift 嵌套字典中的行

python - 在嵌套列表理解中只使用一个列表中的项目一次

python - 寻找嵌套列表中的最低值?

java - Spring JPA和Hibernate在调用save方法时不将实体保存到数据库

java - 对接收端(客户端)Socket进行带宽限制

python - 为什么python在for和while循环之后使用 'else'?

ios - 动画 UILabel 值增量