gnu.awt.j2d

Class Graphics2DImpl

Implemented Interfaces:
Cloneable

public class Graphics2DImpl
extends Graphics2D
implements Cloneable

Delegates almost all work to a state object, that allows us to hot-swap rendering strategies based on state changes inflicted on this Graphics object. This class keeps track of properties that are not affected by the state, (such as clip shape, foreground/background color, font, etc.).

The far front-end of the rendering pipeline consists of the Graphics2D API. In the far back-end, lies the native graphics libraries. In most cases the native graphics libraries only have direct support for a subset of the properties of Graphics2D. To make up missing features in the native graphics libraries, the pipeline between the front-end and the back-end need to translate drawing request to primitive operations that are supported by the back-end. E.g. for X11, drawing a straight line will translate to an XDrawLine, drawing a bezier curve will trigger flattening of the curve and will result in a call to XDrawLines.

This is the basic strategy for the rendering pipeline: Whenever a graphics property change occurs, that causes the current pipeline to be insufficient, amend or replace parts of the pipeline so that the pipeline will once again be able to translate requests to the set of primitives supported by the native graphics library.

Most graphics libraries share common subsets of functionality. To be able to reuse pieces of the rendering pipeline for several backends, we define interfaces that describe subsets of characteristics supported by the backends. A wrapper for the native library can implement several interfaces to describe its range of functionality.

Typically, most painting is done with a graphics object with simple properties. Unless one is using a complex Look & Feel, the painting of Swing components will never require affine transforms, alpha blending, non-rectangular clipping, etc. When graphics objects are created, they start off in a state where all the properties are simple. Most graphics objects experience only trivial property changes, and never leave this simple state. It is therefore wise to ensure that the rendering pipeline for this initial state is lean and as much as possible plugs directly into the backend.

The initial state for graphics object of most raster displays would call for two levels of indirection:

 Graphics2D object ---> IntegerGraphicsState ---> DirectRasterGraphics
 

Constructor Summary

Graphics2DImpl(GraphicsConfiguration config)

Method Summary

