Java Reference
In-Depth Information
Since version 1.4 of the Java SDK (software development kit) there is a third
listener for the mouse: a MouseWheelListener keeps tracks of the mouse wheel
and is activated every time the mouse wheel is moved.
7.6.2
The design of the application
Let us again summarize the implementation of the model-view-control concept
of this application. In this example the class CircleAdministration does all the
work to keep the abstract data up to date. The circles are stored in Circle-
Administration and this class deletes or adds circles. Only this class knows how
many circles there are. The class InteractivePanel ,onthe other hand, does
the work to display the graphics. It does not know about the circles. In order to
draw the graphic it has to consult CircleAdministration .Itdoes so by calling
the drawAll method of CircleAdministration . Also, if the listener associated
with InteractivePanel detects a click, InteractivePanel only knows whether
to add or delete a circle and the position. It then passes the work to CircleAdmin-
istration by calling the addCircle (or removeNearestCircle ) method. After an
update (add or delete) has been completed by CircleAdministration , Inter-
activePanel updates the display. To this end it calls its method repaint , which
causes the runtime system to call paintComponent at an appropriate time.
Exercises
7.1
Change the application as follows. Add a field color to class Circle as an
instance of java.awt.Color . Define a second constructor for Circle which
allows it to pass a colour in addition. Then modify CircleAdministration
in such a way that the circles in turn receive the colours red, blue, green,
red, etc.
7.2
Modify the application in the following way. Add a line to the status panel
which displays the coordinates of the centre of that circle that is currently
closest to the mouse pointer. Note that this might change with every move
of the mouse. Make sure that the application can handle the situation where
no circle is present.
7.3
Change the application to display line segments instead of circles. A line is
added by two consecutive clicks of the left mouse button. The location of the
first click is one end point of the line, the location of the second click is the
other end point. A click of the right button removes a line nearest to the click
location. There are various ways to define 'nearest', for example one can take
the distance to the first end point, to the middle of the line or the geometrical
distance.
Search WWH ::




Custom Search