All the applet does is print a brick wall. I've got it made with using for loops, but now I'd like to get it done using while and do while loops, but I can't seem to get it working using while loops, and I haven't even attempted doing it using do while loops. Any suggestions? All it does is print the first column for some reason.
This is my fully working "for loop" version:
package ton_of_bricks; import java.applet.Applet; import java.awt.*; public class Ton_of_bricks_for extends Applet{ private static final long serialVersionUID = 1L; public static final int START_X = 0; public static final int START_Y = 0; public static final int BRICK_WIDTH = 60; public static final int BRICK_HEIGHT = 30; public static final int DISPLAY_WIDTH = 600; public static final int DISPLAY_HEIGHT = 600; public static final int GAP = 10; public void init(){ resize(DISPLAY_WIDTH, DISPLAY_HEIGHT); setBackground(Color.darkGray); } public void paint(Graphics g){ int x = START_X; int y = START_Y; g.setColor(Color.RED); for (int row = 1; row <= DISPLAY_WIDTH; row++){ //while row is less than windows width, increase rows for (int col = 1; col <= DISPLAY_HEIGHT; col++){ //while col is less than windows height, increase cols g.fillRect (x, y, BRICK_WIDTH, BRICK_HEIGHT); //drawing bricks x += BRICK_WIDTH + GAP; //generates columns } if (row%2 == 0) x = START_X; else x = START_X - BRICK_WIDTH/2 - GAP/2; //offsets bricks y += BRICK_HEIGHT + GAP; //generates rows } //end for(rows) } //end pain } //public class ton_of_bricks_for
This is my broken "while loop" version, where only the first column is working:
package ton_of_bricks; import java.applet.Applet; import java.awt.*; public class Ton_of_bricks_while extends Applet{ private static final long serialVersionUID = 1L; public static final int START_X = 0; public static final int START_Y = 0; public static final int BRICK_WIDTH = 30; public static final int BRICK_HEIGHT = 15; public static final int DISPLAY_WIDTH = 600; public static final int DISPLAY_HEIGHT = 600; public static final int GAP = 5; public void init(){ resize(DISPLAY_WIDTH, DISPLAY_HEIGHT); setBackground(Color.darkGray); } public void paint(Graphics g){ int x = START_X; int y = START_Y; g.setColor(Color.RED); int row = 1; int col = 1; while (row <= DISPLAY_WIDTH){ //while row is less than windows width, increase rows row++; while (col <= DISPLAY_HEIGHT) //while col is less than windows height, increase cols col++; g.fillRect (x, y, BRICK_WIDTH, BRICK_HEIGHT); //drawing bricks x += BRICK_WIDTH + GAP; //generates columns if (row%2 == 0) x = START_X; else x = START_X - BRICK_WIDTH/2 - GAP/2; //offsets bricks y += BRICK_HEIGHT + GAP; //generates rows } //end for(rows) } //end pain }//public class ton_of_bricks_for
Any suggestions on fixing the problem? I would also appreciate it if anyone could point me in the right direction of starting the "do while" version as well. Thank you!