Simple graphics in Java
There are four classes/interfaces that we can use to do real graphics (meaning, drawing
shapes and lines) in Java:
- The class PaintPanel and the interface Painter give you a place to draw and
an interface for drawing. These two classes allow you to do simple graphics
without having to deal with some features of the Java language that are not
covered in CSCI 235.
- Class Graphics is used by a Painter to actually do the drawing. You can find
more features of this class in section 18.3 of your textbook.
- Class Polygon is used by Graphics to describe polygons.
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
- PaintPanel(int width, int height) – The constructor which allows the
programmer to specify the size, in pixels.
- int width() and int height() – methods for finding out the width and
height, in pixels, of the panel.
- void setPainter(Painter) – A method for attaching a Painter object to
this panel, which will do the actual drawing; more about this below.
- void repaint() – Refresh this panel (after, presumably, changes have been
made).
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.
Whenever we want the panel to be drawn, we will call repaint() on the PaintPanel.
That automatically calls the attached Painter’s method paint() with the appropriate
Graphics object.
The panel’s repaint() method is also automatically called whenever the panel needs to
be drawn; for example, when it is made visible for the first time, when it is shown after
being minimized, or when it is uncovered by a window that hid all or part of
it.
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.
- setColor(Color c)
changes the “pen color”, ie, specifies the color of the next thing drawn.
(See the documentation for java.awt.Color for names of contants and other
ways to get color values.)
- drawRect(int x, int y, int width, int height)
draw (the outline of) a rectangle with one corner at point (x,y) and having
size width by height. If width and height are both positive, then the given
pair of coordinates will specify the upper lefthand corner.
- fillRect(int x, int y, int width, int height)
like the previous except that the rectangle is filled in.
- drawOval(int x, int y, int width, int height)
fillOval(int x, int y, int width, int height)
like drawRect() except for an oval. Note that (x,y) still refers to the upper
lefthand “corner” of the oval, which really means the upper lefthand corner of
the smallest rectangle that frames the oval. If width and height are equal,
then this draws a circle.
- drawPolygon(Polygon p)
fillPolygon(Polygon p)
like the above but for a polygon.
- drawLine(int x1, int y1, int x2, int y2)
draw a line from (x1,y1) to (x2,y2).
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)