1 |
import java.applet.Applet; |
2 |
import java.awt.Color; |
3 |
import java.awt.Graphics; |
4 |
import java.awt.Graphics2D; |
5 |
import java.awt.Rectangle; |
6 |
import javax.swing.JOptionPane; |
7 |
|
8 |
/** |
9 |
An applet that lets a user choose a color by specifying |
10 |
the fractions of red, green, and blue. |
11 |
*/ |
12 |
public class ColorApplet extends Applet |
13 |
{ |
14 |
public ColorApplet() |
15 |
{ |
16 |
String input; |
17 |
|
18 |
// ask the user for red, green, blue values |
19 |
|
20 |
input = JOptionPane.showInputDialog("red:"); |
21 |
float red = Float.parseFloat(input); |
22 |
|
23 |
input = JOptionPane.showInputDialog("green:"); |
24 |
float green = Float.parseFloat(input); |
25 |
|
26 |
input = JOptionPane.showInputDialog("blue:"); |
27 |
float blue = Float.parseFloat(input); |
28 |
|
29 |
fillColor = new Color(red, green, blue); |
30 |
} |
31 |
|
32 |
public void paint(Graphics g) |
33 |
{ |
34 |
Graphics2D g2 = (Graphics2D)g; |
35 |
|
36 |
// select color into graphics context |
37 |
|
38 |
g2.setColor(fillColor); |
39 |
|
40 |
// construct and fill a square whose center is |
41 |
// the center of the window |
42 |
|
43 |
Rectangle square = new Rectangle( |
44 |
(getWidth() - SQUARE_LENGTH) / 2, |
45 |
(getHeight() - SQUARE_LENGTH) / 2, |
46 |
SQUARE_LENGTH, |
47 |
SQUARE_LENGTH); |
48 |
|
49 |
g2.fill(square); |
50 |
} |
51 |
|
52 |
private static final int SQUARE_LENGTH = 100; |
53 |
|
54 |
private Color fillColor; |
55 |
} |
56 |
|