Packages  This Package  Prev  Next  Index  

§1.20 Class Graphics

public  abstract  class  java.awt.Graphics
    extends  java.lang.Object  (I-§1.12)
{
        // Constructors
    protected Graphics();	§1.20.1

        // Methods
    public abstract void clearRect(int  x, int  y, 	§1.20.2
                                            int width, int  height);
    public abstract void clipRect(int  x, int  y, 	§1.20.3
                                           int width, int height);
    public abstract void	§1.20.4
        copyArea(int  x, int  y, int  width,
                    int  height, int  dx, int  dy);
    public abstract Graphics create();	§1.20.5
    public Graphics create(int  x, int  y,	§1.20.6
                        int  width, int  height);
    public abstract void dispose();	§1.20.7
    public void draw3DRect(int  x, int  y, int  width,	§1.20.8
                                      int  height, boolean  raised);
    public abstract voiddrawArc(int  x, int  y, int  width,	§1.20.9
                                              int  height, int  startAngle,
                            int  arcAngle);
    public void drawBytes(byte  data[], int  offset,	§1.20.10
                                int  length, int  x, int  y);
    public void drawChars(char  data[], int  offset,	§1.20.11
                                int  length, int  x, int  y);
    public abstract boolean	§1.20.12
        drawImage(Image  img, int  x, int  y,  Color  bgcolor,
                    ImageObserver  observer);
    public abstract boolean	§1.20.13
        drawImage(Image  img, int  x, int  y,
                    ImageObserver  observer);
    public abstract boolean	§1.20.14
        drawImage(Image  img, int  x, int  y,
                            int  width, int  height, Color  bgcolor,
                            ImageObserver  observer);
    public abstract boolean	§1.20.15
        drawImage(Image  img, int  x, int  y,
                  int  width, int  height,
                                    ImageObserver  observer);
    public abstract void drawLine(int  x1, int  y1,	§1.20.16
              int  x2,  int  y2);
    public abstract void drawOval(int  x, int  y,	§1.20.17
                                    int width, int  height);
    public abstract void	§1.20.18
        drawPolygon(int  xPoints[], int yPoints[], 
                                int  nPoints);
    public void drawPolygon(Polygon  p);	§1.20.19
    public void drawRect(int  x, int  y,	§1.20.20
                   int  width, int  height);
    public abstract void	§1.20.21
        drawRoundRect(int  x, int  y, int  width,
              int  height, int  arcWidth,
                  int  arcHeight);
    public abstract void	§1.20.22
        drawString(String  str, int  x, int  y);
    public void	§1.20.23
        fill3DRect(int  x, int  y, int  width,
                              int  height, boolean  raised);
    public abstract void	§1.20.24
        fillArc(int  x, int  y, int  width,
                        int  height, int  startAngle,
                        int  arcAngle);
    public abstract void	§1.20.25
        fillOval(int  x, int  y, int  width, int  height);
    public abstract void	§1.20.26
        fillPolygon(int  xPoints[], int  yPoints[], int  nPoints);
    public void fillPolygon(Polygon  p);	§1.20.27
    public abstract void	§1.20.28
        fillRect(int  x, int  y, int  width,  int  height);
    public abstract void	§1.20.29
        fillRoundRect(int  x, int  y, int  width, int height, 
                                    int  arcWidth,  int  arcHeight);
    public void finalize();	§1.20.30
    public abstract Rectangle getClipRect();	§1.20.31
    public abstract Color getColor();	§1.20.32
    public abstract Font getFont();	§1.20.33
    public FontMetrics getFontMetrics();	§1.20.34
    public abstract FontMetrics getFontMetrics(Font  f);	§1.20.35
    public abstract void setColor(Color  c);	§1.20.36
    public abstract void setFont(Font  font);	§1.20.37
    public abstract void setPaintMode();	§1.20.38
    public abstract void setXORMode(Color  c1);	§1.20.39
    public String toString();	§1.20.40
    public abstract void   translate(int  x, int  y);	§1.20.41
}
The Graphics class is the abstract base class for all graphics contexts which allow an application to draw onto components or onto off-screen images.

