Java Reference
In-Depth Information
g2.draw(circle); //
draws the shape in red
If you want to color the inside of the shape, use the
fill
method instead of the
draw
method. For example,
g2.fill(circle);
fills the inside of the circle with the current color.
67
68
Table 1 Predefined Colors and their RGB Values
Color
RGB Value
Color.BLACK
0, 0, 0
Color.BLUE
0, 0, 255
Color.CYAN
0, 255, 255
Color.GRAY
128, 128, 128
Color.DARKGRAY
64, 64, 64
Color.LIGHTGRAY
192, 192, 192
Color.GREEN
0, 255, 0
Color.MAGENTA
255, 0, 255
Color.ORANGE
255, 200, 0
Color.PINK
255, 175, 175
Color.RED
255, 0, 0
Color.WHITE
255, 255, 255
Color.YELLOW
255, 255, 0
The following program puts all these shapes to work, creating a simple drawing (see
Figure 25
).
68
69
ch02/faceviewer/FaceComponent.java
1
import
java.awt.Color;
2
import
java.awt.Graphics;
3
import
java.awt.Graphics2D;
4
import
java.awt.Rectangle;
5
import
java.awt.geom.Ellipse2D;
6
import
java.awt.geom.Line2D;
7
import
javax.swing.JPanel;
8
import
javax.swing.JComponent;
9
10 /**
11
A component that draws an alien face.
12 */
13
public class
FaceComponent
extends
JComponent
14 {