APIdia Launch Partner
Top Description Constructors Methods
java.awt

public abstract Class Graphics

extends Object
Class Inheritance
Known Direct Subclasses
java.awt.Graphics2D, javax.swing.DebugGraphics, sun.print.ProxyGraphics
Imports
java.awt.image.ImageObserver, java.text.AttributedCharacterIterator

The Graphics class is the abstract base class for all graphics contexts that allow an application to draw onto components that are realized on various devices, as well as onto off-screen images.

A Graphics object encapsulates state information needed for the basic rendering operations that Java supports. This state information includes the following properties:

Coordinates are infinitely thin and lie between the pixels of the output device. Operations that draw the outline of a figure operate by traversing an infinitely thin path between pixels with a pixel-sized pen that hangs down and to the right of the anchor point on the path. Operations that fill a figure operate by filling the interior of that infinitely thin path. Operations that render horizontal text render the ascending portion of character glyphs entirely above the baseline coordinate.

The graphics pen hangs down and to the right from the path it traverses. This has the following implications:

All coordinates that appear as arguments to the methods of this Graphics object are considered relative to the translation origin of this Graphics object prior to the invocation of the method.

All rendering operations modify only pixels which lie within the area bounded by the current clip, which is specified by a Shape in user space and is controlled by the program using the Graphics object. This user clip is transformed into device space and combined with the device clip, which is defined by the visibility of windows and device extents. The combination of the user clip and device clip defines the composite clip, which determines the final clipping region. The user clip cannot be modified by the rendering system to reflect the resulting composite clip. The user clip can only be changed through the setClip or clipRect methods. All drawing or writing is done in the current color, using the current paint mode, and in the current font.

Authors
Sami Shaio, Arthur van Hoff
Since
1.0
See Also
java.awt.Component, java.awt.Graphics#clipRect(int, int, int, int), java.awt.Graphics#setColor(java.awt.Color), java.awt.Graphics#setPaintMode(), java.awt.Graphics#setXORMode(java.awt.Color), java.awt.Graphics#setFont(java.awt.Font)

Constructor Summary

AccessConstructor and Description
protected
Graphics()

Constructs a new Graphics object.

Method Summary

Modifier and TypeMethod and Description
public abstract void
clearRect(int
the x coordinate of the rectangle to clear.
x
,
int
the y coordinate of the rectangle to clear.
y
,
int
the width of the rectangle to clear.
width
,
int
the height of the rectangle to clear.
height
)

Clears the specified rectangle by filling it with the background color of the current drawing surface.

public abstract void
clipRect(int
the x coordinate of the rectangle to intersect the clip with
x
,
int
the y coordinate of the rectangle to intersect the clip with
y
,
int
the width of the rectangle to intersect the clip with
width
,
int
the height of the rectangle to intersect the clip with
height
)

Intersects the current clip with the specified rectangle.

public abstract void
copyArea(int
the x coordinate of the source rectangle.
x
,
int
the y coordinate of the source rectangle.
y
,
int
the width of the source rectangle.
width
,
int
the height of the source rectangle.
height
,
int
the horizontal distance to copy the pixels.
dx
,
int
the vertical distance to copy the pixels.
dy
)

Copies an area of the component by a distance specified by dx and dy.

public abstract Graphics

Returns:

a new graphics context that is a copy of this graphics context.
create
()

Creates a new Graphics object that is a copy of this Graphics object.

public Graphics

Returns:

a new graphics context.
create
(int
the x coordinate.
x
,
int
the y coordinate.
y
,
int
the width of the clipping rectangle.
width
,
int
the height of the clipping rectangle.
height
)

Creates a new Graphics object based on this Graphics object, but with a new translation and clip area.

public abstract void
dispose()

Disposes of this graphics context and releases any system resources that it is using.

public void
draw3DRect(int
the x coordinate of the rectangle to be drawn.
x
,
int
the y coordinate of the rectangle to be drawn.
y
,
int
the width of the rectangle to be drawn.
width
,
int
the height of the rectangle to be drawn.
height
,
boolean
a boolean that determines whether the rectangle appears to be raised above the surface or sunk into the surface.
raised
)

