| Lab | Assigned | Design Due (Mondays 2 PM) |
Lab Due (Fridays 2 PM) |
|||
|---|---|---|---|---|---|---|
| 22 | Jan | 25 | Jan | 29 | Jan | |
Such a display could be driven directly by keypresses or mouse actions, but then the display's use would be restricted to those input devices. Instead, we abstract the display's input so that it can be driven by a variety of sources, including a mouse-click pad that you implement in this lab. In later labs, the display might be driven by other objects that you create.
As will be the case in most of our labs, you will build upon code that is provided to you. Ideally, the API (Application Programming Interface) tells you all you need to know to use this code. In this lab, the segment display and the mouse-click pad are each based on (separate instances of) the CS101.canvas object, whose relevant features are described in this handout.
The system you design and build will contain at least the following kinds of objects (classes):
DirectionVector
ints.
The object represents relative horizontal and
vertical displacement. In Physics, direction vectors
appear in free-body diagrams
to show components of a two-dimensional force.
MousePad
MousePad
should have a method that return the relative displacement between the
two clicks. This displacement is ideally encoded as
a DirectionVector.
SegmentDisplay
DirectionVector. The
specified vector is then drawn, starting at the location of the previous
gesture.
Like its toy analog, the display for Craf-ta-Graf must contain code to keep the drawing from going off the screen.
MousePad
and SegmentDisplay objects communicate? With respect
to object containment, there are three possibilities:
MousePad contains the SegmentDisplay.
Thus, the MousePad is in control. When a click is registered,
the MousePad object synthesizes a
DirectionVector, passing it to the appropriate method in the
SegmentDisplay to update the display.
SegmentDisplay contains the MousePad.
In this design, the display is in control, calling the MousePad
for the next click. A DirectionVector is synthesized, and
the display is updated.
MousePad and the
SegmentDisplay is managed by a third party.
This API may give you an idea how to organize your APIs, in terms of presenting each class's constructors, methods, and instance variables. Your design need not look as nifty as what you see on the web pages---these were generated automatically using javadoc.
MousePad and SegmentDisplay will interact. Think about the three possibilities described
above, and pick the one that promotes the cleanest design and greatest
possibility of code reuse.
Keeping in mind that coordinate (0,0) is at the upper left of the canvas,
read the testCanvas
and predict what the picture will
look like. Then
execute the program to confirm your ideas. If things look different than
you expected, try to find out why. Remember to ask for help as needed.
Note that all coordinates are in pixels, (a word whose origin is "picture elements"). Think of a pixel as the smallest possible dot on the screen. A typical television picture is 640 pixels wide and 480 pixels in height.
DirectionVector API
DirectionVector class is as follows.
DirectionVector(int x, int y) constructs an immutable
direction vector with the supplied x and y components.
getX() returns the x component.
getY() returns the y component.
DirectionVector add(DirectionVector)
returns a new direction vector that
is the vector sum of this direction vector and
the supplied one.
double magnitude() returns the magnitude of
this vector.
toString() represents the direction vector as a
mathematical tuple.
x is the horizontal component
y is the vertical component