Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
绘制带有嵌套for循环的双色棋盘格,其中每个正方形都是自己的对象(Java)_Java_For Loop_Nested - Fatal编程技术网

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

绘制带有嵌套for循环的双色棋盘格,其中每个正方形都是自己的对象(Java),java,for-loop,nested,Java,For Loop,Nested,我正在尝试使用嵌套for循环在java中绘制棋盘格图案,但在使用两种不同颜色时遇到问题。我知道以前有人问过这个问题,但没有人问过板上的两种不同颜色,而不仅仅是背景色。我计划使用单独的方块作为阵列来保持棋盘格的位置,所以我确实需要制作每个单独的方块。最好是放弃嵌套for循环的冰来创建每个正方形,还是我应该坚持使用该快捷方式?如果我坚持使用它,嵌套循环将如何格式化(每种颜色一个)?在创建棋盘格平铺时,我将为x坐标和y坐标传递一个int,例如: import java.awt.Colo

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

在创建棋盘格平铺时,我将为x坐标和y坐标传递一个
int
,例如:

        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);

        }

    }
//创建JPanel,需要将其添加到main中的JFrame对象中
导入java.awt.BorderLayout;
导入java.awt.Graphics;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类棋盘扩展JPanel{
CheckerTile[][]checkerTiles;//checkerTiles的二维数组
公共棋盘(){
超级();
这个。设置大小(800800);
checkerTiles=新CheckerTile[9][9];
//这将创建棋盘格。
对于(int i=0;i<9;i++)
{
对于(int j=0;j<9;j++)
{
checkerTiles[i][j]=新的CheckerTile(j*CheckerTile.WIDTH,i*CheckerTile.HEIGHT);
}
}
此.setVisible(true);
//立即重新绘制以显示结果。
重新油漆();
}
//我们需要覆盖这个来在黑板上画瓷砖。
@凌驾
公共组件(图形g)
{
for(int i=0;i
在创建棋盘格平铺时,我会为x坐标和y坐标传入一个
int
,例如:

        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);

        }

    }
//创建JPanel,需要将其添加到main中的JFrame对象中
导入java.awt.BorderLayout;
导入java.awt.Graphics;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类棋盘扩展JPanel{
CheckerTile[][]checkerTiles;//checkerTiles的二维数组
公共棋盘(){
超级();
这个。设置大小(800800);
checkerTiles=新CheckerTile[9][9];
//这将创建棋盘格。
对于(int i=0;i<9;i++)
{
对于(int j=0;j<9;j++)
{
checkerTiles[i][j]=新的CheckerTile(j*CheckerTile.WIDTH,i*CheckerTile.HEIGHT);
}
}
此.setVisible(true);
//立即重新绘制以显示结果。
重新油漆();
}
//我们需要覆盖这个来在黑板上画瓷砖。
@凌驾
公共组件(图形g)
{
for(int i=0;i