java - Last square of 8x8 grid not drawn, using two nested FOR loops -
i'm having trouble understanding why code isn't working. should display checkerboard grid 8x8, but last square doesn't drawn! idea why?
i searched see if had been asked before, , didn't find anything. in advance!
code:
/* * file:checkerboard.java * ---------------------- */ import acm.graphics.*; import acm.program.*; public class checkerboard extends graphicsprogram { int row, column, x, y; public void run() { // checkerboard (row = 0; row < 8; row++) { (column = 0; column < 8; column++) { // x, y, x width, y width add(new grect(x, y, 50, 50)); x = column * 50; y = row * 50; } } } }
btw: book i'm reading asks use 2 nested loops ("the art , science of java", chapter 4, exercise 11, cs-106a)
you need set x
, y
before drawing rectangle. otherwise, last rectangle not going display:
for (row = 0; row < 8; row++) { (column = 0; column < 8; column++) { x = column * 50; y = row * 50; // x, y, x width, y width add(new grect(x, y, 50, 50)); } }
better yet, drop x
, y
altogether, , use calculations directly:
for (row = 0; row < 8; row++) { (column = 0; column < 8; column++) { add(new grect(column * 50, row * 50, 50, 50)); } }
Comments
Post a Comment