blob: 961fef33e505c3aebea71e37232cb34f5caa739f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
/* LightBulb.java */
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.beans.*;
public class LightBulb
extends Canvas
implements Serializable
{
//Instanzvariablen
protected boolean lighton;
transient protected Image offimage;
transient protected Image onimage;
//Methoden
public LightBulb()
{
lighton = false;
initTransientState();
}
//Getter/Setter Licht an/aus
public void setLightOn(boolean on)
{
if (on != this.lighton) {
this.lighton = on;
repaint();
}
}
public boolean getLightOn()
{
return this.lighton;
}
public void toggleLight()
{
setLightOn(!getLightOn());
}
//Implementierung der Oberfl�che
public void paint(Graphics g)
{
int width = getSize().width;
int height = getSize().height;
int xpos = 0;
if (width > 40) {
xpos = (width - 40) / 2;
}
int ypos = 0;
if (height > 40) {
ypos = (height - 40) / 2;
}
g.drawImage(
(this.lighton ? onimage : offimage),
xpos,
ypos,
this
);
}
public Dimension getPreferredSize()
{
return new Dimension(40, 40);
}
public Dimension getMinimumSize()
{
return new Dimension(40, 40);
}
//Private Methoden
private void initTransientState()
{
offimage = getImageResource("bulb1.gif");
onimage = getImageResource("bulb2.gif");
}
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException
{
stream.defaultReadObject();
initTransientState();
}
private Image getImageResource(String name)
{
Image img = null;
try {
java.net.URL url = getClass().getResource(name);
img = getToolkit().createImage(url);
} catch (Exception e) {
System.err.println(e.toString());
}
return img;
}
}
|