簡體   English   中英

繪制帶有嵌套for循環的兩個彩色棋盤,其中每個正方形都是自己的對象(Java)

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

我正在嘗試使用嵌套的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);

        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM