Java Reference
In-Depth Information
8.
private double x,y,radius;
9.
10.
public Circle( double xx, double yy, double rad)
11.
{
12.
x
= xx;
13.
y
= yy;
14.
radius = rad;
15.
}
16.
17.
// Draws the circle
18.
public void draw(Graphics g)
19.
{
20.
g.fillOval(( int )Math.round(x-radius),( int )Math.round(y-radius),
21.
( int )Math.round(2.0*radius),( int )Math.round(2.0*radius));
22.
}
23.
24.
// Computes the distance between the circle's
25.
// centre and the point (x1,y1).
26.
public double distanceTo( double x1, double y1)
27.
{
28.
return (Math.sqrt(Math.pow(x-x1,2)+Math.pow(y-y1,2) ));
29.
}
30. }
7.2.2
A data structure to administer circles
Class CircleAdministration does most of the work. The heart of it is an object
of type Vector . This is a class predefined in Java; one can think of it as an array
with flexible length. Vectors can store arbitrary objects. If a vector contains n
objects then they are indexed from 0 to n
1. The vector circles used in Circle-
Administration stores the Circle objects currently in our graphic. To access the
i th object one uses the method get(i) . The method returns an Object and thus
one has to cast it to the correct data type, a Circle in our case 1 . The syntax for
this is:
(Circle)(circles.get(i))
Besides the constructor, the class CircleAdministration has the following meth-
ods:
void addCircle(Circle circ)
void removeNearestCircle( int x1, int y1)
void drawAll(Graphics g)
int getNoOfCircles()
1
Version 1.5 of the Java SDK (software development kit) allows typed vectors to be defined.
These can only store objects from a single class, e.g. only Circles . This avoids casting.
Search WWH ::




Custom Search