简体   繁体   中英

Drawing a two colored checker board with nested for loops where each square being their own object (Java)

I am attempting to draw a checkerboard pattern in java using nested for loops, but I am having trouble doing it with two different colors. I know this question has been asked before, but it hasn't been asked with two different colors on the board that are not just using a background color. I plan on using the individual squares as an array to hold checker positions, so I do need each individual square made. Would it be better to drop the ice of a nested for loop to create each square, or should i stick with that shortcut? And if I were to stick with it, how would the nested loop be formatted (one for each color)?

When creating checker tiles, I would pass in an int for the x coordinate, and y coordinate such as:

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

        }

That way we have a way to draw the tile on the board. Now, when creating each object, we increment a currentId variable so that we can color each one individually using the modulus operator later.

I am assuming you are using Swing so I decided to add a draw(Graphics g) method so when repainting in java it would use that Graphics object. If you are using a different library, then you will have to do some research in to how to draw it on the board.

Now in your JPanel , it would look something like this:

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

        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM