Java Reference
In-Depth Information
A graphic context knows how to draw Shape objects. To draw a shape on a component, you just need to
pass the object defining the shape to the draw() method for the Graphics2D object for the component. To
look at this in detail, we'll split the shapes into three groups, straight lines and rectangles, arcs and ellipses,
and freeform curves. First though, we must take a look at how points are defined.
Classes Defining Points
There are two classes in the
java.awt.geom package that define
points, Point2D.Float and
Point2D.Double . From the class names
you can see that these are both inner
classes to the class Point2D , which also
happens to be an abstract base class for
both too. The Point2D.Float class
defines a point from a pair of (x,y)
coordinates of type float, whereas the
Point2D.Double class defines a point as
a coordinate pair of type double. The
Point class in the java.awt package
also defines a point, but in terms of a
coordinate pair of type int . This class also
has Point2D as a base.
java.awt.geom
abstract
base class
Point2D
java.awt
Java.awt.geom
Java.awt.geom
Point
Point2D.Double
Point2D.Float
Coordinates
of type Int
Coordinates
of type double
Coordinates
of type float
The Point class actually predates the Point2D class, but the class was redefined to make it a subclass
of Point2D when Point2D was introduced, hence the somewhat unusual class hierarchy with only two
of the subclasses as inner classes. The merit of this arrangement is that all of the subclasses inherit the
methods defined in the Point2D class, so operations on each of the three kinds of point are the same.
The three subclasses of Point2D define a default constructor that defines the point 0,0, and a
constructor that accept a pair of coordinates of the type appropriate to the class type.
The operations that each of the three concrete point classes inherit are:
1.
Accessing coordinate values:
The getX() and getY() methods return the x and y coordinates of a point as type double,
regardless of how the coordinates are stored. These are abstract methods in the Point2D class
so they are defined in each of the subclasses. Although you get coordinates as double values
from all three concrete classes via these methods you can always access the coordinates with
their original type directly since the coordinates are stored in public fields with the same
names, x and y, in each case.
2.
Calculating the distance between two points:
You have no less that three overloaded versions of the distance() method for calculating
the distance between two points, and returning it as type double:
Search WWH ::




Custom Search