Simulation einer Verkehrsampel (Java): Unterschied zwischen den Versionen

Aus Byte-Welt Wiki
KKeine Bearbeitungszusammenfassung
KKeine Bearbeitungszusammenfassung
 
(2 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
[[Datei:Ampelsimulation.png|mini|Bildschirmausgabe]]
[[Datei:Ampelsimulation.gif|mini|Bildschirmausgabe]]
Die Klasse ''Ampel.java'' baut unserer Projekt in einem Fenster zusammen und startet es.
Die Klasse ''Ampel.java'' baut unserer Projekt in einem Fenster zusammen und startet es.
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
Zeile 125: Zeile 125:
   public void run() {
   public void run() {
       while(!isInterrupted()) {
       while(!isInterrupted()) {
         Tact.sleep(8000);
         try {
        tactable.next();
            Tact.sleep(5000);
        Tact.sleep(2000);
            tactable.next();
        tactable.next();
            Tact.sleep(2000);
        Tact.sleep(10000);
            tactable.next();
        tactable.next();
            Tact.sleep(5000);
        Tact.sleep(5000);
            tactable.next();
        tactable.next();
            Tact.sleep(3000);
      }
            tactable.next();
  }
        }
 
         catch (InterruptedException e) {
  public static void sleep(long millis) {
            System.err.println("Fehler bei CPU-Freigabe\n" + e.getLocalizedMessage());
      try {
        }
         Thread.sleep(millis);
      }
      catch(InterruptedException e) {
        System.err.println(e);
        Thread.currentThread().interrupt();
       }
       }
   }
   }

Aktuelle Version vom 7. Dezember 2018, 09:14 Uhr

Bildschirmausgabe

Die Klasse Ampel.java baut unserer Projekt in einem Fenster zusammen und startet es.

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();
         }
      });
   }
}

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

/**
 * @author Gernot Segieth
 */
public interface Tactable {
   public void next();
}

AmpelPanel.java beschreibt die virtuelle Hardware unserer Ampel. Sie zeichnet die Ampel und die Ampelphasen.

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);
   }
}

Tact.java beschreibt Takte bzw. Wartezeiten, die eine Ampelschaltung einer Verkehrsampel durchläuft.

/**
 * @author Gernot Segieth
 */
public class Tact extends Thread {
   private Tactable tactable;

   public Tact(Tactable tactable) {
      this.tactable = tactable;
   }

   public void run() {
      while(!isInterrupted()) {
         try {
            Tact.sleep(5000);
            tactable.next();
            Tact.sleep(2000);
            tactable.next();
            Tact.sleep(5000);
            tactable.next();
            Tact.sleep(3000);
            tactable.next();
         }
         catch (InterruptedException e) {
            System.err.println("Fehler bei CPU-Freigabe\n" + e.getLocalizedMessage());
         }
      }
   }
}


Das Thema wurde nicht ausreichend behandelt? Du hast Fragen dazu und brauchst weitere Informationen? Lass Dir von uns helfen!

  • Besuche unseren Chat

Wir helfen dir gerne!