The Graphics Class
Every applet's paint(Graphics g) method receives an object of type
java.awt.Graphics. That single object is your entire drawing toolkit - it draws text, lines,
rectangles, ovals, and polygons onto the applet's surface. Every coordinate you pass is a pixel position
measured from the applet's top-left corner: (0, 0) is the top-left, x increases to the right,
and y increases downward (not upward, as you might expect from school-level graphs).
drawString - Text
You've already used this one in Your First Applet. It draws a string with its baseline at the given (x, y):
g.drawString("Hello!", 20, 30);
drawLine - Straight Lines
Draws a line between two points, (x1, y1) and (x2, y2):
g.drawLine(20, 50, 120, 50);
drawRect and fillRect - Rectangles
Both take the top-left corner plus a width and height. drawRect draws only the outline;
fillRect paints the whole rectangle solid using the current color:
g.drawRect(20, 60, 60, 40); // outline only
g.fillRect(20, 60, 60, 40); // solid
There are rounded-corner variants too, drawRoundRect and fillRoundRect, which take two extra arguments for the corner arc's width and height.
drawOval and fillOval - Circles and Ellipses
These also take a bounding box (x, y, width, height) - the oval is drawn inscribed inside that rectangle. Make the width equal to the height and you get a circle:
g.fillOval(100, 60, 40, 40); // a solid circle, 40x40
drawPolygon - Arbitrary Shapes
For anything with more than four sides, build parallel arrays of x and y coordinates and pass them along with the point count:
int[] xPoints = {200, 230, 260};
int[] yPoints = {100, 60, 100};
g.drawPolygon(xPoints, yPoints, 3); // a triangle
There is a matching fillPolygon(int[] xPoints, int[] yPoints, int nPoints) for a solid version.
| Method | Draws |
|---|---|
drawString(text, x, y) | Text, baseline at (x, y) |
drawLine(x1, y1, x2, y2) | A straight line between two points |
drawRect / fillRect(x, y, w, h) | Rectangle outline / solid rectangle |
drawOval / fillOval(x, y, w, h) | Ellipse outline / solid ellipse inscribed in the box |
drawPolygon / fillPolygon(int[], int[], n) | Outline / solid shape from n points |
Putting It Together: Extending PaintApplet
Rather than crowd every shape call directly into paint(), pull them into a small private
helper method, drawShapes(Graphics g), and call that helper from paint(). This
keeps paint() readable as the applet grows more drawing logic in later lessons:
import java.applet.Applet;
import java.awt.Graphics;
public class PaintApplet extends Applet {
public void init() {
setBackground(java.awt.Color.WHITE);
}
public void paint(Graphics g) {
g.drawString("Welcome to PaintApplet!", 20, 30);
g.drawRect(10, 10, getWidth() - 20, getHeight() - 20);
drawShapes(g);
}
private void drawShapes(Graphics g) {
g.drawLine(20, 50, 120, 50);
g.drawRect(20, 60, 60, 40);
g.fillOval(100, 60, 40, 40);
int[] xPoints = {200, 230, 260};
int[] yPoints = {100, 60, 100};
g.drawPolygon(xPoints, yPoints, 3);
}
}
drawShapes() is marked private because it's an internal implementation detail of
PaintApplet, not something any other class needs to call. paint() now reads as a
short list of what gets drawn, with the "how" tucked away in the helper.
PaintApplet some color and a
styled title.