Java Reference
In-Depth Information
29.
{
30.
minDist = circ.distanceTo(x1,y1);
31.
minDistIndex = i;
32.
}// if
33.
}// for i
34.
if ((minDistIndex >= 0) && (minDist < 30))
35.
{
36.
circles.removeElementAt(minDistIndex);
37.
}// if
38.
}// method
39.
40.
public void drawAll(Graphics g)
41.
{
42.
Circle currentCircle;
43.
for ( int i=0;i<circles.size() ; i++)
44.
{
45.
currentCircle = (Circle)(circles.get(i));
46.
currentCircle.draw(g);
47.
}// for i
48.
}// method
49.
50.
public int getNoOfCircles(){
51.
return (circles.size());
52.
}// method
53. }
7.3
The view part
The view is much like that in Chapter 6. The frame class InteractiveFrame is
just a renamed copy of class MouseEventFrame from Chapter 6.
The panel InteractivePanel is extended with respect to class MouseEvent-
Panel from Chapter 6. It now contains a field of type CircleAdministration
and methods addCircle and removeNearestCircle for adding and deleting a
circle to the circle administration. These methods just pass the mouse posi-
tions to those methods of CircleAdministration with the same name and
then call repaint . tisimportant to call the repaint method of the panel af-
!
ter every deletion and insertion of a circle! Otherwise a circle would only be
added to or deleted from the circle administration - the display would not be
updated.
The paintComponent method of JPanel has to be overridden to draw the cir-
cles. The panel itself knows neither which circles are currently present nor how
to draw them. This is known only by the CircleAdministration . Thus the only
drawing command in paintComponent is a call of the method drawAll of Cir-
cleAdministration .
Search WWH ::




Custom Search