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


i attempting draw checkerboard pattern in java using nested loops, having trouble doing 2 different colors. know question has been asked before, hasn't been asked 2 different colors on board not using background color. plan on using individual squares array hold checker positions, need each individual square made. better drop ice of nested loop create each square, or should stick shortcut? , if stick it, how nested loop formatted (one each color)?

when creating checker tiles, pass in int x coordinate, , 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, same width square              public static int currentid = 0; //variable reference unique id 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 take x , 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 tile on panel.             public void draw(graphics g)             {                 //if checkertile's id divisible 2, draw red, otherwise draw black.                 g.setcolor( id % 2 == 0 ? color.red : color.black);                 g.fillrect(x, y, width, height);             }          } 

that way have way draw tile on board. now, when creating each object, increment currentid variable can color each 1 individually using modulus operator later.

i assuming using swing decided add draw(graphics g) method when repainting in java use graphics object. if using different library, have research in how draw on board.

now in jpanel, this:

    //creates jpanel, needs added 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 checkertiles.             for(int = 0; < 9; i++)             {                 for( int j = 0; j < 9; j++)                 {                     checkertiles[i][j] = new checkertile( j * checkertile.width, * checkertile.height );                 }             }               this.setvisible(true);              //repaint right away show results.             repaint();          }          //we need override paint tiles on board.         @override         public void paintcomponent(graphics g)         {             for(int = 0; < checkertiles.length; i++)             {                 for(int j = 0; j < checkertiles[i].length; j++)                 {                     //call draw method on each tile.                     checkertiles[i][j].draw(g);                 }             }         }          //a demo of adding panel frame , showing tiles.         public static void main(string[] args)         {             //create jframe , add checkerboard made it.             jframe frame = new jframe();             frame.setsize(800,800);             frame.setlayout(new borderlayout());             frame.add(new checkerboard(), borderlayout.center);             frame.setvisible(true);          }      } 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -