dh-Materialien
Java Projekte
// JavaProject TrafficLightSystem

// TrafficLightSystem.java

import java.awt.*;
import javax.swing.JFrame;


class CFrame extends JFrame {
    private static final long serialVersionUID = 0L;

    Image icon;

    ControlBox controlbox;

    TrafficLight tl1;
    TrafficLight tl2;

    public CFrame() {
        setTitle("Ampelanlage");
        icon = Toolkit.getDefaultToolkit().getImage("dh.png");
        setIconImage(icon);
        setSize(349, 301);
        getContentPane().setBackground(new Color(240, 240, 240));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setLayout(null);

        tl1 = new TrafficLight(20, 20);
        tl2 = new TrafficLight(100, 20);
        add(tl1);
        add(tl2);

        controlbox = new ControlBox(tl1, tl2);
        add(controlbox);

        setVisible(true);
    }
}

public class TrafficLightSystem {

    public static void main(String[] args) {
        new CFrame();
    }
}



// ControlBox.java

import java.awt.*;
import javax.swing.*;


public class ControlBox extends JPanel implements Runnable {
    private static final long serialVersionUID = 0L;

    JButton b_normal;
    JProgressBar waitingbar;
    JSlider slider;
    JButton b_danger;
    JButton b_emergency;
    JButton b_off;

    Color bgcol = new Color(200, 200, 200);

    Thread thread;
    boolean running;
    boolean normalrun;

    TrafficLight tl1, tl2;
    int period = 40; // Umlaufzeit = 40 Sekunden
    int greentime = 14; // aktuelle Grünphase von tl1 in Sekunden

    enum Mode {START, NORMAL, DANGER, EMERGENCY, OFF};
    Mode mode;

    ControlBox(TrafficLight tl1, TrafficLight tl2) {
        this.tl1 = tl1;
        this.tl2 = tl2;
        setBounds(172, 100, 140, 162);
        setBackground(bgcol);
        setBorder(BorderFactory.createMatteBorder(12, 12, 12, 12, bgcol));
        setLayout(new GridLayout(6, 1, 8, 8));

        b_normal = new JButton("Normalbetrieb");
        add(b_normal);
        waitingbar = new JProgressBar();
        waitingbar.setBackground(Color.WHITE);
        waitingbar.setStringPainted(true);
        waitingbar.setMinimum(0);
        add(waitingbar);
        slider = new JSlider(8, 29, 14);
        add(slider);
        b_danger = new JButton("Gefahr");
        add(b_danger);
        b_emergency = new JButton("Notfall");
        add(b_emergency);
        b_off = new JButton("Aus");
        add(b_off);

        mode = Mode.OFF;
        waitingbar.setString("OFF");

        thread = new Thread(this);
        thread.start();
        running = true;
        normalrun = false;

        b_normal.addActionListener(e ->    runInNormalMode());
        slider.addChangeListener(e ->      changePhase());
        b_danger.addActionListener(e ->    runInDangerMode());
        b_emergency.addActionListener(e -> runInEmergencyMode());
        b_off.addActionListener(e ->       switchOff());
    }

    private void normalwait(int sec) {
        int tmax = 800*sec;
        waitingbar.setMaximum(100);
        for (int t=0; t<tmax; t++) {
            try {
                if (normalrun) waitingbar.setValue((int) (100*(tmax-t)/tmax));
                if (normalrun) Thread.sleep(1);
            }
            catch (InterruptedException e) { }
        }
    }

    private void wait(int sec) {
        try {
            Thread.sleep(sec*1000);
        }
        catch (InterruptedException e) { }
    }

    private void setPhase(String ph1, String ph2) {
        tl1.setPhase(ph1);
        tl2.setPhase(ph2);
        tl1.repaint();
        tl2.repaint();
    }

    private void changePhase() {
        greentime = slider.getValue();
    }

    private void runInEmergencyMode() {
        waitingbar.setString("EMERGENCY");
        normalrun = false;
        waitingbar.setValue(0);
        mode = Mode.EMERGENCY;
    }

    private void runInNormalMode() {
        waitingbar.setString("");
        normalrun = true;
        mode = Mode.START;
    }

    private void runInDangerMode() {
        waitingbar.setString("DANGER");
        normalrun = false;
        waitingbar.setValue(0);
        mode = Mode.DANGER;
    }

    private void switchOff() {
        waitingbar.setString("OFF");
        normalrun = false;
        waitingbar.setValue(0);
        mode = Mode.OFF;
    }

    @Override
    public void run() {
        while (running) {
            while ((normalrun)&&(mode == Mode.START)) {
                if (normalrun) setPhase("red", "red");
                if (normalrun) waitingbar.setForeground(tl1.red);
                if (normalrun) normalwait(3);
                if (normalrun) setPhase("red", "red_yellow");
                if (normalrun) wait(2);
                if (normalrun) setPhase("red", "green");
                if (normalrun) waitingbar.setForeground(tl1.red);
                if (normalrun) normalwait(4);
                if (normalrun) mode = Mode.NORMAL;
            }
            while ((normalrun)&&(mode == Mode.NORMAL)) {
                if (normalrun) setPhase("red_yellow", "yellow");
                if (normalrun) wait(3);
                if (normalrun) setPhase("green", "red");
                if (normalrun) waitingbar.setForeground(tl1.green);
                if (normalrun) normalwait(greentime);
                if (normalrun) setPhase("yellow", "red_yellow");
                if (normalrun) wait(3);
                if (normalrun) setPhase("red", "green");
                if (normalrun) waitingbar.setForeground(tl1.red);
                if (normalrun) normalwait(period-greentime);
            }
            while (mode == Mode.DANGER) {
                setPhase("yellow", "yellow");
                wait(1);
                setPhase("off", "off");
                wait(1);
            }
            while (mode == Mode.EMERGENCY) {
                setPhase("red", "red");
                wait(1);
            }
            while (mode == Mode.OFF) {
                setPhase("off", "off");
                wait(1);
            }
        }
    }
}



// TrafficLight.java

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;
import javax.swing.*;


public class TrafficLight extends JPanel {
    private static final long serialVersionUID = 0L;

    BasicStroke stroke;
    Shape post;
    Shape lightbox;

    Shape lighttop;
    Shape lightmiddle;
    Shape lightbottom;

    Color colortop;
    Color colormiddle;
    Color colorbottom;

    Color red =         new Color(255, 5, 15);
    Color yellow =      new Color(248, 187, 56);
    Color green =       new Color(2, 192, 2);
    Color red_dark =    new Color(31, 21, 21);
    Color yellow_dark = new Color(47, 45, 41);
    Color green_dark =  new Color(19, 25, 19);

    public TrafficLight(int x, int y) {
        setBounds(x, y, 39, 281);
        setBackground(new Color(240, 240, 240));

        stroke =      new BasicStroke(1.0f);
        lightbox =    new Rectangle2D.Float(1, 1, 37, 100);
        post =        new Rectangle2D.Float(17, 100, 7, 181);

        lighttop =    new Ellipse2D.Float(7, 7, 26, 26);
        lightmiddle = new Ellipse2D.Float(7, 38, 26, 26);
        lightbottom = new Ellipse2D.Float(7, 70, 26, 26);

        colortop =    red_dark;
        colormiddle = yellow_dark;
        colorbottom = green_dark;

        setVisible(true);
    }

    private void defineRendering(Graphics2D g) {
        RenderingHints rh;
        rh = new RenderingHints (
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON
        );
        g.setRenderingHints(rh);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        defineRendering(g2d);

        g2d.setPaint(Color.DARK_GRAY);
        g2d.fill(post);
        g2d.setPaint(new Color(5, 5, 5));
        g2d.fill(lightbox);
        g2d.setPaint(Color.BLACK);
        g2d.setStroke(stroke);
        g2d.draw(lightbox);

        g2d.setPaint(colortop);
        g2d.fill(lighttop);
        g2d.setPaint(colormiddle);
        g2d.fill(lightmiddle);
        g2d.setPaint(colorbottom);
        g2d.fill(lightbottom);
    }

    protected void setPhase(String ph) {
        switch(ph) {
            case "red":
                colortop =    red;
                colormiddle = yellow_dark;
                colorbottom = green_dark;
                break;
            case "red_yellow":
                colortop =    red;
                colormiddle = yellow;
                colorbottom = green_dark;
                break;
            case "green":
                colortop =    red_dark;
                colormiddle = yellow_dark;
                colorbottom = green;
                break;
            case "yellow":
                colortop =    red_dark;
                colormiddle = yellow;
                colorbottom = green_dark;
                break;
            case "off":
                colortop =    red_dark;
                colormiddle = yellow_dark;
                colorbottom = green_dark;
                break;
            default:
                colortop =    red_dark;
                colormiddle = yellow_dark;
                colorbottom = green_dark;
        }
    }
}

Download TrafficLightSystem