Java Reference
In-Depth Information
Lines
Lines are created using the
Line2D.Float
class. This class takes four arguments: the x,y
coordinate of one endpoint followed by the x,y coordinate of the other. Here's an
example:
Line2D.Float ln = new Line2D.Float(60F,5F,13F,28F);
This statement creates a line between 60, 5 and 13, 28. Note that an
F
is used with the
literals sent as arguments. Otherwise, the Java compiler would assume that the values
were integers.
Rectangles
Rectangles are created by using the
Rectangle2D.Float
class or
Rectangle2D.Double
class. The difference between the two is that one takes
float
arguments, and the other
takes
double
arguments.
Rectangle2D.Float
takes four arguments: x coordinate, y coordinate, width, and height.
The following is an example:
Rectangle2D.Float rc = new Rectangle2D.Float(10F, 13F, 40F, 20F);
This creates a rectangle at 10, 13 that is 40 pixels wide by 20 pixels tall.
Ellipses
Ellipses, which were called ovals in early versions of Java, can be created with the
Ellipse2D.Float
class. It takes four arguments: x coordinate, y coordinate, width, and
height.
The following statement creates an ellipse at 113, 25 with a width of 22 pixels and a
height of 40 pixels:
Ellipse2D.Float ee = new Ellipse2D.Float(113, 25, 22, 40);
Arcs
Of all the shapes you can draw in Java2D, arcs are the most complex to construct.
Arcs are created with the
Arc2D.Float
class, which takes seven arguments:
The x,y coordinate of an invisible ellipse that would include the arc if it was drawn
n
The width and height of the ellipse
n
The starting degree of the arc
n
The number of degrees it travels on the ellipse
n
An integer describing how the arc is closed
n