From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../hjp5/examples/LightBulb.java | 99 ++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 Master/Reference Architectures and Patterns/hjp5/examples/LightBulb.java (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/LightBulb.java') diff --git a/Master/Reference Architectures and Patterns/hjp5/examples/LightBulb.java b/Master/Reference Architectures and Patterns/hjp5/examples/LightBulb.java new file mode 100644 index 0000000..961fef3 --- /dev/null +++ b/Master/Reference Architectures and Patterns/hjp5/examples/LightBulb.java @@ -0,0 +1,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; + } +} \ No newline at end of file -- cgit v1.2.3