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.
public void clearRect(int x,
int y,
int width,
int height)
x -
the x coordinate
y -
the y coordinate
width -
the width of the rectangle
height -
the height of the rectangle
public void draw3DRect(int x,
int y,
int width
int height,
boolean raised)
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
public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
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).
public void drawLine(int x1,
int y1,
int x2,
int y2)
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
public void drawOval(int x,
int y,
int width,
int height)
x -
the x coordinate
y -
the y coordinate
width -
the width of the rectangle
height -
the height of the rectangle
public void drawPolygon(Polygon p)
p -
a
polygon
public void drawRect(int x,
int y,
int width,
int height)
x -
the x coordinate
y -
the y coordinate
width -
the width of the rectangle
height -
the height of the rectangle
public void drawRoundRect(int x,
int y,
int width,
int height,
int arcWidth,
int arcHeight)
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
public void drawString(String str,
int x,
int y)
str -
the string to be drawn
x -
the x coordinate
y -
the y coordinate
public void fill3DRect(int x,
int y,
int width,
int height,
boolean raised)
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
public void fillArc(int x,
int y,
int width,
int height,
int startAngle,
int arcAngle)
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).
public abstract void fillOval(int x,
int y,
int width,
int height)
x -
the x coordinate
y -
the y coordinate
width -
the width of the rectangle
height -
the height of the rectangle
public void fillPolygon(Polygon p)
p -
the polygon
public void fillRect(int x,
int y,
int width,
int height)
x -
the x coordinate
y -
the y coordinate
width -
the width of the rectangle
height -
the height of the rectangle
public void fillRoundRect(int x,
int y,
int width,
int height,
int arcWidth,
int arcHeight)
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
public void setColor(Color c)
c -
the color
public void setFont(Font font)
font -
the font
public void setPaintMode()
public void setXORMode(Color c1)
c1 -
the second color