2016-09-07 28 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); 

     } 

    } 
相关问题