draw 2d shapes in 3d java

The Coffee 2D API provides several classes that define common geometric objects such as points, lines, curves, and rectangles. These geometry classes are part of the java.awt.geom package.
The PathIterator interface defines methods for retrieving elements from a path.
The Shape interface provides a set of methods for describing and inspecting geometric path objects. This interface is implemented by the GeneralPath class and other geometry classes.

All examples represented in this section create geometries by using coffee.awt.geom package and then render them by using the Graphics2D class. To begin y'all obtain a Graphics2D object, for example by casting the Graphics parameter of the pigment() method.

public void paint (Graphics g) {     Graphics2D g2 = (Graphics2D) 1000;     ... }          

Point

The Indicate grade creates a point representing a location in (x,y) coordinate space. The subclasses Point2D.Float and Point2D.Double provide correspondingly float and double precision for storing the coordinates of the indicate.

//Create Point2D.Double Point2D.Double point = new Point2D.Double(10, y);          

To create a point with the coordinates 0,0 yous use the default constructor, Point2D.Double().
You can use the setLocation method to prepare the position of the indicate as follows:

  • setLocation(double 10, double y) – To set the location of the signal- defining coordinates as double values.
  • setLocation(Point2D p) – To set the location of the point using the coordinates of another betoken.

Also, the Point2D form has methods to calculate the altitude betwixt the electric current point and a signal with given coordinates, or the distance between two points.

Line

The Line2D form represents a line segment in (ten, y) coordinate space. The Line2D. Bladder and Line2D.Double subclasses specify lines in float and double precision. For example:

// draw Line2D.Double g2.draw(new Line2D.Double(x1, y1, x2, y2));          

Line

This form includes several setLine() methods to define the endpoints of the line.
Alternatively, the endpoints of the line could be specified past using the constructor for the Line2D.Float class as follows:

  • Line2D.Float(float X1, float Y1, float X2, float Y2)
  • Line2D.Float(Point2D p1, Point2D p2)

Use the Stroke object in the Graphics2D grade to define the stroke for the line path.

Curves

The coffee.awt.geom packet enables you to create a quadratic or cubic curve segment.

Quadratic Curve Segment

The QuadCurve2D class implements the Shape interface. This grade represents a quadratic parametric bend segment in (x, y) coordinate space. The QuadCurve2D.Float and QuadCurve2D.Double subclasses specify a quadratic curve in float and double precision.

Several setCurve methods are used to specify 2 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), sets the quadratic curve with the same endpoints and the control signal as a supplied curve. For example:

// create new QuadCurve2D.Float QuadCurve2D q = new QuadCurve2D.Float(); // draw QuadCurve2D.Bladder with prepare coordinates q.setCurve(x1, y1, ctrlx, ctrly, x2, y2); g2.draw(q);          

Quadratic parametric curve segment

Cubic Curve Segment

The CubicCurve2D class as well implements the Shape interface. This class represents a cubic parametric curve segment in (x, y) coordinate space. CubicCurve2D.Bladder and CubicCurve2D.Double subclasses specify a cubic curve in float and double precision.

The CubicCurve2D grade has similar methods for setting the curve as the QuadraticCurve2Dclass, except with a second command point. For instance:

// create new CubicCurve2D.Double CubicCurve2D c = new CubicCurve2D.Double(); // draw CubicCurve2D.Double with set coordinates c.setCurve(x1, y1, ctrlx1,            ctrly1, ctrlx2, ctrly2, x2, y2); g2.draw(c);          

Cubic Curve Segment

Rectangle

Classes that specify primitives represented in the post-obit example extend the RectangularShape class, which implements the Shape interface and adds a few methods of its own.

These methods enables you to become 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 Rectangle2D class represents a rectangle defined by a location (10, y) and dimension (due west x h). The Rectangle2D.Float and Rectangle2D.Double subclasses specify a rectangle in float and double precision. For instance:

// draw Rectangle2D.Double g2.describe(new Rectangle2D.Double(x, y,                                rectwidth,                                rectheight));          

Rectangle

The RoundRectangle2D form represents a rectangle with rounded corners defined by a location (x, y), a dimension (westward ten h), and the width and height of the corner arc. The RoundRectangle2D.Float and RoundRectangle2D.Double subclasses specify a round rectangle in float and double precision.

The rounded rectangle is specified with following parameters:

  • Location
  • Width
  • Height
  • Width of the corner arc
  • Top of the corner arc

To set the location, size, and arcs of a RoundRectangle2D object, use the method setRoundRect(double a, double y, double west, double h, double arcWidth, double arcHeight). For example:

// draw RoundRectangle2D.Double g2.draw(new RoundRectangle2D.Double(ten, y,                                    rectwidth,                                    rectheight,                                    10, 10));          

Rounded Rectangle

Ellipse

The Ellipse2D class represents an ellipse defined by a bounding rectangle. The Ellipse2D.Bladder and Ellipse2D.Double subclasses specify an ellipse in bladder and double precision.

Ellipse is fully divers by a location, a width and a meridian. For instance:

// draw Ellipse2D.Double g2.draw(new Ellipse2D.Double(ten, y,                              rectwidth,                              rectheight));          

Ellipse

Arc

To depict a piece of an ellipse, you use the Arc2D course. This form represents an arc defined by a bounding rectangle, a get-go angle, an angular extent, and a closure blazon. The Arc2D.Float and Arc2D.Double subclasses specify an arc in float and double precision.

The Arc2D form defines the following 3 types of arcs, represented past corresponding constants in this grade: OPEN, PIE and CHORD.

Arc

Several methods set the size and parameters of the arc:

  • Directly, by coordinates
  • By supplied Point2D and Dimension2D
  • By copying an existing Arc2D

Also, you can apply the setArcByCenter method to specify an arc from a center point, given by its coordinates and a radius.

// depict Arc2D.Double g2.draw(new Arc2D.Double(ten, y,                          rectwidth,                          rectheight,                          90, 135,                          Arc2D.Open up));          

Arc

The ShapesDemo2D.coffee lawmaking case contains implementations of all described geometric primitives. For more information about classes and methods represented in this section, see the java.awt.geom specification.

powerspearouble.blogspot.com

Source: https://docs.oracle.com/javase/tutorial/2d/geometry/primitives.html

0 Response to "draw 2d shapes in 3d java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel