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/Listing3410.java | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Master/Reference Architectures and Patterns/hjp5/examples/Listing3410.java (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing3410.java') diff --git a/Master/Reference Architectures and Patterns/hjp5/examples/Listing3410.java b/Master/Reference Architectures and Patterns/hjp5/examples/Listing3410.java new file mode 100644 index 0000000..c65b520 --- /dev/null +++ b/Master/Reference Architectures and Patterns/hjp5/examples/Listing3410.java @@ -0,0 +1,69 @@ +/* Listing3410.java */ + +import java.awt.*; +import java.awt.event.*; + +public class Listing3410 +extends Frame +implements Runnable +{ + //Konstanten + private static final int NUMLEDS = 20; + private static final int SLEEP = 60; + private static final int LEDSIZE = 10; + private static final Color ONCOLOR = new Color(255,0,0); + private static final Color OFFCOLOR = new Color(100,0,0); + + //Instanzvariablen + private Thread th; + private int switched; + private int dx; + + public static void main(String[] args) + { + Listing3410 frame = new Listing3410(); + frame.setSize(270,150); + frame.setVisible(true); + frame.startAnimation(); + } + + public Listing3410() + { + super("Leuchtdiodenkette"); + setBackground(Color.lightGray); + addWindowListener(new WindowClosingAdapter(true)); + } + + public void startAnimation() + { + th = new Thread(this); + th.start(); + } + + public void run() + { + switched = -1; + dx = 1; + while (true) { + repaint(); + try { + Thread.sleep(SLEEP); + } catch (InterruptedException e){ + //nichts + } + switched += dx; + if (switched < 0 || switched > NUMLEDS - 1) { + dx = -dx; + switched += 2*dx; + } + } + } + + public void paint(Graphics g) + { + for (int i = 0; i < NUMLEDS; ++i) { + g.setColor(i == switched ? ONCOLOR : OFFCOLOR); + g.fillOval(10+i*(LEDSIZE+2),80,LEDSIZE,LEDSIZE); + } + } +} \ No newline at end of file -- cgit v1.2.3