The Java 2D™ API provides several classes that define common geometric objects such as points, lines, curves, and rectangles. These geometry classes are part of thejava.awt.geompackage.
ThePathIteratorinterface defines methods for retrieving elements from a path.
TheShapeinterface provides a set of methods for describing and inspecting geometric path objects. This interface is implemented by theGeneralPathclass and other geometry classes.All examples represented in this section create geometries by using
java.awt.geomand then render them by using theGraphics2Dclass. To begin you obtain aGraphics2Dobject, for example by casting theGraphicsparameter of thepaint()method.public void paint (Graphics g) { Graphics2D g2 = (Graphics2D) g; ... }Point
ThePointclass creates a point representing a location in (x,y) coordinate space. The subclassesPoint2D.FloatandPoint2D.Doubleprovide correspondingly float and double precision for storing the coordinates of the point.//Create Point2D.Double Point2D.Double point = new Point2D.Double(x, y);To create a point with the coordinates 0,0 you use the default constructor,Point2D.Double().
You can use thesetLocationmethod to set the position of the point as follows:Also, the
setLocation(double x, double y)– To set the location of the point- defining coordinates as double valuessetLocation(Point2D p)– To set the location of the point using the coordinates of another point.Point2Dclass has methods to calculate the distance between the current point and a point with given coordinates, or the distance between two points.Line
TheLine2Dclass represents a line segment in (x, y) coordinate space. TheLine2D.FloatandLine2D.Doublesubclasses specify lines in float and double precision. For example:
// draw Line2D.Double g2.draw(new Line2D.Double(x1, y1, x2, y2));This class includes several
setLine()methods to define the endpoints of the line.
Aternatively, the endpoints of the line could be specified by using the constructor for theLine2D.Floatclass as follows:Use the
Line2D.Float(float X1, float Y1, float X2, float Y2)Line2D.Float(Point2D p1, Point2D p2)Strokeobject in theGraphics2Dclass to define the stroke for the line path.Curves
Thejava.awt.geompackage enables you to create a quadratic or cubic curve segment.Quadratic Curve Segment
TheQuadCurve2Dclass implements theShapeinterface. This class represents a quadratic parametric curve segment in (x, y) coordinate space. TheQuadCurve2D.FloatandQuadCurve2D.Doublesubclasses specify a quadratic curve in float and double precision.Several
setCurvemethods are used to specify two endpoints and a control point of the curve, whose coordinates can be defined directly, by the coordinates of other points and by using a given array.
A very useful method,setCurve(QuadCurve2D c), sets the quadratic curve with the same endpoints and the control point as a supplied curve. For example:
// create new QuadCurve2D.Float QuadCurve2D q = new QuadCurve2D.Float(); // draw QuadCurve2D.Float with set coordinates g2.draw(q.setCurve(x1, y1, x2, y2, ctrlx, ctrly));
Cubic Curve Segment
TheCubicCurve2Dclass also implements theShapeinterface. This class represents a cubic parametric curve segment in (x, y) coordinate space.CubicCurve2D.FloatandCubicCurve2D.Doublesubclasses specify a cubic curve in float and double precision.The
CubicCurve2Dclass has similar methods for setting the curve as theQuadraticCurve2Dclass, except with a second control point. For example:
// create new CubicCurve2D.Double CubicCurve2D c = new CubicCurve2D.Double(); // draw CubicCurve2D.Double with set coordinates g2.draw(c.setCurve(x1, y1, x2, y2, ctrlx1, ctrly1, ctrlx2, ctrly2));
Rectangle
Classes that specify primitives represented in the following example extend the
RectangularShapeclass, which implements theShapeinterface and adds a few methods of its own.These methods enables you to get information about a shape’s location and size, to examine the center point of a rectangle, and to set the bounds of the shape.
The
Rectangle2Dclass represents a rectangle defined by a location (x, y) and dimension (w x h). TheRectangle2D.FloatandRectangle2D.Doublesubclasses specify a rectangle in float and double precision. For example:
// draw Rectangle2D.Double g2.draw(new Rectangle2D.Double(x, y, rectwidth, rectheight));The
RoundRectangle2Dclass represents a rectangle with rounded corners defined by a location (x, y), a dimension (w x h), and the width and height of the corner arc. TheRoundRectangle2D.FloatandRoundRectangle2D.Doublesubclasses specify a round rectangle in float and double precision.The rounded rectangle is specified with following parameters:
To set the location, size, and arcs of a
- Location
- Width
- Height
- Width of the corner arc
- Height of the corner acr
RoundRectangle2Dobject, use the methodsetRoundRect(double a, double y, double w, double h, double arcWidth, double arcHeight). For example:
// draw RoundRectangle2D.Double g2.draw(new RoundRectangle2D.Double(x, y, rectwidth, rectheight, 10, 10));
Ellipse
TheEllipse2dclass represents an ellipse defined by a bounding rectangle. TheEllipse2D.FloatandEllipse2D.Doublesubclasses specify an ellipse in float and double precision.Ellipse is fully defined by a location, a width and a height. For example:
// draw Ellipse2D.Double g2.draw(new Ellipse2D.Double(x, y, rectwidth, rectheight));
Arc
To draw a piece of an ellipse, you use theArc2Dclass. This class represents an arc defined by a bounding rectangle, a start angle, an angular extent, and a closure type. TheArc2D.FloatandArc2D.Doublesubclasses specify an ellipse in float and double precision.The
Arc2Dclass defines the following three types of arcs, represented by corresponding constants in this class: OPEN, PIE and CHORD.Several methods set the size and parameters of the arc:
Also, you can use the
- Directly, by coordinates
- By supplied
Point2DandDimension2D- By copying an existing
Arc2DsetArcByCentermethod to specify an arc from a center point, given by its coordinates and a radius.
// draw Arc2D.Double g2.draw(new Arc2D.Double(x, y, rectwidth, rectheight, 90, 135, Arc2D.OPEN));The
ShapesDemo2D.javacode example contains implementations off all described geometric primitives. For more information about classes and methods represented in this section see thejava.awt.geomspecification.