Java Reference
In-Depth Information
The Method paint and the Class Graphics
Almost all Swing and Swing-related components and containers have a method named
paint. The method paint draws the component or container on the screen. Up
until now, we have had no need to redefine this method or to even mention it. It is
defined for you and is called automatically when the figure is displayed on the screen.
However, to draw geometric figures, such as circles and boxes, you need to redefine the
method paint. It is this method that draws the figures.
Display 18.11 shows a GUI program that displays a JFrame with a rather primitive
face drawn inside of it. The mouth and eyes are just straight line segments. We will
soon see how to get round eyes and a smile (and more), but the basic technique can be
seen more clearly in this simple figure. The code for drawing the face is given in the
method paint.
paint
VideoNote
Walkthrough of a
Simple Drawing
Program
Display 18.11
Drawing a Very Simple Face (part 1 of 2)
1 import javax.swing.JFrame;
2 import java.awt.Graphics;
3 import java.awt.Color;
4 public class Face extends JFrame
5 {
6 public static final int WINDOW_WIDTH = 400;
7 public static final int WINDOW_HEIGHT = 400;
8 public static final int FACE_DIAMETER = 200;
9 public static final int X_FACE = 100;
10
public static final int Y_FACE = 100;
11
public static final int EYE_WIDTH = 20;
12
public static final int X_RIGHT_EYE = X_FACE + 55;
13
public static final int Y_RIGHT_EYE = Y_FACE + 60;
14
public static final int X_LEFT_EYE = X_FACE + 130;
15
public static final int Y_LEFT_EYE = Y_FACE + 60;
16
public static final int MOUTH_WIDTH = 100;
17
public static final int X_MOUTH = X_FACE + 50;
18
public static final int Y_MOUTH = Y_FACE + 150;
19 public static void main(String[] args)
20 {
21 Face drawing = new Face();
22 drawing.setVisible( true );
23 }
24 public Face()
25 {
 
Search WWH ::




Custom Search