Program To Draw Shapes In Java

Posted on by
See More On Stackoverflow

I want to write a program which can draw any type of shape that I assign to it like Circle Square Rectangle Which library should I use, and how do I go about it in. Drawing simple shapes by mouse dragging. Move the logic to draw different shapes into concrete subclasses. Extremely Simple Paint Program in Java.

Java 2D: Graphics in Java 2. /** An example of drawing/filling shapes with Java 2D in * Java 1.2 and later. Software Development & Management. Java - Drawing Shapes Example in java Introduction. Applet is a program to run on the browser and it is embedded on the web page.

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 the package.

The interface defines methods for retrieving elements from a path. The interface provides a set of methods for describing and inspecting geometric path objects. This interface is implemented by the class and other geometry classes.

All examples represented in this section create geometries by using java.awt.geom package and then render them by using the class. Wake Island 2007 Battlefield 2 D Hotels Hotel Vista Bahia Club Ibiza. on this page. To begin you obtain a Graphics2D object, for example by casting the Graphics parameter of the paint() method. //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 the setLocation method to set the position of the point as follows: • setLocation(double x, double y) – To set the location of the point- defining coordinates as double values. • setLocation(Point2D p) – To set the location of the point using the coordinates of another point. Also, the Point2D class has methods to calculate the distance between the current point and a point with given coordinates, or the distance between two points.

Line The class represents a line segment in (x, y) coordinate space. Float and Line2D.Double subclasses specify lines in float and double precision. // 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 the Line2D.Float class as follows: • Line2D.Float(float X1, float Y1, float X2, float Y2) • Line2D.Float(Point2D p1, Point2D p2) Use the object in the Graphics2D class to define the stroke for the line path. Curves The java.awt.geom package enables you to create a quadratic or cubic curve segment.

Quadratic Curve Segment The class implements the Shape interface. This class represents a quadratic parametric curve 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 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), sets the quadratic curve with the same endpoints and the control point as a supplied curve. // create new QuadCurve2D.Float QuadCurve2D q = new QuadCurve2D.Float(); // draw QuadCurve2D.Float with set coordinates q.setCurve(x1, y1, ctrlx, ctrly, x2, y2); g2.draw(q); Cubic Curve Segment The class also implements the interface. This class represents a cubic parametric curve segment in (x, y) coordinate space.

CubicCurve2D.Float and CubicCurve2D.Double subclasses specify a cubic curve in float and double precision. The CubicCurve2D class has similar methods for setting the curve as the QuadraticCurve2Dclass, except with a second control point. // 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); Rectangle Classes that specify primitives represented in the following example extend the RectangularShape class, which implements the Shape interface 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 class represents a rectangle defined by a location (x, y) and dimension (w x h). The Rectangle2D.Float and Rectangle2D.Double subclasses specify a rectangle in float and double precision.

Comments are closed.