void
addRenderingHints(Map<K,V> hints)
void
clearRect(int x, int y, int width, int height)
void
clip(Shape shape)
Sets the clip region to the intersection of the current clipping region and s.
void
clipRect(int x, int y, int width, int height)
Object
clone()
This method may be called to create a new copy of the Object.
void
copyArea(int x, int y, int width, int height, int dx, int dy)
Graphics
create()
void
dispose()
void
draw(Shape shape)
Draws an outline around a shape using the current stroke and paint.
void
drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
void
drawGlyphVector(GlyphVector g, float x, float y)
Draws a glyph vector at the specified location.
boolean
drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)
boolean
drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
boolean
drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)
boolean
drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
boolean
drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)
boolean
drawImage(Image image, int x, int y, ImageObserver observer)
boolean
drawImage(Image image, AffineTransform xform, ImageObserver obs)
void
drawImage(BufferedImage image, BufferedImageOp op, int x, int y)
void
drawLine(int x1, int y1, int x2, int y2)
void
drawOval(int x, int y, int width, int height)
void
drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
void
drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
void
drawRenderableImage(RenderableImage image, AffineTransform xform)
void
drawRenderedImage(RenderedImage image, AffineTransform xform)
void
drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
void
drawString(String text, float x, float y)
Draws a string at the specified location, using the current font.
void
drawString(String text, int x, int y)
Draws a string at the specified location, using the current font.
void
drawString(AttributedCharacterIterator iterator, float x, float y)
Draws an attributed string at the specified location.
void
drawString(AttributedCharacterIterator iterator, int x, int y)
Draws an attributed string at the specified location.
void
fill(Shape shape)
Fills the interior of the specified shape using the current paint.
void
fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
void
fillOval(int x, int y, int width, int height)
void
fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
void
fillRect(int x, int y, int width, int height)
void
fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
Color
getBackground()
Returns the color used by the Graphics.clearRect(int,int,int,int) method.
Shape
getClip()
Rectangle
getClipBounds()
Color
getColor()
Composite
getComposite()
Returns the current compositing rule.
GraphicsConfiguration
getDeviceConfiguration()
Font
getFont()
FontMetrics
getFontMetrics(Font font)
FontRenderContext
getFontRenderContext()
Returns the font render context.
Paint
getPaint()
Returns the current paint.
Object
getRenderingHint(RenderingHints.Key hintKey)
Returns the current value of a rendering hint.
RenderingHints
getRenderingHints()
Returns the current rendering hints.
Stroke
getStroke()
Returns the current stroke.
AffineTransform
getTransform()
Returns the current transform.
boolean
hit(Rectangle rect, Shape text, boolean onStroke)
void
rotate(double theta)
void
rotate(double theta, double x, double y)
void
scale(double scaleX, double scaleY)
void
setBackground(Color color)
Sets the background color (used by the Graphics.clearRect(int,int,int,int) method).
void
setClip(int x, int y, int width, int height)
void
setClip(Shape clip)
void
setColor(Color color)
void
setComposite(Composite comp)
Sets the current compositing rule.
void
setFont(Font font)
void
setPaint(Paint paint)
Sets the paint to be used for subsequent drawing operations.
void
setPaintMode()
void
setRenderingHint(RenderingHints.Key hintKey, Object hintValue)
Adds or updates a hint in the current rendering hints table.
void
setRenderingHints(Map<K,V> hints)
void
setState(AbstractGraphicsState state)
void
setStroke(Stroke stroke)
Sets the stroke to be used for subsequent drawing operations.
void
setTransform(AffineTransform Tx)
Sets the current transform.
void
setXORMode(Color altColor)
void
shear(double shearX, double shearY)
void
transform(AffineTransform Tx)
Sets the current transform to a concatenation of transform and the existing transform.
void
translate(double tx, double ty)
void
translate(int x, int y)

Methods inherited from class java.awt.Graphics2D

addRenderingHints, clip, draw, draw3DRect, drawGlyphVector, drawImage, drawImage, drawRenderableImage, drawRenderedImage, drawString, drawString, drawString, drawString, fill, fill3DRect, getBackground, getComposite, getDeviceConfiguration, getFontRenderContext, getPaint, getRenderingHint, getRenderingHints, getStroke, getTransform, hit, rotate, rotate, scale, setBackground, setComposite, setPaint, setRenderingHint, setRenderingHints, setStroke, setTransform, shear, transform, translate, translate

Methods inherited from class java.awt.Graphics

clearRect, clipRect, copyArea, create, create, dispose, draw3DRect, drawArc, drawBytes, drawChars, drawImage, drawImage, drawImage, drawImage, drawImage, drawImage, drawLine, drawOval, drawPolygon, drawPolygon, drawPolyline, drawRect, drawRoundRect, drawString, drawString, fill3DRect, fillArc, fillOval, fillPolygon, fillPolygon, fillRect, fillRoundRect, finalize, getClip, getClipBounds, getClipBounds, getClipRect, getColor, getFont, getFontMetrics, getFontMetrics, hitClip, setClip, setClip, setColor, setFont, setPaintMode, setXORMode, toString, translate

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

Graphics2DImpl

public Graphics2DImpl(GraphicsConfiguration config)

Method Details

addRenderingHints

public void addRenderingHints(Map<K,V> hints)

clearRect

public void clearRect(int x,
                      int y,
                      int width,
                      int height)
Overrides:
clearRect in interface Graphics

clip

public void clip(Shape shape)
Sets the clip region to the intersection of the current clipping region and s.
Overrides:
clip in interface Graphics2D
Parameters:

clipRect

public void clipRect(int x,
                     int y,
                     int width,
                     int height)
Overrides:
clipRect in interface Graphics

clone