Unless otherwise stated, all graphics operations performed with a graphics context object only modify bits within the graphic context's clipping region (II-§1.20.3). All drawing or writing is done in the current color (II-§1.20.36) using the current paint mode (II-§1.20.38, §1.20.39) and in the current font (II-§1.20.37).


Constructors

Graphics

protected Graphics()
This constructor is the default contructor for a graphics context.
Since Graphics is an abstract class, applications cannot call this constructor directly. Graphics contexts are obtained from other graphics contexts (II-§1.20.5) or are created by a component (II-§1.10.17).


Methods

clearRect

public abstract void
clearRect(int x, int y, int width, int height)
Clears the specified rectangle by filling it with the background color of the screen1. This operation does not use the current paint mode (II-§1.20.38, §1.20.39).

Parameters:
x - the x coordinate
y - the y coordinate
width - the width of the rectangle
height - the height of the rectangle
See Also:
fillRect (II-§1.20.28)
drawRect (II-§1.20.20).

clipRect

public abstract void
clipRect(int x, int y, int width, int height)
Sets a clipping rectangle for this graphics context. The resulting clipping area is the intersection of the current clipping area and the specified rectangle.
Graphics operations performed with this graphics context have no effect outside the clipping area.
Parameters:
x - the x coordinate
y - the y coordinate
width - the width of the rectangle
height - the height of the rectangle
See Also:
getClipRect (II-§1.20.31).

copyArea

public abstract void
copyArea(int x, int y, int width, int height,
int dx, int dy)
Copies an area. The source is the rectangle with origin , and size specified by the width and height arguments. The destination is the rectangle with origin and the same width and height.
The clipping rectangle of this graphics context affects only the destination, not the source.
Parameters:
x - the x-coordinate of the source
y - the y-coordinate of the source
width - the width
height - the height
dx - the horizontal distance
dy - the vertical distance

create

public abstract Graphics create()
Returns:
a new graphics context that is a copy of this graphics context.

create

public Graphics
create(int x, int y, int width, int height)
Creates a new graphics context The new graphics context is identical to this graphics context, except in two respects:
Parameters:
x - the x coordinate
y - the y coordinate
width - the width of the clipping rectangle
height - the height of the clipping rectangle
Returns:
a new graphics context.
See Also:
translate (II-§1.20.41).

dispose

public abstract void dispose()
Disposes of this graphics context.
The graphics context cannot be used after being disposed.
See Also:
finalize (II-§1.20.30).

draw3DRect

public void draw3DRect(int x, int y, int width,
int height, boolean raised)
Draws a highlighted 3-D rectangle.
Parameters:
x - the x coordinate
y - the y coordinate
width - the width of the rectangle
height - the height of the rectangle
raised - if true, the rectangle appears raised; if false, it appears lowered

drawArc

public abstract void
drawArc(int x, int y, int width, int height,
int startAngle, int arcAngle)
Draws a single circular or elliptical arc.
The center of the arc is the center of the rectangle whose origin is and whose size is specified by the width and height arguments.
The two axes of the arc are given by the width and height arguments.
The arc is drawn from startAngle to startAngle + arcAngle. The start angle and arc angle are in degrees, not radians.
A start angle of 0 indicates the 3-o'clock position. A positive arc angle indicates a counter-clockwise rotation; a negative arc angle indicates a clockwise rotation.
Parameters:
x - the x coordinate
y - the y coordinate
width - the width of the rectangle
height - the height of the rectangle
startAngle - the beginning angle
arcAngle - the angle of the arc (relative to startAngle).
See Also:
fillArc (II-§1.20.24).

drawBytes

public void drawBytes(byte data[], int offset,
int length, int x, int y)
Draws the text given by the specified byte array using this graphics context's current font and color. The baseline of the first character is at position in this graphics context's coordinate system.
Parameters:
data - the data to be drawn
offset - the start offset in the data
length - the number of bytes that are drawn
x - the x coordinate
y - the y coordinate
See Also:
drawString (II-§1.20.22)
drawChars (II-§1.20.11).

