org.apache.xmlgraphics.java2d

## Class DefaultGraphics2D

- java.lang.Object
- - java.awt.Graphics
  - - java.awt.Graphics2D
    - - org.apache.xmlgraphics.java2d.AbstractGraphics2D
      - - org.apache.xmlgraphics.java2d.DefaultGraphics2D

- All Implemented Interfaces:
    java.lang.Cloneable

      public class DefaultGraphics2D
      extends AbstractGraphics2D

  This concrete implementation of `AbstractGraphics2D` is a simple help to programmers to get started with their own implementation of `Graphics2D`. `DefaultGraphics2D` implements all the abstract methods is `AbstractGraphics2D` and makes it easy to start implementing a `Graphic2D` piece-meal.
  Version:
    $Id$
  See Also:
    `Originally authored by Vincent Hardy.`

- - ### Field Summary

    - ### Fields inherited from class org.apache.xmlgraphics.java2d.AbstractGraphics2D

      `gc, inPossibleRecursion, textAsShapes`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `DefaultGraphics2D(boolean textAsShapes)` Default constructor
    `DefaultGraphics2D(DefaultGraphics2D g)` This constructor supports the create method

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `copyArea(int x, int y, int width, int height, int dx, int dy)` Copies an area of the component by a distance specified by `dx` and `dy`.
    `java.awt.Graphics` | `create()` Creates a new `Graphics` object that is a copy of this `Graphics` object.
    `void` | `dispose()` Disposes of this graphics context and releases any system resources that it is using.
    `void` | `draw(java.awt.Shape s)` Strokes the outline of a `Shape` using the settings of the current `Graphics2D` context.
    `boolean` | `drawImage(java.awt.Image img, int x, int y, java.awt.image.ImageObserver observer)` Draws as much of the specified image as is currently available.
    `boolean` | `drawImage(java.awt.Image img, int x, int y, int width, int height, java.awt.image.ImageObserver observer)` Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.
    `void` | `drawRenderableImage(java.awt.image.renderable.RenderableImage img, java.awt.geom.AffineTransform xform)` Renders a `RenderableImage`, applying a transform from image space into user space before drawing.
    `void` | `drawRenderedImage(java.awt.image.RenderedImage img, java.awt.geom.AffineTransform xform)` Renders a `RenderedImage`, applying a transform from image space into user space before drawing.
    `void` | `drawString(java.text.AttributedCharacterIterator iterator, float x, float y)` Renders the text of the specified iterator, using the `Graphics2D` context's current `Paint`.
    `void` | `drawString(java.lang.String s, float x, float y)` Renders the text specified by the specified `String`, using the current `Font` and `Paint` attributes in the `Graphics2D` context.
    `void` | `fill(java.awt.Shape s)` Fills the interior of a `Shape` using the settings of the `Graphics2D` context.
    `java.awt.GraphicsConfiguration` | `getDeviceConfiguration()` Returns the device configuration associated with this `Graphics2D`.
    `java.awt.FontMetrics` | `getFontMetrics(java.awt.Font f)` Gets the font metrics for the specified font.
    `void` | `setXORMode(java.awt.Color c1)` Sets the paint mode of this graphics context to alternate between this graphics context's current color and the new specified color.

    - ### Methods inherited from class org.apache.xmlgraphics.java2d.AbstractGraphics2D

      `addRenderingHints, clearRect, clip, clipRect, drawArc, drawGlyphVector, drawImage, drawImage, drawImage, drawImage, drawImage, drawImage, drawLine, drawOval, drawPolygon, drawPolyline, drawRect, drawRoundRect, drawString, drawString, fillArc, fillOval, fillPolygon, fillRect, fillRoundRect, getBackground, getClip, getClipBounds, getColor, getComposite, getFont, getFontRenderContext, getGraphicContext, getPaint, getRenderingHint, getRenderingHints, getStroke, getTransform, hit, rotate, rotate, scale, setBackground, setClip, setClip, setColor, setComposite, setFont, setPaint, setPaintMode, setRenderingHint, setRenderingHints, setStroke, setTransform, shear, transform, translate, translate`

    - ### Methods inherited from class java.awt.Graphics2D

      `draw3DRect, fill3DRect`

    - ### Methods inherited from class java.awt.Graphics

      `create, drawBytes, drawChars, drawPolygon, fillPolygon, finalize, getClipBounds, getClipRect, getFontMetrics, hitClip, toString`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### DefaultGraphics2D

          public DefaultGraphics2D(boolean textAsShapes)

      Default constructor

    - #### DefaultGraphics2D

          public DefaultGraphics2D(DefaultGraphics2D g)

      This constructor supports the create method

  - ### Method Detail

    - #### create

          public java.awt.Graphics create()

      Creates a new `Graphics` object that is a copy of this `Graphics` object.
      Specified by:
        `create` in class `java.awt.Graphics`
      Returns:
        a new graphics context that is a copy of this graphics context.

    - #### drawImage

          public boolean drawImage(java.awt.Image img,
                                   int x,
                                   int y,
                                   java.awt.image.ImageObserver observer)

      Draws as much of the specified image as is currently available. The image is drawn with its top-left corner at (x, y) in this graphics context's coordinate space. Transparent pixels in the image do not affect whatever pixels are already there.

      This method returns immediately in all cases, even if the complete image has not yet been loaded, and it has not been dithered and converted for the current output device.

      If the image has not yet been completely loaded, then `drawImage` returns `false`. As more of the image becomes available, the process that draws the image notifies the specified image observer.

      Specified by:
        `drawImage` in class `java.awt.Graphics`
      Parameters:
        `img` - the specified image to be drawn.
        `x` - the x coordinate.
        `y` - the y coordinate.
        `observer` - object to be notified as more of the image is converted.
      See Also:
        `Image`, `ImageObserver`, `ImageObserver.imageUpdate(java.awt.Image, int, int, int, int, int)`

    - #### drawImage

          public boolean drawImage(java.awt.Image img,
                                   int x,
                                   int y,
                                   int width,
                                   int height,
                                   java.awt.image.ImageObserver observer)

      Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.

      The image is drawn inside the specified rectangle of this graphics context's coordinate space, and is scaled if necessary. Transparent pixels do not affect whatever pixels are already there.

      This method returns immediately in all cases, even if the entire image has not yet been scaled, dithered, and converted for the current output device. If the current output representation is not yet complete, then `drawImage` returns `false`. As more of the image becomes available, the process that draws the image notifies the image observer by calling its `imageUpdate` method.

      A scaled version of an image will not necessarily be available immediately just because an unscaled version of the image has been constructed for this output device. Each size of the image may be cached separately and generated from the original data in a separate image production sequence.

      Specified by:
        `drawImage` in class `java.awt.Graphics`
      Parameters:
        `img` - the specified image to be drawn.
        `x` - the x coordinate.
        `y` - the y coordinate.
        `width` - the width of the rectangle.
        `height` - the height of the rectangle.
        `observer` - object to be notified as more of the image is converted.
      See Also:
        `Image`, `ImageObserver`, `ImageObserver.imageUpdate(java.awt.Image, int, int, int, int, int)`

    - #### dispose

          public void dispose()

      Disposes of this graphics context and releases any system resources that it is using. A `Graphics` object cannot be used after `dispose`has been called.

      When a Java program runs, a large number of `Graphics` objects can be created within a short time frame. Although the finalization process of the garbage collector also disposes of the same system resources, it is preferable to manually free the associated resources by calling this method rather than to rely on a finalization process which may not run to completion for a long period of time.

      Graphics objects which are provided as arguments to the `paint` and `update` methods of components are automatically released by the system when those methods return. For efficiency, programmers should call `dispose` when finished using a `Graphics` object only if it was created directly from a component or another `Graphics` object.

      Specified by:
        `dispose` in class `java.awt.Graphics`
      See Also:
        `Graphics.finalize()`, `Component.paint(java.awt.Graphics)`, `Component.update(java.awt.Graphics)`, `Component.getGraphics()`, `Graphics.create()`

    - #### draw

          public void draw(java.awt.Shape s)

      Strokes the outline of a `Shape` using the settings of the current `Graphics2D` context. The rendering attributes applied include the `Clip`, `Transform`, `Paint`, `Composite` and `Stroke` attributes.
      Specified by:
        `draw` in class `java.awt.Graphics2D`
      Parameters:
        `s` - the `Shape` to be rendered
      See Also:
        `AbstractGraphics2D.setStroke(java.awt.Stroke)`, `AbstractGraphics2D.setPaint(java.awt.Paint)`, `Graphics.setColor(java.awt.Color)`, `AbstractGraphics2D.setTransform(AffineTransform)`, `AbstractGraphics2D.setClip(Shape)`, `AbstractGraphics2D.setComposite(java.awt.Composite)`

    - #### drawRenderedImage

          public void drawRenderedImage(java.awt.image.RenderedImage img,
                                        java.awt.geom.AffineTransform xform)

      Renders a `RenderedImage`, applying a transform from image space into user space before drawing. The transformation from user space into device space is done with the current `Transform` in the `Graphics2D`. The specified transformation is applied to the image before the transform attribute in the `Graphics2D` context is applied. The rendering attributes applied include the `Clip`, `Transform`, and `Composite` attributes. Note that no rendering is done if the specified transform is noninvertible.
      Specified by:
        `drawRenderedImage` in class `java.awt.Graphics2D`
      Parameters:
        `img` - the image to be rendered
        `xform` - the transformation from image space into user space
      See Also:
        `AbstractGraphics2D.setTransform(AffineTransform)`, `AbstractGraphics2D.setComposite(java.awt.Composite)`, `AbstractGraphics2D.setClip(Shape)`

    - #### drawRenderableImage

          public void drawRenderableImage(java.awt.image.renderable.RenderableImage img,
                                          java.awt.geom.AffineTransform xform)

      Renders a `RenderableImage`, applying a transform from image space into user space before drawing. The transformation from user space into device space is done with the current `Transform` in the `Graphics2D`. The specified transformation is applied to the image before the transform attribute in the `Graphics2D` context is applied. The rendering attributes applied include the `Clip`, `Transform`, and `Composite` attributes. Note that no rendering is done if the specified transform is noninvertible.

      Rendering hints set on the `Graphics2D` object might be used in rendering the `RenderableImage`. If explicit control is required over specific hints recognized by a specific `RenderableImage`, or if knowledge of which hints are used is required, then a `RenderedImage` should be obtained directly from the `RenderableImage` and rendered using `drawRenderedImage`.

      Specified by:
        `drawRenderableImage` in class `java.awt.Graphics2D`
      Parameters:
        `img` - the image to be rendered
        `xform` - the transformation from image space into user space
      See Also:
        `AbstractGraphics2D.setTransform(AffineTransform)`, `AbstractGraphics2D.setComposite(java.awt.Composite)`, `AbstractGraphics2D.setClip(Shape)`, `drawRenderedImage(java.awt.image.RenderedImage, java.awt.geom.AffineTransform)`

    - #### drawString

          public void drawString(java.lang.String s,
                                 float x,
                                 float y)

      Renders the text specified by the specified `String`, using the current `Font` and `Paint` attributes in the `Graphics2D` context. The baseline of the first character is at position (x, y) in the User Space. The rendering attributes applied include the `Clip`, `Transform`, `Paint`, `Font` and `Composite` attributes. For characters in script systems such as Hebrew and Arabic, the glyphs can be rendered from right to left, in which case the coordinate supplied is the location of the leftmost character on the baseline.
      Specified by:
        `drawString` in class `java.awt.Graphics2D`
      Parameters:
        `s` - the `String` to be rendered
        `x` - the x coordinate where the `String` should be rendered
        `y` - the y coordinate where the `String` should be rendered
      See Also:
        `AbstractGraphics2D.setPaint(java.awt.Paint)`, `Graphics.setColor(java.awt.Color)`, `Graphics.setFont(java.awt.Font)`, `AbstractGraphics2D.setTransform(AffineTransform)`, `AbstractGraphics2D.setComposite(java.awt.Composite)`, `AbstractGraphics2D.setClip(Shape)`

    - #### drawString

          public void drawString(java.text.AttributedCharacterIterator iterator,
                                 float x,
                                 float y)

      Renders the text of the specified iterator, using the `Graphics2D` context's current `Paint`. The iterator must specify a font for each character. The baseline of the first character is at position (x, y) in the User Space. The rendering attributes applied include the `Clip`, `Transform`, `Paint`, and `Composite` attributes. For characters in script systems such as Hebrew and Arabic, the glyphs can be rendered from right to left, in which case the coordinate supplied is the location of the leftmost character on the baseline.
      Overrides:
        `drawString` in class `AbstractGraphics2D`
      Parameters:
        `iterator` - the iterator whose text is to be rendered
        `x` - the x coordinate where the iterator's text is to be rendered
        `y` - the y coordinate where the iterator's text is to be rendered
      See Also:
        `AbstractGraphics2D.setPaint(java.awt.Paint)`, `Graphics.setColor(java.awt.Color)`, `AbstractGraphics2D.setTransform(AffineTransform)`, `AbstractGraphics2D.setComposite(java.awt.Composite)`, `AbstractGraphics2D.setClip(Shape)`

    - #### fill

          public void fill(java.awt.Shape s)

      Fills the interior of a `Shape` using the settings of the `Graphics2D` context. The rendering attributes applied include the `Clip`, `Transform`, `Paint`, and `Composite`.
      Specified by:
        `fill` in class `java.awt.Graphics2D`
      Parameters:
        `s` - the `Shape` to be filled
      See Also:
        `AbstractGraphics2D.setPaint(java.awt.Paint)`, `Graphics.setColor(java.awt.Color)`, `AbstractGraphics2D.setTransform(AffineTransform)`, `AbstractGraphics2D.setComposite(java.awt.Composite)`, `AbstractGraphics2D.setClip(Shape)`

    - #### getDeviceConfiguration

          public java.awt.GraphicsConfiguration getDeviceConfiguration()

      Returns the device configuration associated with this `Graphics2D`.
      Specified by:
        `getDeviceConfiguration` in class `java.awt.Graphics2D`

    - #### getFontMetrics

          public java.awt.FontMetrics getFontMetrics(java.awt.Font f)

      Gets the font metrics for the specified font.
      Specified by:
        `getFontMetrics` in class `java.awt.Graphics`
      Parameters:
        `f` - the specified font
      Returns:
        the font metrics for the specified font.
      See Also:
        `Graphics.getFont()`, `FontMetrics`, `Graphics.getFontMetrics()`

    - #### setXORMode

          public void setXORMode(java.awt.Color c1)

      Sets the paint mode of this graphics context to alternate between this graphics context's current color and the new specified color. This specifies that logical pixel operations are performed in the XOR mode, which alternates pixels between the current color and a specified XOR color.

      When drawing operations are performed, pixels which are the current color are changed to the specified color, and vice versa.

      Pixels that are of colors other than those two colors are changed in an unpredictable but reversible manner; if the same figure is drawn twice, then all pixels are restored to their original values.

      Specified by:
        `setXORMode` in class `java.awt.Graphics`
      Parameters:
        `c1` - the XOR alternation color

    - #### copyArea

          public void copyArea(int x,
                               int y,
                               int width,
                               int height,
                               int dx,
                               int dy)

      Copies an area of the component by a distance specified by `dx` and `dy`. From the point specified by `x` and `y`, this method copies downwards and to the right. To copy an area of the component to the left or upwards, specify a negative value for `dx` or `dy`. If a portion of the source rectangle lies outside the bounds of the component, or is obscured by another window or component, `copyArea` will be unable to copy the associated pixels. The area that is omitted can be refreshed by calling the component's `paint` method.
      Specified by:
        `copyArea` in class `java.awt.Graphics`
      Parameters:
        `x` - the x coordinate of the source rectangle.
        `y` - the y coordinate of the source rectangle.
        `width` - the width of the source rectangle.
        `height` - the height of the source rectangle.
        `dx` - the horizontal distance to copy the pixels.
        `dy` - the vertical distance to copy the pixels.

Copyright 1999-2025 The Apache Software Foundation. All Rights Reserved.