Program To Draw Shapes In Java
You have already learned how to draw most of shapes represented in the java.awt.geom package. To create more complicated geometry, such as polygons, polylines, or stars you use another class from this package, GeneralPath. A Simple Java Drawing Tool. Null, 'This small freeware program is written. Class TriangleShape extends Shapes { public void draw(java.util.
Core Approach When overriding the paintComponent method of a Swing component, always call super.paintComponent. Modify drawing parameters (optional). Drawing parameters are applied to the Graphics2D object, not to the Shape object. Changes to the graphics context ( Graphics2D) apply to every subsequent drawing of a Shape. SetPaint(fillColorOrPattern); g2d. SetStroke(penThicknessOrPattern); g2d. SetComposite(someAlphaComposite); g2d.
SetFont(someFont); g2d. Translate(.); g2d. Rotate(.); g2d.
Scale(.); g2d. Shear(.); g2d. SetTransform(someAffineTransform); Create a Shape object. Rectangle2D.Double rect =.; Ellipse2D.Double ellipse =.; Polygon poly =.; GeneralPath path =.; // Satisfies Shape interface SomeShapeYouDefined shape =.; Draw an outlined or filled version of the Shape. Pass in the Shape object to either the draw or fill method of the Graphics2D object.
The graphic context (any paint, stroke, or transform applied to the Graphics2D object) will define exactly how the shape is drawn or filled. Draw(someShape); g2d.
Fill(someShape); The Graphics2D class extends the Graphics class and therefore inherits all the familiar AWT graphic methods covered in Section 9.11 (Graphics Operations). The Graphics2D class adds considerable functionality to drawing capabilities. Methods that affect the appearance or transformation of a Shape are applied to the Graphics2D object. Once the graphics context is set, all subsequent Shapes that are drawn will undergo the same set of drawing rules.
Keep in mind that the methods that alter the coordinate system ( rotate, translate, scale) are cumulative. Useful Graphics2D Methods The more common methods of the Graphics2D class are summarized below. Public void draw(Shape shape) This method draws an outline of the shape, based on the current settings of the Graphics2D context. By default, a shape is bound by a Rectangle with the upper-left corner positioned at (0,0). To position a shape elsewhere, first apply a transformation to the Graphics2D context: rotate, transform, translate. Public boolean drawImage(BufferedImage image, BufferedImageOp filter, int left, int top) This method draws the BufferedImage with the upper-left corner located at ( left, top). A filter can be applied to the image.
See Section 10.3 (Paint Styles) for details on using a BufferedImage. Public void drawString(String s, float left, float bottom) The method draws a string in the bottom-left corner of the specified location, where the location is specified in floating-point units. The Java 2D API does not provide an overloaded drawString method that supports double arguments. Thus, the method call drawString(s, 2.0, 3.0) will not compile. Correcting the error requires explicit statement of floating-point, literal arguments, as in drawString(s, 2.0f, 3.0f). Java 2D supports fractional coordinates to permit proper scaling and transformations of the coordinate system.

