Java
OOP GUIDE / WORK
GRID CLASS
Topics
- Object arrays
- Using NOOPDraw
TASK –
PART 1 – TILE CLASS
Create a class named Tile that will represent a single Tile that can be drawn
on screen as part of a large Grid pattern (like floor tiles). To keep things simple, we will make Tile
object simply hold their RGB colour.
Also,
to keep the use of the class easy, we will make the instance variables
public. So no data encapsulation here.
It
has the following specifications:
INSTANCE VARIABLES
- public int r – The amount of red
in our colour ranging from 0 to 255.
- public int g – The amount of
green in the colour ranging from 0 to 255.
- public int b – The amount of blue
in the colour ranging from 0 to 255.
CONSTRUCTOR
- A constructor that gets a value
for each of the instance variables.
- A constructor that has no
parameters. It simply sets all
three RGB values to random values from 0 to 255 inclusively.
METHODS
- A method named setRandomColour()
that simply sets r, g and b to random values between 0 and 255
inclusively.
- A method named setColour(int r,
int g, int b) that gets a new set of RGB values and uses them to set the
instance variables.
IMPROVEMENT
It
is often best to only have one piece of code to do a particular task. In the class above, we have two locations
where we create a random colour (constructor and method) and two locations
where the user can specify the colour (again, constructor and method).
If later, one of the code locations was changed, we would have to remember
that the other one would also have to be changed. Otherwise, this could lead to an
inconsistency.
To
avoid this, we will simply make the constructors call the methods instead.
- The first constructor should be
changed to only have one statement – a call the setColour() method.
- The second constructor should be
changed to have one statement – a call to the setRandomColour() method.
TASK –
PART 2 – PRACTICING WITH NOOPDRAW AND TILE
Steps
to follow:
1. Start by copying the NOOPDraw class to your project.
2.
Now create a TileTester class and add a main function to it.
3.
Inside main, start by creating a NOOPDraw window.
4.
Create a Tile t object with a random colour.
5.
Use NOOPDraw.setColor(…) to set the NOOPDraw drawing colour to the Tile’s
colour.
6.
Fill a rectangle located at (0, 0) with a width and height of 20.
7.
Steps 4-6 were needed to create a rectangle based on the colour of a
Tile. Repeat all three steps to create
another rectangle but place this one at (20, 0) instead.
8. Again, repeat steps 4-6 to create a third rectangle at (40, 0).
There
is a pattern developing for the rectangles.
If we created an array of Tiles, we could draw them using a loop. That is what we will do in the next part.
TASK –
PART 3 – THE GRID CLASS
The
Grid class will have a 2D array of Tile objects and will display them on the
screen in a Grid like display. It will
make use of NOOPDraw for the displaying.
Here
is the file specification:
INSTANCE VARIABLES
·
public Tile[][] grid – A 2D Tile array holding all the tiles
·
public int tileSize – The size of the tile in pixels
CONSTRUCTOR
- A constructor gets a number of rows
and columns and creates a 2D Tile array with those values.
It then loops over the array with nested for loops and creates a Tile
object with random colours.
Finally, it sets the tileSize instance variable to 20 (for now).
METHODS
- A draw() method will be used to
draw the Tiles from the array on to the screen. Here are the details:
- First, add the following comment
above the draw() method header:
//Pre-condition: NOOPDraw window must already be created for this to
work.
- Now add the draw method header.
- Inside the method, loop over the
grid array using nested for loops.
Use r and c as control variables.
- Inside the loops, you have two
things to do. First, set the
drawing colour to the current tile’s colour. Then, draw the rectangle on the
screen.
A few notes:
- The current tile is reffered to
as grid[r][c].
- To set the NOOPDraw drawing
colour to the current tile’s colour, we can use:
NOOPDraw.setColor(grid[r][c].r, grid[r][c].g, grid[r][c].b);
but I prefer using two lines of code instead:
Tile t = grid[r][c];
NOOPDraw.setColor(t.r, t.g, t.b);
- We now need to draw (fill) the
rectangle that represents the tile.
To do this, we need to figure out a pattern between r and c in
the array and x and y on the window.
Click here to see guide on how to
determine the pattern.
|