Simulation einer Verkehrsampel (Java)

Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springen
Datei:Ampelsimulation.png
Bildschirmausgabe

Die Klasse Ampel.java baut unserer Projekt in einem Fenster zusammen und startet es. <code=java> import javax.swing.*;

/**

* @author Gernot Segieth
*/

public class Ampel {

  public Ampel() {
     try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
     }
     catch(Exception e) {
        System.err.println(e);
     }
     JFrame f = new JFrame("Ampel-Simulation");
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     f.add(new AmpelPanel());
     f.pack();
     f.setLocationRelativeTo(null);
     f.setVisible(true);
  }
  public static void main(String[] args) {
     SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
           new Ampel();
        }
     });
  }

} </code=java>

Tactable.java ist ein Interface, das Klassen implementieren müssen, wenn sie über den Wechsel der Ampelphasen informiert werden möchten. <code=java> /**

* @author Gernot Segieth
*/

public interface Tactable {

  public void next();

} </code=java>

AmpelPanel.java beschreibt die virtuelle Hardware unserer Ampel. Sie zeichnet die Ampel und die Ampelphasen. <code=java> import java.awt.*; import java.awt.event.*;

import javax.swing.*;

/**

* @author Gernot Segieth
*/

public class AmpelPanel extends JPanel implements Tactable {

  private int phase;
  
  public AmpelPanel() {
     setBackground(Color.WHITE);
     Tact tact = new Tact(this);
     tact.start();
  }
  public void next() {
     phase++;
     repaint();
     if(phase > 3) {
        phase = 0;
     }
  }
  
  protected void paintComponent(Graphics g) {
     super.paintComponent(g);
     g.fillRoundRect(10, 10, 200, 600, 10, 10);
        g.setColor(Color.DARK_GRAY);
        g.fillOval(20, 20, 180, 180);
        g.fillOval(20, 220, 180, 180);
        g.fillOval(20, 420, 180, 180);
     
     switch(phase) {
        case 0:
        g.setColor(Color.RED);
        g.fillOval(20, 20, 180, 180);
        break;
        
        case 1:
        g.setColor(Color.RED);
        g.fillOval(20, 20, 180, 180);
        g.setColor(Color.YELLOW);
        g.fillOval(20, 220, 180, 180);
        break;
        
        case 2:
        g.setColor(Color.GREEN);
        g.fillOval(20, 420, 180, 180);
        break;
        
        case 3:
        g.setColor(Color.YELLOW);
        g.fillOval(20, 220, 180, 180);
        break;
     }
  }
  
  public Dimension getPreferredSize() {
     return new Dimension(220, 620);
  }

} </code=java>

Tact.java beschreibt Takte bzw. Wartezeiten, die eine Ampelschaltung einer Verkehrsampel durchläuft. <code=java> /**

* @author Gernot Segieth
*/

public class Tact extends Thread {

  private Tactable tactable;
  public Tact(Tactable tactable) {
     this.tactable = tactable;
  }
  public void run() {
     while(!isInterrupted()) {
        Tact.sleep(8000);
        tactable.next();
        Tact.sleep(2000);
        tactable.next();
        Tact.sleep(10000);
        tactable.next();
        Tact.sleep(5000);
        tactable.next();
     }
  }
  public static void sleep(long millis) {
     try {
        Thread.sleep(millis);
     }
     catch(InterruptedException e) {
        System.err.println(e);
        Thread.currentThread().interrupt();
     }
  }

} </code=java>