drawChars

public void drawChars(char data[], int offset,
int length, int x, int y)
Draws the text given by the specified character array using this graphics context's current font and color. The baseline of the first character is at position in the graphics context's coordinate system.
Parameters:
data - the array of characters to be drawn
offset - the start offset in the data
length - the number of characters to be drawn
x - the x coordinate
y - the y coordinate
See Also:
drawString (II-§1.20.22)
drawBytes (II-§1.20.10).

drawImage

public abstract boolean
drawImage(Image img, int x, int y, Color bgcolor,
ImageObserver observer)
Draws the specified image with its top left corner at in this graphics context's coordinate space. Transparent pixels in the image are drawn in the specified background color.
If the image has not yet been completely loaded, the image observer's imageUpdate (II-§2.11.9) method is notified as more of the image becomes available.

Parameters:
img - the specified image to be drawn
x - the x coordinate
y - the y coordinate
bgcolor - the background color
observer - object to notify when the image is loaded.
Returns:
true if all bits of the image are available; false otherwise.
See Also:
Image (II-§1.24)
ImageObserver (II-§2.11).

drawImage

public abstract boolean
drawImage(Image img, int x, int y, ImageObserver observer)
Draws the specified image with its top left corner at in this graphics context's coordinate space. Transparent pixels in the image do not affect whatever pixels are already there.
If the image has not yet been completely loaded, the image observer's imageUpdate (II-§2.11.9) method is notified as more of the image becomes available.

Parameters:
img - the specified image to be drawn
x - the x coordinate
y - the y coordinate
observer - object to notify when the image is loaded
Returns:
true if all bits of the image are available; false otherwise.
See Also:
Image (II-§1.24)
ImageObserver (II-§2.11).

drawImage

public abstract boolean
drawImage(Image img, int x, int y, int width,
int height, Color bgcolor, ImageObserver observer)
Draws the specified image inside the specified rectangle of this graphics context's coordinate space. The image is scaled if necessary. Transparent pixels in the image are drawn in the specified background color.
If the image has not yet been completely loaded, the image observer's imageUpdate (II-§2.11.9) method is notified as more of the image becomes available.

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
bgcolor - background color for the image
observer - object to notify when the image is loaded
Returns:
true if all bits of the image are available; false otherwise.
See Also:
Image (II-§1.24)
ImageObserver (II-§2.11).

drawImage

public abstract boolean
drawImage(Image img, int x, int y, int width,
int height, ImageObserver observer)
Draws the specified image inside the specified rectangle of this graphics context's coordinate space. The image is scaled if necessary. Transparent pixels in the image do not affect whatever pixels are already there.
If the image has not yet been completely loaded, the image observer's imageUpdate (II-§2.11.9) method is notified as more of the image becomes available.

Parameters:
img - the specified image to be drawn
x - the x coordinate
y - the y coordinate
width - the width of the rectangle
bgcolor - background color for the image
observer - object to notify when the image is loaded
Returns:
true if all bits of the image are available; false otherwise.
See Also:
Image (II-§1.24)
ImageObserver (II-§2.11).

drawLine

public abstract void
drawLine(int x1, int y1, int x2, int y2)
Draws a line from to in this graphics context's coordinate system.
The line is drawn below and to the right of the logical coordinates.
Parameters:
x1 - the first point's x coordinate
y1 - the first point's y coordinate
x2 - the second point's x coordinate
y2 - the second point's y coordinate

drawOval

public abstract void
drawOval(int x, int y, int width, int height)
Draws a circle or an ellipse such that it fits within the rectangle specified by the x, y, width and height arguments.
The center of the oval is the center of the rectangle whose origin is is this graphics context's coordinate system and whose size is specified by the width and height arguments.
Parameters:
x - the x coordinate
y - the y coordinate
width - the width of the rectangle
height - the height of the rectangle
See Also:
fillOval (II-§1.20.25).

