Package java.awt Previous
Previous
Java API
Java API
Index
Index
Next
Next

Class Graphics

Constructors , Methods , Footnotes

public  abstract  class  java.awt.Graphics
    extends  java.lang.Object  
{
        // Constructors
    protected Graphics();	

        // Methods
    public abstract void clearRect(int  x, int  y, 	
                                            int width, int  height);
    public abstract void clipRect(int  x, int  y, 	
                                           int width, int height);
    public abstract void	
        copyArea(int  x, int  y, int  width,
                    int  height, int  dx, int  dy);
    public abstract Graphics create();	
    public Graphics create(int  x, int  y,	
                        int  width, int  height);
    public abstract void dispose();	
    public void draw3DRect(int  x, int  y, int  width,	
                                      int  height, boolean  raised);
    public abstract voiddrawArc(int  x, int  y, int  width,	
                                              int  height, int  startAngle,
                            int  arcAngle);
    public void drawBytes(byte  data[], int  offset,	
                                int  length, int  x, int  y);
    public void drawChars(char  data[], int  offset,	
                                int  length, int  x, int  y);
    public abstract boolean	
        drawImage(Image  img, int  x, int  y,  Color  bgcolor,
                    ImageObserver  observer);
    public abstract boolean	
        drawImage(Image  img, int  x, int  y,
                    ImageObserver  observer);
    public abstract boolean	
        drawImage(Image  img, int  x, int  y,
                            int  width, int  height, Color  bgcolor,
                            ImageObserver  observer);
    public abstract boolean	
        drawImage(Image  img, int  x, int  y,
                  int  width, int  height,
                                    ImageObserver  observer);
    public abstract void drawLine(int  x1, int  y1,	
              int  x2,  int  y2);
    public abstract void drawOval(int  x, int  y,	
                                    int width, int  height);
    public abstract void	
        drawPolygon(int  xPoints[], int yPoints[], 
                                int  nPoints);
    public void drawPolygon(Polygon  p);	
    public void drawRect(int  x, int  y,	
                   int  width, int  height);
    public abstract void	
        drawRoundRect(int  x, int  y, int  width,
              int  height, int  arcWidth,
                  int  arcHeight);
    public abstract void	
        drawString(String  str, int  x, int  y);
    public void	
        fill3DRect(int  x, int  y, int  width,
                              int  height, boolean  raised);
    public abstract void	
        fillArc(int  x, int  y, int  width,
                        int  height, int  startAngle,
                        int  arcAngle);
    public abstract void	
        fillOval(int  x, int  y, int  width, int  height);
    public abstract void	
        fillPolygon(int  xPoints[], int  yPoints[], int  nPoints);
    public void fillPolygon(Polygon  p);	
    public abstract void	
        fillRect(int  x, int  y, int  width,  int  height);
    public abstract void	
        fillRoundRect(int  x, int  y, int  width, int height, 
                                    int  arcWidth,  int  arcHeight);
    public void finalize();	
    public abstract Rectangle getClipRect();	
    public abstract Color getColor();	
    public abstract Font getFont();	
    public FontMetrics getFontMetrics();	
    public abstract FontMetrics getFontMetrics(Font  f);	
    public abstract void setColor(Color  c);	
    public abstract void setFont(Font  font);	
    public abstract void setPaintMode();	
    public abstract void setXORMode(Color  c1);	
    public String toString();	
    public abstract void   translate(int  x, int  y);	
}

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 . All drawing or writing is done in the current color using the current paint mode (setPaintMode, setXORMode) and in the current font .


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 or are created by a component .


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 screen(1). This operation does not use the current paint mode (setPaintMode, setXORMode).

ParameterDescription
x the x coordinate
y the y coordinate
width the width of the rectangle
height the height of the rectangle

See Also: fillRect drawRect .


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.

ParameterDescription
x the x coordinate
y the y coordinate
width the width of the rectangle
height the height of the rectangle

See Also: getClipRect .


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 (x,y), and size specified by the width and height arguments. The destination is the rectangle with origin (x+dx, y+dy) and the same width and height.

The clipping rectangle of this graphics context affects only the destination, not the source.

ParameterDescription
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() 

Return Value:

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:

Return Value:

Returns a new graphics context.

ParameterDescription
x the x coordinate
y the y coordinate
width the width of the clipping rectangle
height the height of the clipping rectangle

See Also: translate .


dispose

public abstract void dispose() 

Disposes of this graphics context.

The graphics context cannot be used after being disposed.

See Also: finalize .


draw3DRect

public void draw3DRect(int  x, int  y, int  width,
                                              int  height, boolean  raised) 

Draws a highlighted 3-D rectangle.

ParameterDescription
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 (x,y) 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.

ParameterDescription
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 .


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 (x,y) in this graphics context's coordinate system.

ParameterDescription
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 drawChars .


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 (x,y) in the graphics context's coordinate system.

ParameterDescription
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 drawBytes .


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 (x,y) 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 method is notified as more of the image becomes available.

Return Value:

Returns true if all bits of the image are available; false otherwise.

ParameterDescription
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.