public Object clone()
This method may be called to create a new copy of the Object. The typical behavior is as follows:
  • o == o.clone() is false
  • o.getClass() == o.clone().getClass() is true
  • o.equals(o) is true

However, these are not strict requirements, and may be violated if necessary. Of the three requirements, the last is the most commonly violated, particularly if the subclass does not override Object.equals(Object).

If the Object you call clone() on does not implement Cloneable (which is a placeholder interface), then a CloneNotSupportedException is thrown. Notice that Object does not implement Cloneable; this method exists as a convenience for subclasses that do.

Object's implementation of clone allocates space for the new Object using the correct class, without calling any constructors, and then fills in all of the new field values with the old field values. Thus, it is a shallow copy. However, subclasses are permitted to make a deep copy.

All array types implement Cloneable, and override this method as follows (it should never fail):

 public Object clone()
 {
   try
     {
       super.clone();
     }
   catch (CloneNotSupportedException e)
     {
       throw new InternalError(e.getMessage());
     }
 }
 
Overrides:
clone in interface Object
Returns:
a copy of the Object
See Also:
Cloneable

copyArea

public void copyArea(int x,
                     int y,
                     int width,
                     int height,
                     int dx,
                     int dy)
Overrides:
copyArea in interface Graphics

create

public Graphics create()
Overrides:
create in interface Graphics

dispose

public void dispose()
Overrides:
dispose in interface Graphics

draw

public void draw(Shape shape)
Draws an outline around a shape using the current stroke and paint.
Overrides:
draw in interface Graphics2D
Parameters:
shape - the shape (null not permitted).

drawArc

public void drawArc(int x,
                    int y,
                    int width,
                    int height,
                    int startAngle,
                    int arcAngle)
Overrides:
drawArc in interface Graphics

drawGlyphVector

public void drawGlyphVector(GlyphVector g,
                            float x,
                            float y)
Draws a glyph vector at the specified location.
Overrides:
drawGlyphVector in interface Graphics2D
Parameters:
g - the glyph vector.
x - the x-coordinate.
y - the y-coordinate.

drawImage

public boolean drawImage(Image img,
                         int dx1,
                         int dy1,
                         int dx2,
                         int dy2,
                         int sx1,
                         int sy1,
                         int sx2,
                         int sy2,
                         Color bgcolor,
                         ImageObserver observer)
Overrides:
drawImage in interface Graphics

drawImage

public boolean drawImage(Image img,
                         int dx1,
                         int dy1,
                         int dx2,
                         int dy2,
                         int sx1,
                         int sy1,
                         int sx2,
                         int sy2,
                         ImageObserver observer)
Overrides:
drawImage in interface Graphics

drawImage

public boolean drawImage(Image img,
                         int x,
                         int y,
                         int width,
                         int height,
                         Color bgcolor,
                         ImageObserver observer)
Overrides:
drawImage in interface Graphics

drawImage

public boolean drawImage(Image img,
                         int x,
                         int y,
                         int width,
                         int height,
                         ImageObserver observer)
Overrides:
drawImage in interface Graphics

drawImage

public boolean drawImage(Image img,
                         int x,
                         int y,
                         Color bgcolor,
                         ImageObserver observer)
Overrides:
drawImage in interface Graphics

drawImage

public boolean drawImage(Image image,
                         int x,
                         int y,
                         ImageObserver observer)
Overrides:
drawImage in interface Graphics

drawImage

public boolean drawImage(Image image,
                         AffineTransform xform,
                         ImageObserver obs)
Overrides:
drawImage in interface Graphics2D

drawImage

public void drawImage(BufferedImage image,
                      BufferedImageOp op,
                      int x,
                      int y)
Overrides:
drawImage in interface Graphics2D

drawLine

public void drawLine(int x1,
                     int y1,
                     int x2,
                     int y2)
Overrides:
drawLine in interface Graphics

drawOval

public void drawOval(int x,
                     int y,
                     int width,
                     int height)
Overrides:
drawOval in interface Graphics