Draws a 3-D highlighted outline of the specified rectangle.

public abstract void
drawArc(int
the x coordinate of the upper-left corner of the arc to be drawn.
x
,
int
the y coordinate of the upper-left corner of the arc to be drawn.
y
,
int
the width of the arc to be drawn.
width
,
int
the height of the arc to be drawn.
height
,
int
the beginning angle.
startAngle
,
int
the angular extent of the arc, relative to the start angle.
arcAngle
)

Draws the outline of a circular or elliptical arc covering the specified rectangle.

public void
drawBytes(byte[]
the data to be drawn
data
,
int
the start offset in the data
offset
,
int
the number of bytes that are drawn
length
,
int
the x coordinate of the baseline of the text
x
,
int
the y coordinate of the baseline of the text
y
)

Draws the text given by the specified byte array, using this graphics context's current font and color.

public void
drawChars(char[]
the array of characters to be drawn
data
,
int
the start offset in the data
offset
,
int
the number of characters to be drawn
length
,
int
the x coordinate of the baseline of the text
x
,
int
the y coordinate of the baseline of the text
y
)

Draws the text given by the specified character array, using this graphics context's current font and color.

public abstract boolean

Returns:

false if the image pixels are still changing; true otherwise.
drawImage
(Image
the specified image to be drawn. This method does nothing if img is null.
img
,
int
the x coordinate.
x
,
int
the y coordinate.
y
,
ImageObserver
object to be notified as more of the image is converted.
observer
)

Draws as much of the specified image as is currently available.

public abstract boolean

Returns:

false if the image pixels are still changing; true otherwise.
drawImage
(Image
the specified image to be drawn. This method does nothing if img is null.
img
,
int
the x coordinate.
x
,
int
the y coordinate.
y
,
int
the width of the rectangle.
width
,
int
the height of the rectangle.
height
,
ImageObserver
object to be notified as more of the image is converted.
observer
)

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

public abstract boolean

Returns:

false if the image pixels are still changing; true otherwise.
drawImage
(Image
the specified image to be drawn. This method does nothing if img is null.
img
,
int
the x coordinate.
x
,
int
the y coordinate.
y
,
Color
the background color to paint under the non-opaque portions of the image.
bgcolor
,
ImageObserver
object to be notified as more of the image is converted.
observer
)

Draws as much of the specified image as is currently available.

public abstract boolean

Returns:

false if the image pixels are still changing; true otherwise.
drawImage
(Image
the specified image to be drawn. This method does nothing if img is null.
img
,
int
the x coordinate.
x
,
int
the y coordinate.
y
,
int
the width of the rectangle.
width
,
int
the height of the rectangle.
height
,
Color
the background color to paint under the non-opaque portions of the image.
bgcolor
,
ImageObserver
object to be notified as more of the image is converted.
observer
)

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

public abstract boolean

Returns:

false if the image pixels are still changing; true otherwise.
drawImage
(Image
the specified image to be drawn. This method does nothing if img is null.
img
,
int
the x coordinate of the first corner of the destination rectangle.
dx1
,
int
the y coordinate of the first corner of the destination rectangle.
dy1
,
int
the x coordinate of the second corner of the destination rectangle.
dx2
,
int
the y coordinate of the second corner of the destination rectangle.
dy2
,
int
the x coordinate of the first corner of the source rectangle.
sx1
,
int
the y coordinate of the first corner of the source rectangle.
sy1
,
int
the x coordinate of the second corner of the source rectangle.
sx2
,
int
the y coordinate of the second corner of the source rectangle.
sy2
,
ImageObserver
object to be notified as more of the image is scaled and converted.
observer
)

Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface.

public abstract boolean

Returns:

false if the image pixels are still changing; true otherwise.
drawImage
(Image
the specified image to be drawn. This method does nothing if img is null.
img
,
int
the x coordinate of the first corner of the destination rectangle.
dx1
,
int
the y coordinate of the first corner of the destination rectangle.
dy1
,
int
the x coordinate of the second corner of the destination rectangle.
dx2
,
int
the y coordinate of the second corner of the destination rectangle.
dy2
,
int
the x coordinate of the first corner of the source rectangle.
sx1
,
int
the y coordinate of the first corner of the source rectangle.
sy1
,
int
the x coordinate of the second corner of the source rectangle.
sx2
,
int
the y coordinate of the second corner of the source rectangle.
sy2
,
Color
the background color to paint under the non-opaque portions of the image.
bgcolor
,
ImageObserver
object to be notified as more of the image is scaled and converted.
observer
)

Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface.

