Java Reference
In-Depth Information
Interactive graphics
7
In Chapter 5 we saw how an application can display predefined graphics. In Chap-
ter 6 the mouse was embedded into an application. We shall now see how to
combine these two things so that one can interactively generate and manipulate
graphics.
We extend the application from Chapter 6 by an interactive part. This will enable
you to generate and alter a drawing using the mouse. The drawing consists of
circles that are added to or removed by mouse clicks. The resulting application
has a model, a view and a control part. The model part consists of a class defining
a circle and a class defining a data structure which stores the circles currently in
the graphic and which also supplies methods to add a circle or remove one. The
view part is mostly like the application in Chapter 6. The class MouseEventPanel
of Chapter 6 is augmented by overriding its paintComponent method as described
in Chapter 5. The control part is implemented by extending the listeners from
Chapter 6.
7.1
Specification of the GUI
The layout is very much like that in Figure 6.1. We therefore specify only the
additional functions:
If clicking the left mouse button inside the 300
×
300 panel, a small filled black
circle appears at the current mouse position.
If clicking the right mouse button the circle closest to the current mouse po-
sition is removed, but only if the distance is less than 30 pixels. Distances are
in the Euclidean metric of
2 .Ifmore than one circle has the same distance
to the click position, choose an arbitrary one.
R
The status panel now also displays the current number of circles.
Search WWH ::




Custom Search