In this assignment you will implement methods for drawing in a two-dimensional grid by changing the colors of the cells in the grid. This will give you practice with developing algorithms for two-dimensional data structures.†
The two-dimensional data structure used in this lab is represented by a
BoundedGrid
object made up of rows and columns.‡ A
BoundedGrid
object models a bounded rectangular grid that contains
objects at various grid locations. Each cell contains zero or one objects.
In this program, cells that are not empty will contain color blocks (objects of
the ColorBlock
class).
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |||||||||||||||||||||||||||||||||||||
0 |
| ||||||||||||||||||||||||||||||||||||||||||||
1 | |||||||||||||||||||||||||||||||||||||||||||||
2 | |||||||||||||||||||||||||||||||||||||||||||||
3 |
We refer to locations in the grid by their row and column numbers in
parentheses, such as location (2, 7). Row and column numbers start at 0 rather
than 1, so location (0, 0) refers to the first row and first column. Object
obj1
in the grid shown above is in the first row and fourth column,
or at location (0, 3). Object obj5
is at location (3, 8). (This is
similar to the way Java array and ArrayList
indices are numbered.)
In this assignment you will define methods for drawing in the two-dimensional grid by putting color blocks in the cells of the grid.
In this exercise you will be experimenting with the Grid Plotter program to see how it works.
Exercise Set 1: Becoming Familiar with the Existing Program
|
The starting point of the Grid Plotter application is the
GridPlotterApp
class, which contains the main
method.
If you look over the class, you will see that it does remarkably little. It
creates two constants that define the size of the window containing the
graphical user interface, one that defines the smallest allowable grid cell
size, and two more that define the extreme values for the speed adjustment
slider. The class's main
method creates a graphical user interface
object, tells it to include a "Help" menu, and asks it to construct the window
contents (this also makes the window visible on the screen). Notice that
the main
method does not say anything about the "File" menu or the
various buttons and pull-down menus on the graphical user interface; those are
specified in the GridPlotterGUI
constructor.
Exercise Set 2: Experimenting with the
|
We will not look at the GridPlotterGUI
class, which includes
some quite advanced features of Java. Instead, let's look at the
GridPlotter
class. The graphical user interface calls methods of
this class whenever a user constructs a new grid, presses one of the drawing
buttons, or presses the clear button. As you read through the
GridPlotter
class you may wish to read the class
documentation for some classes it uses from grid.jar
, such as
Grid
, Location
, ColorBlock
, and
ColorChoiceMenu
.
Exercise Set 3: Reading and Understanding the
|
Now it's time to write some drawing methods of your own.
Exercise Set 4: Writing Your Own Methods
|