There are four classes that we’ll meet today that will allow us to do real graphics (meaning, drawing shapes and lines) in Java:

PaintPanel

A PaintPanel is a window component, just like JLabel or JButton. It can be added to a window. It is simply an area where the shapes will appear. The methods that we’ll be concerned with are

Painter

This interface contains one method, void paint(Graphics g). Objects of classes that implement this interface are attached to a PaintPanel. When the paint() method is called, the Painter draws shapes and lines on the paint panel by calling appropriate methods on the Graphics object, more on that below.

The interaction works like this: Any time a change has been made which affects the graphical display, we need to call repaint() on the PaintPanel. This will automatically call the paint() method of the Painter object attached to the PaintPanel, giving the appropriate Graphics object as a parameter.

This also happens (ie, the paint() method of the Painter is automatically called) when the window containing the PaintPanel is first made visible.

Graphics

An instance of Graphics controls the drawing of a certain GUI component; in our examples it will always be a PaintPanel. When the following methods are called on the Graphics object, then lines, shapes, etc, appear on the PaintPanel. Coordinates act like coordinates in the first quadrant of the plane, except that the origin is in the upper lefthand corner, not the lower lefthand corner.

Polygon

This class is used to represent a polygon which can be passed to the fillPolygon() method. We’ll only need the constructor:

Polygon(int[] xpoints, int[] ypoints, int npoints)