drawPolygon

public void drawPolygon(int[] xPoints,
                        int[] yPoints,
                        int nPoints)

drawPolyline

public void drawPolyline(int[] xPoints,
                         int[] yPoints,
                         int nPoints)

drawRenderableImage

public void drawRenderableImage(RenderableImage image,
                                AffineTransform xform)
Overrides:
drawRenderableImage in interface Graphics2D

drawRenderedImage

public void drawRenderedImage(RenderedImage image,
                              AffineTransform xform)
Overrides:
drawRenderedImage in interface Graphics2D

drawRoundRect

public void drawRoundRect(int x,
                          int y,
                          int width,
                          int height,
                          int arcWidth,
                          int arcHeight)
Overrides:
drawRoundRect in interface Graphics

drawString

public void drawString(String text,
                       float x,
                       float y)
Draws a string at the specified location, using the current font.
Overrides:
drawString in interface Graphics2D
Parameters:
text - the string to draw.
x - the x-coordinate.
y - the y-coordinate.

drawString

public void drawString(String text,
                       int x,
                       int y)
Draws a string at the specified location, using the current font.
Overrides:
drawString in interface Graphics2D
Parameters:
text - the string to draw.
x - the x-coordinate.
y - the y-coordinate.

drawString

public void drawString(AttributedCharacterIterator iterator,
                       float x,
                       float y)
Draws an attributed string at the specified location.
Overrides:
drawString in interface Graphics2D
Parameters:
iterator - the source of the attributed text.
x - the x-coordinate.
y - the y-coordinate.

drawString

public void drawString(AttributedCharacterIterator iterator,
                       int x,
                       int y)
Draws an attributed string at the specified location.
Overrides:
drawString in interface Graphics2D
Parameters:
iterator - the source of the attributed text.
x - the x-coordinate.
y - the y-coordinate.

fill

public void fill(Shape shape)
Fills the interior of the specified shape using the current paint.
Overrides:
fill in interface Graphics2D
Parameters:
shape - the shape to fill (null not permitted).

fillArc

public void fillArc(int x,
                    int y,
                    int width,
                    int height,
                    int startAngle,
                    int arcAngle)
Overrides:
fillArc in interface Graphics

fillOval

public void fillOval(int x,
                     int y,
                     int width,
                     int height)
Overrides:
fillOval in interface Graphics

fillPolygon

public void fillPolygon(int[] xPoints,
                        int[] yPoints,
                        int nPoints)

fillRect

public void fillRect(int x,
                     int y,
                     int width,
                     int height)
Overrides:
fillRect in interface Graphics

fillRoundRect

public void fillRoundRect(int x,
                          int y,
                          int width,
                          int height,
                          int arcWidth,
                          int arcHeight)
Overrides:
fillRoundRect in interface Graphics

getBackground

public Color getBackground()
Returns the color used by the Graphics.clearRect(int,int,int,int) method.
Overrides:
getBackground in interface Graphics2D
Returns:
The background color.

getClip

public Shape getClip()
Overrides:
getClip in interface Graphics

getClipBounds

public Rectangle getClipBounds()
Overrides:
getClipBounds in interface Graphics

getColor

public Color getColor()
Overrides:
getColor in interface Graphics

getComposite

public Composite getComposite()
Returns the current compositing rule.
Overrides:
getComposite in interface Graphics2D
Returns:
The current compositing rule.

getDeviceConfiguration

public GraphicsConfiguration getDeviceConfiguration()
Overrides:
getDeviceConfiguration in interface Graphics2D

getFont

public Font getFont()
Overrides:
getFont in interface Graphics

getFontMetrics

public FontMetrics getFontMetrics(Font font)
Overrides:
getFontMetrics in interface Graphics

getFontRenderContext

public FontRenderContext getFontRenderContext()
Returns the font render context.
Overrides:
getFontRenderContext in interface Graphics2D
Returns:
The font render context.

getPaint

