2016-09-07 21 views
0

我試圖在java中使用嵌套for循環繪製棋盤圖案,但我在使用兩種不同顏色時遇到問題。我知道這個問題之前已經被問到過,但是它並沒有被問到板子上的兩種不僅僅是使用背景顏色的不同顏色。我計劃使用單個方塊作爲數組來保持方格位置,所以我確實需要製作每個單獨的方塊。爲了創建每個方塊,放下嵌套for循環的冰塊會更好嗎?還是應該堅持使用這個快捷方式?如果我堅持下去,嵌套循環如何格式化(每種顏色一個)?繪製一個嵌套for循環的兩個彩色棋盤,其中每個方塊都是它們自己的對象(Java)

回答

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

     } 

    } 
相關問題