public abstract void
drawLine(int
the first point's x coordinate.
x1
,
int
the first point's y coordinate.
y1
,
int
the second point's x coordinate.
x2
,
int
the second point's y coordinate.
y2
)

Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system.

public abstract void
drawOval(int
the x coordinate of the upper left corner of the oval to be drawn.
x
,
int
the y coordinate of the upper left corner of the oval to be drawn.
y
,
int
the width of the oval to be drawn.
width
,
int
the height of the oval to be drawn.
height
)

Draws the outline of an oval.

public abstract void
drawPolygon(int[]
an array of x coordinates.
xPoints
,
int[]
an array of y coordinates.
yPoints
,
int
the total number of points.
nPoints
)

Draws a closed polygon defined by arrays of x and y coordinates.

public void
drawPolygon(Polygon
the polygon to draw.
p
)

Draws the outline of a polygon defined by the specified Polygon object.

public abstract void
drawPolyline(int[]
an array of x points
xPoints
,
int[]
an array of y points
yPoints
,
int
the total number of points
nPoints
)

Draws a sequence of connected lines defined by arrays of x and y coordinates.

public void
drawRect(int
the x coordinate of the rectangle to be drawn.
x
,
int
the y coordinate of the rectangle to be drawn.
y
,
int
the width of the rectangle to be drawn.
width
,
int
the height of the rectangle to be drawn.
height
)

Draws the outline of the specified rectangle.

public abstract void
drawRoundRect(int
the x coordinate of the rectangle to be drawn.
x
,
int
the y coordinate of the rectangle to be drawn.
y
,
int
the width of the rectangle to be drawn.
width
,
int
the height of the rectangle to be drawn.
height
,
int
the horizontal diameter of the arc at the four corners.
arcWidth
,
int
the vertical diameter of the arc at the four corners.
arcHeight
)

Draws an outlined round-cornered rectangle using this graphics context's current color.

public abstract void
drawString(String
the string to be drawn.
str
,
int
the x coordinate.
x
,
int
the y coordinate.
y
)

Draws the text given by the specified string, using this graphics context's current font and color.

public abstract void
drawString(AttributedCharacterIterator
the iterator whose text is to be drawn
iterator
,
int
the x coordinate.
x
,
int
the y coordinate.
y
)

Renders the text of the specified iterator applying its attributes in accordance with the specification of the TextAttribute class.

public void
fill3DRect(int
the x coordinate of the rectangle to be filled.
x
,
int
the y coordinate of the rectangle to be filled.
y
,
int
the width of the rectangle to be filled.
width
,
int
the height of the rectangle to be filled.
height
,
boolean
a boolean value that determines whether the rectangle appears to be raised above the surface or etched into the surface.
raised
)

Paints a 3-D highlighted rectangle filled with the current color.

public abstract void
fillArc(int
the x coordinate of the upper-left corner of the arc to be filled.
x
,
int
the y coordinate of the upper-left corner of the arc to be filled.
y
,
int
the width of the arc to be filled.
width
,
int
the height of the arc to be filled.
height
,
int
the beginning angle.
startAngle
,
int
the angular extent of the arc, relative to the start angle.
arcAngle
)

Fills a circular or elliptical arc covering the specified rectangle.

public abstract void
fillOval(int
the x coordinate of the upper left corner of the oval to be filled.
x
,
int
the y coordinate of the upper left corner of the oval to be filled.
y
,
int
the width of the oval to be filled.
width
,
int
the height of the oval to be filled.
height
)

Fills an oval bounded by the specified rectangle with the current color.

public abstract void
fillPolygon(int[]
an array of x coordinates.
xPoints
,
int[]
an array of y coordinates.
yPoints
,
int
the total number of points.
nPoints
)

Fills a closed polygon defined by arrays of x and y coordinates.

