Java Reference
In-Depth Information
addCircle(circ) adds circle circ to vector circles .
removeNearestCircle(x1,y1) determines a circle stored in vector circles
whose centre has minimum distance to the point ( x
,
y ). If the distance is at
most 30 (pixel), then the circle is removed from circles .
drawAll(g) draws all circles currently stored in circles . This is done by calling
the draw -method of each of them. The Graphics object g is passed on.
getNoOfCircles() returns the number of circles currently stored in vector cir-
cles .
In method removeNearestCircle(x1,y1) the circle to be removed is found by
computing the distances of the point ( x 1 ,
y 1 )tothe centre of all circles. This
is not the most effective way of doing this but we choose it to keep the code
short.
File: its/InteractiveGraphic/CircleAdministration.java
package its.InteractiveGraphic;
1.
2.
3.
import java.awt.Graphics;
import java.util.Vector;
4.
5.
6.
public class CircleAdministration
{
7.
private Vector circles;
8.
9.
10.
public CircleAdministration()
11.
{
12.
circles = new Vector();
13.
}
14.
15.
public void addCircle(Circle circ)
16.
{
17.
circles.add(circ);
18.
}
19.
20.
public void removeNearestCircle( int x1, int y1)
21.
{
22.
Circle circ;
double minDist = Double.MAX_VALUE;
23.
int
minDistIndex = -1;
24.
for ( int i=0;i<circles.size() ; i++)
25.
{
26.
circ = (Circle)(circles.get(i));
27.
if (circ.distanceTo(x1,y1) < minDist)
28.
Search WWH ::




Custom Search