Graphics Contract

An instance of the Graphics class is an object that is capable of drawing in a window on the screen. The state of a graphics object includes the current pen color, the current font, and the current drawing mode; these determine how a graphics object responds to various drawing commands.

Below is a simple contract for the Graphics class. It turns out that instances of the Graphics class actually implement more methods than are described here. The full list of methods can be found in the Java API for the class java.awt.Graphics.


Index for the Graphics Class

Methods:


Methods of the Graphics Class


clearRect

public 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.
Parameters:
x - the x coordinate
y - the y coordinate
width - the width of the rectangle
height - the height of the rectangle
See Also:
fillRect
drawRect


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


drawLine

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


drawRoundRect

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


drawString

public 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.
Parameters:
str - the string to be drawn
x - the x coordinate
y - the y coordinate


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


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.
Parameters:
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 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


fillRect

public 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.
Parameters:
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 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.
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


setColor

public 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


setFont

public void setFont(Font font) 
Sets this graphics context's font to the specified font.
All subsequent text operations (such as drawString) using this graphics context use this font.
Parameters:
font - the font


setPaintMode

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


setXORMode

public 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



Franklyn Turbak adapted this page from the
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