drawPolygon

public abstract void
drawPolygon(int xPoints[], int yPoints[], int nPoints)
Draws a closed polygon defined by an array of x points and y points.
This method draws the polygon defined by npoint line segments, where the first npoint-1 line segments are lines segments from to , for . The last line segment starts at the final point and ends at the first point.
Parameters:
xPoints - an array of x points
yPoints - an array of y points
nPoints - the total number of points
See Also:
fillPolygon (II-§1.20.26).

drawPolygon

public void drawPolygon(Polygon p)
Draws a polygon defined by the specified polygon argument.
Parameters:
p - a polygon
See Also:
fillPolygon (II-§1.20.26).

drawRect

public void drawRect(int x, int y, int width, int height)
Draws the outline of the specified rectangle using the current color. The left and right edges of the rectangle are at and respectively. The top and bottom edges of the rectangle are at and . respectively.
Parameters:
x - the x coordinate
y - the y coordinate
width - the width of the rectangle
height - the height of the rectangle
See Also:
fillRect (II-§1.20.28)
clearRect (II-§1.20.2).

drawRoundRect

public abstract void
drawRoundRect(int x, int y, int width, int height,
int arcWidth, int arcHeight)
Draws an outlined round-cornered rectangle using this graphics context's current color. The left and right edges of the rectangle are at and respectively. The top and bottom edges of the rectangle are at and .
Parameters:
x - the x coordinate
y - the y coordinate
width - the width of the rectangle
height - the height of the rectangle
arcWidth - the horizontal diameter of the arc at the four corners
arcHeight - the vertical diameter of the arc at the four corners
See Also:
fillRoundRect (II-§1.20.29).

drawString

public abstract void drawString(String str, int x, int y)
Draws the string using this graphics context's current font and color. The baseline of the first character is at position in the graphics context's coordinate system.
Parameters:
str - the string to be drawn
x - the x coordinate
y - the y coordinate
See Also:
drawChars (II-§1.20.11)
drawBytes (II-§1.20.10).

fill3DRect

public void fill3DRect(int x, int y, int width,
int height, boolean raised)
Draws a highlighted 3-D rectangle that is filled with this graphics context's current color.
Parameters:
x - the x coordinate
y - the y coordinate
width - the width of the rectangle
height - the height of the rectangle
raised - if true, the rectangle is raised; if false, it is lowered

fillArc

public abstract void
fillArc(int x, int y, int width, int height,
int startAngle, int arcAngle)
Draws a single circular or elliptical arc that is filled with this graphics context's current color. The result is a pie shape.
The center of the arc is the center of the rectangle whose origin is and whose size is specified by the width and height arguments.
The two axes of the arc are given by the width and height arguments.
The arc is drawn from startAngle to startAngle + arcAngle. The start angle and arc angle are in degrees, not radians.
A start angle of 0 indicates the 3-o'clock position. A positive arc angle indicates a counter-clockwise rotation; a negative arc angle indicates a clockwise rotation.
Parameters:
x - the x coordinate
y - the y coordinate
width - the width of the rectangle
height - the height of the rectangle
startAngle - the beginning angle
arcAngle - the angle of the arc (relative to startAngle).
See Also:
drawArc (II-§1.20.9).

fillOval

public abstract void
fillOval(int x, int y, int width, int height)
Draws a filled circle or a filled ellipse using this graphics context's current color.
The center of the oval is the center of the rectangle whose origin is is the graphics context's coordinate system and whose size is specified by the width and height arguments.
Parameters:
x - the x coordinate
y - the y coordinate
width - the width of the rectangle
height - the height of the rectangle
See Also:
drawOval (II-§1.20.17).

fillPolygon

public abstract void
fillPolygon(int xPoints[], int yPoints[], int nPoints)
Fills a polygon defined by an array of x points and y points with this graphics context's current color.
This method fills the polygon defined by npoint line segments, where the first npoint-1 line segments are lines segments from to , for . The last line segment starts at the final point and ends at the first point.
The area inside the polygon is defined using an "even-odd" fill rule, also known as the "alternating rule."
Parameters:
xPoints - an array of x points
yPoints - an array of y points
nPoints - the total number of points
See Also:
drawPolygon (II-§1.20.18).

