Java OOP GUIDE / WORK HINT – DRAWING TILES AT RIGHT LOCATION The array holds the colours of each tile. To draw the
tiles, we need to find a pattern between the element’s r and c in the array
and the corresponding x, y, width and height (which
we need to draw a rectangle in NOOPDraw).
To discover a pattern, we will look at a table of tiles and list
out the values or r, c, x, y, width and height. TABLE 1 –
TILES IN THE TOP ROW Here is a table considering the first row of
squares. A pattern is easy to find for the value of x.
TABLE 2 –
TILES IN THE LEFTMOST COLUMN Here is a table considering the first column of
squares. Again, a pattern is easy to find for y.
Now we can also consider a few different squares that we
randomly picked to find an overall pattern.
So, the statement NOOPDraw.fillRectangle(c*20, r*20, 20, 20); will draw grid[r][c] of the array at its
proper location.
So
that the grid can be scaled based on the desired tileSize,
we can replace all of the 20s by the variable tileSize
(which is an instance variable). NOOPDraw.fillRectangle(c*tileSize, r*tileSize, tileSize, tileSize); |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|