public Paint getPaint()
Returns the current paint.
Overrides:
getPaint in interface Graphics2D
Returns:
The current paint.

getRenderingHint

public Object getRenderingHint(RenderingHints.Key hintKey)
Returns the current value of a rendering hint.
Overrides:
getRenderingHint in interface Graphics2D
Parameters:
hintKey - the key for the hint.
Returns:
The value for the specified hint.

getRenderingHints

public RenderingHints getRenderingHints()
Returns the current rendering hints.
Overrides:
getRenderingHints in interface Graphics2D
Returns:
The current rendering hints.

getStroke

public Stroke getStroke()
Returns the current stroke.
Overrides:
getStroke in interface Graphics2D
Returns:
The current stroke.

getTransform

public AffineTransform getTransform()
Returns the current transform.
Overrides:
getTransform in interface Graphics2D
Returns:
The current transform.

hit

public boolean hit(Rectangle rect,
                   Shape text,
                   boolean onStroke)
Overrides:
hit in interface Graphics2D

rotate

public void rotate(double theta)
Overrides:
rotate in interface Graphics2D

rotate

public void rotate(double theta,
                   double x,
                   double y)
Overrides:
rotate in interface Graphics2D

scale

public void scale(double scaleX,
                  double scaleY)
Overrides:
scale in interface Graphics2D

setBackground

public void setBackground(Color color)
Sets the background color (used by the Graphics.clearRect(int,int,int,int) method).
Overrides:
setBackground in interface Graphics2D
Parameters:
color - the color.

setClip

public void setClip(int x,
                    int y,
                    int width,
                    int height)
Overrides:
setClip in interface Graphics

setClip

public void setClip(Shape clip)
Overrides:
setClip in interface Graphics

setColor

public void setColor(Color color)
Overrides:
setColor in interface Graphics

setComposite

public void setComposite(Composite comp)
Sets the current compositing rule.
Overrides:
setComposite in interface Graphics2D
Parameters:
comp - the composite.

setFont

public void setFont(Font font)
Overrides:
setFont in interface Graphics

setPaint

public void setPaint(Paint paint)
Sets the paint to be used for subsequent drawing operations.
Overrides:
setPaint in interface Graphics2D
Parameters:
paint - the paint (null not permitted).

setPaintMode

public void setPaintMode()
Overrides:
setPaintMode in interface Graphics

setRenderingHint

public void setRenderingHint(RenderingHints.Key hintKey,
                             Object hintValue)
Adds or updates a hint in the current rendering hints table.
Overrides:
setRenderingHint in interface Graphics2D
Parameters:
hintKey - the hint key.
hintValue - the hint value.

setRenderingHints

public void setRenderingHints(Map<K,V> hints)

setState

public void setState(AbstractGraphicsState state)

setStroke

public void setStroke(Stroke stroke)
Sets the stroke to be used for subsequent drawing operations.
Overrides:
setStroke in interface Graphics2D
Parameters:
stroke - the stroke (null not permitted).

setTransform

public void setTransform(AffineTransform Tx)
Sets the current transform. If the caller specifies a null transform, this method should set the current transform to the identity transform.
Overrides:
setTransform in interface Graphics2D
Parameters:

setXORMode

public void setXORMode(Color altColor)
Overrides:
setXORMode in interface Graphics

shear

public void shear(double shearX,
                  double shearY)
Overrides:
shear in interface Graphics2D

transform

public void transform(AffineTransform Tx)
Sets the current transform to a concatenation of transform and the existing transform.
Overrides:
transform in interface Graphics2D
Parameters:

translate

public void translate(double tx,
                      double ty)
Overrides:
translate in interface Graphics2D

translate

public void translate(int x,
                      int y)
Overrides:
translate in interface Graphics2D

Copyright (C) 2000, 2002, 2003 Free Software Foundation This file is part of libgcj. This software is copyrighted work licensed under the terms of the Libgcj License. Please consult the file "LIBGCJ_LICENSE" for details.