Java 2D objects live in the User Coordinate Space where the axes are defined by floating-point units. When the graphics are rendered on the screen or a printer, the User Coordinate Space is transformed to the Device Coordinate Space. The transformation maps 72 User Coordinate Space units to one physical inch on the output device. Thus, before the graphics are rendered on the physical device, fractional values are converted to their nearest integral values. Public void fill(Shape shape) This method draws a solid version of the shape, based on the current settings of the Graphics2D context. See the draw method for details of positioning. Public void rotate(double theta) This method applies a rotation of theta radians to the Graphics2D transformation.
See More On Stackoverflow
The point of rotation is about ( x, y)=(0, 0). This rotation is added to any existing rotations of the Graphics2D context. See Section 10.7 (Coordinate Transformations). Public void rotate(double theta, double x, double y) This method also applies a rotation of theta radians to the Graphics2D transformation. However, the point of rotation is about ( x, y).
See Section 10.7 (Coordinate Transformations) for details. Public void scale(double xscale, yscale) This method applies a linear scaling to the x- and y-axis. Values greater than 1.0 expand the axis, and values less than 1.0 shrink the axis. A value of -1 for xscale results in a mirror image reflected across the x-axis. A yscale value of -1 results in a reflection about the y-axis. Public void setComposite(Composite rule) This method specifies how the pixels of a new shape are combined with the existing background pixels.
You can specify a custom composition rule or apply one of the predefined AlphaComposite rules: AlphaComposite.Clear, AlphaComposite.DstIn, AlphaComposite.DstOut, AlphaComposite.DstOver, AlphaComposite.Src, AlphaComposite.SrcIn, AlphaComposite.SrcOut, AlphaComposite.ScrOver. To create a custom AlphaComposite rule, call getInstance as in g2d.setComposite(AlphaComposite.SrcOver); or int type = AlphaComposite.SRCOVER; float alpha = 0.75f; AlphaComposite rule = AlphaComposite.getInstance(type, alpha); g2d.setComposite(rule); The second approach permits you to set the alpha value associated with composite rule, which controls the transparency of the shape.
By default, the transparency value is 1.0f (opaque). See Section 10.4 (Transparent Drawing) for details.
Clarification of the mixing rules is given by T. Porter and T. Duff in 'Compositing Digital Images,' SIGGRAPH 84, pp. Detailed project report solar power plant pdf. Public void setPaint(Paint paint) This method sets the painting style of the Graphics2D context. Any style that implements the Paint interface is legal. Existing styles in the Java 2 Platform include a solid Color, a GradientPaint, and a TexturePaint.
Public void setRenderingHints(Map hints) This method allows you to control the quality of the 2D drawing. The AWT includes a RenderingHints class that implements the Map interface and provides a rich suite of predefined constants.
Quality aspects that can be controlled include antialiasing of shape and text edges, dithering and color rendering on certain displays, interpolation between points in transformations, and fractional text positioning. Typically, antialiasing is turned on, and the image rendering is set to quality, not speed: RenderingHints hints = new RenderingHints( RenderingHints.KEYANTIALIASING, RengeringHints.VALUEANTIALIASON); hints.add(new RenderingHints( RenderingHints.KEYRENDERING, RenderingHints.VALUERENDERQUALITY)); public void setStroke(Stroke pen) The Graphics2D context determines how to draw the outline of a shape, based on the current Stroke. This method sets the drawing Stroke to the behavior defined by pen. A user-defined pen must implement the Stroke interface. The AWT includes a BasicStroke class to define the end styles of a line segment, to specify the joining styles of line segments, and to create dashing patterns. See Section 10.6 (Stroke Styles) for details. Public void transform(AffineTransform matrix) This method applies the Affine transformation, matrix, to the existing transformation of the Graphics2D context.
The Affine transformation can include both a translation and a rotation. See Section 10.7 (Coordinate Transformations). Public void translate(double x, double y) This method translates the origin by ( x, y) units. This translation is added to any prior translations of the Graphics2D context. The units passed to the drawing primitives initially represent 1/72nd of an inch, which on a monitor, amounts to one pixel. However, on a printer, one unit might map to 4 or 9 pixels (300 dpi or 600 dpi).
Public void setPaintMode This method overrides the setPaintMode method of the Graphics object. This implementation also sets the drawing mode back to 'normal' (vs. However, when applied to a Graphics2D object, this method is equivalent to setComposite(AlphaComposite.SrcOver), which places the source shape on top of the destination (background) when drawn. Public void setXORMode(Color color) This method overrides the setXORMode for the Graphics object. For a Graphics2D object, the setXORMode method defines a new compositing rule that is outside the eight predefined Porter-Duff alpha compositing rules (see Section 10.4). The XOR compositing rule does not account for transparency (alpha) values and is calculated by a bitwise XORing of the source color, destination color, and the passed-in XOR color. Using XOR twice in a row when you are drawing a shape will return the shape to the original color.
The transparency (alpha) value is ignored under this mode, and the shape will always be opaque. In addition, antialiasing of shape edges is not supported under XOR mode.