public void
fillPolygon(Polygon
the polygon to fill.
p
)

Fills the polygon defined by the specified Polygon object with the graphics context's current color.

public abstract void
fillRect(int
the x coordinate of the rectangle to be filled.
x
,
int
the y coordinate of the rectangle to be filled.
y
,
int
the width of the rectangle to be filled.
width
,
int
the height of the rectangle to be filled.
height
)

Fills the specified rectangle.

public abstract void
fillRoundRect(int
the x coordinate of the rectangle to be filled.
x
,
int
the y coordinate of the rectangle to be filled.
y
,
int
the width of the rectangle to be filled.
width
,
int
the height of the rectangle to be filled.
height
,
int
the horizontal diameter of the arc at the four corners.
arcWidth
,
int
the vertical diameter of the arc at the four corners.
arcHeight
)

Fills the specified rounded corner rectangle with the current color.

public void
finalize()

Overrides java.lang.Object.finalize.

Deprecated for removal since 9. Finalization has been deprecated for removal. See java.lang.Object#finalize for background information and details about migration options.

Disposes of this graphics context once it is no longer referenced.

public abstract Shape

Returns:

a Shape object representing the current clipping area, or null if no clip is set.
getClip
()

Gets the current clipping area.

public abstract Rectangle

Returns:

the bounding rectangle of the current clipping area, or null if no clip is set.
getClipBounds
()

Returns the bounding rectangle of the current clipping area.

public Rectangle

Returns:

the bounding rectangle of the current clipping area.
getClipBounds
(Rectangle
the rectangle where the current clipping area is copied to. Any current values in this rectangle are overwritten.
r
)

Returns the bounding rectangle of the current clipping area.

public Rectangle

Returns:

the bounding rectangle of the current clipping area or null if no clip is set.
getClipRect
()
Deprecated As of JDK version 1.1, replaced by getClipBounds().

Returns the bounding rectangle of the current clipping area.

public abstract Color

Returns:

this graphics context's current color.
getColor
()

Gets this graphics context's current color.

public abstract Font

Returns:

this graphics context's current font.
getFont
()

Gets the current font.

public FontMetrics

Returns:

the font metrics of this graphics context's current font.
getFontMetrics
()

Gets the font metrics of the current font.

public abstract FontMetrics

Returns:

the font metrics for the specified font.
getFontMetrics
(Font
the specified font
f
)

Gets the font metrics for the specified font.

public boolean

Returns:

true if the specified rectangle intersects the bounds of the current clip; false otherwise.
hitClip
(int
the x coordinate of the rectangle to test against the clip
x
,
int
the y coordinate of the rectangle to test against the clip
y
,
int
the width of the rectangle to test against the clip
width
,
int
the height of the rectangle to test against the clip
height
)

Returns true if the specified rectangular area might intersect the current clipping area.

public abstract void
setClip(int
the x coordinate of the new clip rectangle.
x
,
int
the y coordinate of the new clip rectangle.
y
,
int
the width of the new clip rectangle.
width
,
int
the height of the new clip rectangle.
height
)

Sets the current clip to the rectangle specified by the given coordinates.

public abstract void
setClip(Shape
the Shape to use to set the clip. Passing null clears the current clip.
clip
)

Sets the current clipping area to an arbitrary clip shape.

public abstract void
setColor(Color
the new rendering color.
c
)

Sets this graphics context's current color to the specified color.

public abstract void
setFont(Font
the font.
font
)

Sets this graphics context's font to the specified font.

public abstract void
setPaintMode()

Sets the paint mode of this graphics context to overwrite the destination with this graphics context's current color.

public abstract void
setXORMode(Color
the XOR alternation color
c1
)

Sets the paint mode of this graphics context to alternate between this graphics context's current color and the new specified color.

public String

Returns:

a string representation of this graphics context.
toString
()

Overrides java.lang.Object.toString.

Returns a String object representing this Graphics object's value.

public abstract void
translate(int
the x coordinate.
x
,
int
the y coordinate.
y
)

Translates the origin of the graphics context to the point (x, y) in the current coordinate system.

Inherited from java.lang.Object:
cloneequalsgetClasshashCodenotifynotifyAllwaitwaitwait