See Also: Image ImageObserver .


drawImage

public abstract boolean
drawImage(Image  img, int  x, int  y, ImageObserver  observer) 

Draws the specified image 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.

If the image has not yet been completely loaded, the image observer's imageUpdate method is notified as more of the image becomes available.

Return Value:

Returns true if all bits of the image are available; false otherwise.

ParameterDescription
img the specified image to be drawn
x the x coordinate
y the y coordinate
observer object to notify when the image is loaded

See Also: Image ImageObserver .


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 method is notified as more of the image becomes available.

Return Value:

Returns true if all bits of the image are available; false otherwise.

ParameterDescription
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

See Also: Image ImageObserver .


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 method is notified as more of the image becomes available.

Return Value:

Returns true if all bits of the image are available; false otherwise.

ParameterDescription
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

See Also: Image ImageObserver .


drawLine

public abstract void
drawLine(int  x1, int  y1, int  x2, int  y2) 

Draws a line from (x1,y1) to (x2,y2) in this graphics context's coordinate system.

The line is drawn below and to the right of the logical coordinates.

ParameterDescription
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 (x,y) is this graphics context's coordinate system and whose size is specified by the width and height arguments.

ParameterDescription
x the x coordinate
y the y coordinate
width the width of the rectangle
height the height of the rectangle

See Also: fillOval .


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 (xPoints[i-1], yPoints[i-1]) to (xPoints[i], yPoints[i]), for 1 £ i < npoint. The last line segment starts at the final point and ends at the first point.

ParameterDescription
xPoints an array of x points
yPoints an array of y points
nPoints the total number of points

See Also: fillPolygon .


drawPolygon

public void drawPolygon(Polygon  p) 

Draws a polygon defined by the specified polygon argument.

ParameterDescription
p a polygon

See Also: fillPolygon .


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 x and x + width respectively. The top and bottom edges of the rectangle are at y and y + height. respectively.

ParameterDescription
x the x coordinate
y the y coordinate
width the width of the rectangle
height the height of the rectangle

See Also: fillRect clearRect .


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 x and x + width respectively. The top and bottom edges of the rectangle are at y and y + height.

ParameterDescription
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 .


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 (x,y) in the graphics context's coordinate system.

ParameterDescription
str the string to be drawn
x the x coordinate
y the y coordinate

See Also: drawChars drawBytes .


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.

ParameterDescription
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 (x,y) 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.

ParameterDescription
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 .


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 (x,y) is the graphics context's coordinate system and whose size is specified by the width and height arguments.

ParameterDescription
x the x coordinate
y the y coordinate
width the width of the rectangle
height the height of the rectangle

See Also: drawOval .


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 (xPoints[i-1], yPoints[i-1]) to (xPoints[i], yPoints[i]), for 1 £ i < npoints. 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."

ParameterDescription
xPoints an array of x points
yPoints an array of y points
nPoints the total number of points

See Also: drawPolygon .


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."

ParameterDescription
p the polygon

See Also: drawPolygon .


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 x and x + width - 1 respectively. The top and bottom edges are at y and y + height - 1 respectively.

ParameterDescription
x the x coordinate
y the y coordinate
width the width of the rectangle
height the height of the rectangle

See Also: drawRect clearRect .


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 x and x + width - 1 respectively. The top and bottom edges are at y and y + height - 1 respectively.

ParameterDescription
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

Remarks:

Draws a rounded rectangle filled in with the current color.

See Also: drawRoundRect .


finalize

public void finalize() 

The finalize method ensures that this graphics context's dispose method is called when this graphics context is no longer referenced.

Overrides:

finalize in class Object .


getClipRect

public abstract Rectangle getClipRect() 

Return Value:

Returns the bounding rectangle of this graphics context's clipping area.

See Also: clipRect .


getColor

public abstract Color getColor() 

Return Value:

Returns this graphics context's current color.

See Also: setColor .


getFont

public abstract Font getFont() 

Return Value:

Returns this graphics context's current font.

See Also: setFont .


getFontMetrics

public FontMetrics getFontMetrics() 

Return Value:

Returns this font metrics of the graphics context's current font.

See Also: getFont .


getFontMetrics

public abstract FontMetrics getFontMetrics(Font  f) 

Return Value:

Returns the font metrics for the specified font.

ParameterDescription
f the specified font

See Also: getFont .


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.

ParameterDescription
c the color

See Also: Color getColor .


setFont

public abstract void setFont(Font  font) 

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

All subsequent text operations (such as drawString , drawBytes , and drawChars ) using this graphics context use this font.

ParameterDescription
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.

ParameterDescription
c1 the second color


toString

public String toString() 

Return Value:

Returns a string representation of this graphics context.

Overrides:

toString in class Object .


translate

public abstract void translate(int  x, int  y) 

Modifies this graphics context so that its new origin corresponds to the point (x,y) in this graphics context's original coordinate system.

ParameterDescription
x the x coordinate
y the y coordinate


Footnotes

(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 followed by fillRect to ensure that an offscreen image is cleared to a specific color.

Top© 1996 Sun Microsystems, Inc. All rights reserved.