|
Lens |
|
1 // joi/1/lights/Lens.java
2 //
3 //
4 // Copyright 2003 Bill Campbell and Ethan Bolker
5
6 import java.awt.*;
7
8 /**
9 * A Lens has a certain color and can either be turned on
10 * (the color) or turned off (black).
11 *
12 * @version 1
13 */
14
15 public class Lens extends Canvas
16 {
17 private Color onColor; // color on
18 private Color offColor = Color.black; // color off
19 private Color currentColor; // color the lens is now
20
21 private final static int SIZE = 100; // how big is this Lens?
22 private final static int OFFSET = 20; // offset of Lens in Canvas
23
24 /**
25 * Construct a Lens to display a given color.
26 *
27 * The lens is black when it's turned off.
28 *
29 * @param color the color of the lens when it is turned on.
30 */
31
32 public Lens( Color color )
33 {
34 this.setBackground( Color.black );
35 this.onColor = color;
36 this.setSize( SIZE , SIZE );
37 this.turnOff();
38 }
39
40 /**
41 * How this Lens paints itself.
42 *
43 * @param g a Graphics object to manage brush and color information.
44 */
45
46 public void paint( Graphics g )
47 {
48 g.setColor( this.currentColor );
49 g.fillOval( OFFSET, OFFSET,
50 SIZE - OFFSET*2, SIZE - OFFSET*2 );
51 }
52
53 /**
54 * Have this Lens display its color.
55 */
56
57 public void turnOn()
58 {
59 currentColor = onColor;
60 this.repaint();
61 }
62
63 /**
64 * Darken this lens.
65 */
66
67 public void turnOff()
68 {
69 currentColor = offColor;
70 this.repaint();
71 }
72 }
73
|
Lens |
|