fillPolygon

public void fillPolygon(Polygon p)
Fills a polygon defined by the specified polygon argument with this graphics context's current color.
The area inside the polygon is defined using an "even-odd" fill rule, also known as the "alternating rule."
Parameters:
p - the polygon
See Also:
drawPolygon (II-§1.20.19).

fillRect

public abstract void
fillRect(int x, int y, int width, int height)
Fills the specified rectangle with this graphics context's current color. The left and right edges are at and respectively. The top and bottom edges are at and respectively.
Parameters:
x - the x coordinate
y - the y coordinate
width - the width of the rectangle
height - the height of the rectangle
See Also:
drawRect (II-§1.20.20)
clearRect (II-§1.20.2).

fillRoundRect

public abstract void
fillRoundRect(int x, int y, int width, int height,
int arcWidth, int arcHeight)
Fills an outlined rounded corner rectangle with this graphics context's current color. The left and right edges are at and respectively. The top and bottom edges are at and respectively.
Parameters:
x - the x coordinate
y - the y coordinate
width - the width of the rectangle
height - the height of the rectangle
arcWidth - the horizontal diameter of the arc at the four corners
arcHeight - the vertical diameter of the arc at the four corners
Draws a rounded rectangle filled in with the current color.
See Also:
drawRoundRect (II-§1.20.21).

finalize

public void finalize()
The finalize method ensures that this graphics context's dispose method (II-§1.20.7) is called when this graphics context is no longer referenced.

Overrides:
finalize in class Object (I-§1.12.4).

getClipRect

public abstract Rectangle getClipRect()
Returns:
the bounding rectangle of this graphics context's clipping area.
See Also:
clipRect (II-§1.20.3).

getColor

public abstract Color getColor()
Returns:
this graphics context's current color.
See Also:
setColor (II-§1.20.36).

getFont

public abstract Font getFont()
Returns:
this graphics context's current font.
See Also:
setFont (II-§1.20.37).

getFontMetrics

public FontMetrics getFontMetrics()
Returns:
this font metrics of the graphics context's current font.
See Also:
getFont (II-§1.20.33).

getFontMetrics

public abstract FontMetrics getFontMetrics(Font f)
Parameters:
f - the specified font
Returns:
the font metrics for the specified font.
See Also:
getFont (II-§1.20.33).

setColor

public abstract void setColor(Color c)
Sets this graphics context's current color to the specified color. All subsequent graphics operations using this graphics context use this specified color.
Parameters:
c - the color
See Also:
Color (II-§1.9)
getColor (II-§1.20.32).

setFont

public abstract void setFont(Font font)
Sets this graphics context's font to the specified font.
All subsequent text operations (such as drawString (II-§1.20.22), drawBytes (II-§1.20.10), and drawChars (II-§1.20.11)) using this graphics context use this font.

Parameters:
font - the font

setPaintMode

public abstract void setPaintMode()
Sets the paint mode of this graphics context to overwrite the destination with this graphics context's current color.

setXORMode

public abstract void setXORMode(Color c1)
Sets the paint mode of this graphics context to alternate between this graphics context's current color and the new specified 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.
Parameters:
c1 - the second color

toString

public String toString()
Returns:
a string representation of this graphics context.
Overrides:
toString in class Object (I-§1.12.9).

translate

public abstract void translate(int x, int y)
Modifies this graphics context so that its new origin corresponds to the point in this graphics context's original coordinate system.
Parameters:
x - the x coordinate
y - the y coordinate

1 In Java 1.0, the background color of offscreen images is white. In Java 1.1, the background color of offscreen images may be system dependent. Applications should use setColor (II-§1.20.36) followed by fillRect (II-§1.20.28) to ensure that an offscreen image is cleared to a specific color.

Packages  This Package  Prev  Next  Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to doug.kramer@sun.com