Advertisement
Guest User

FUSSGAENGERAMPEL_10b

a guest
Feb 18th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. class FUSSGAENGERAMPEL
  2. {
  3.     int positionX = 0;
  4.     int positionY = 0;
  5.     String ampelphase;
  6.     char ausrichtung;
  7.     LAMPE lampeOben;
  8.     LAMPE lampeUnten;
  9.  
  10.     FUSSGAENGERAMPEL ()
  11.     {
  12.         positionX = 1;
  13.         positionY = 1;
  14.         ampelphase = "rot";
  15.         ausrichtung = 'S';
  16.        
  17.         lampeOben = new LAMPE();
  18.         lampeUnten = new LAMPE();
  19.         RotSetzen();
  20.         AmpelAnordnen();
  21.     }
  22.    
  23.     FUSSGAENGERAMPEL (int x, int y, char dir) {  
  24.         lampeOben = new LAMPE();
  25.         lampeUnten = new LAMPE();
  26.         RotSetzen();
  27.         AusrichtungSetzen(dir);
  28.         PositionSetzen(x, y);
  29.         AmpelAnordnen();
  30.     }
  31.    
  32.     void GruenSetzen ()
  33.     {
  34.         ampelphase = "gruen";
  35.         lampeOben.FarbeSetzen("schwarz");
  36.         lampeUnten.FarbeSetzen("gruen");
  37.     }
  38.  
  39.     void RotSetzen ()
  40.     {
  41.         ampelphase = "rot";
  42.         lampeOben.FarbeSetzen("rot");
  43.         lampeUnten.FarbeSetzen("schwarz");
  44.     }
  45.    
  46.     /**
  47.      * Setzt die Position der Ampel.
  48.      * Der Ursprung liegt in der Mitte des
  49.      * Fensters, die y-Achse zeigt nach unten. (x /y) bedeutet das
  50.      * Kästchen rechts unterhalb der Gitterlinien.
  51.      * @param x xNeu-Position
  52.      * @param y yNeu-Position
  53.      */
  54.     void PositionSetzen (int xNeu, int yNeu)
  55.     {
  56.         positionX = xNeu;
  57.         positionY = yNeu;
  58.         AmpelAnordnen();
  59.     }
  60.    
  61.     /**
  62.      * Setzt die Ausrichtung der Ampel auf die möglichen
  63.      * Werte S, W, N, O.
  64.      * @param ausrichtungNeu neue Ausrichtung der Ampel
  65.      */
  66.     void AusrichtungSetzen(char ausrichtungNeu)
  67.     {
  68.         ausrichtung = Character.toUpperCase(ausrichtungNeu);
  69.         AmpelAnordnen();
  70.     }
  71.    
  72.     /**
  73.      * Zeichnet die Ampel im Raster.
  74.      */
  75.     private void AmpelAnordnen()
  76.     {
  77.          switch ( ausrichtung )
  78.          {
  79.              case 'S':
  80.                 lampeOben.PositionSetzen(positionX, positionY);
  81.                 lampeUnten.PositionSetzen(positionX, positionY + 1);
  82.                
  83.                 break;
  84.              case 'W':
  85.                 lampeOben.PositionSetzen(positionX, positionY);
  86.                 lampeUnten.PositionSetzen(positionX - 1, positionY);
  87.                 break;
  88.              case 'N':
  89.                 lampeOben.PositionSetzen(positionX, positionY);
  90.                 lampeUnten.PositionSetzen(positionX, positionY - 1);
  91.                 break;
  92.              case 'O':
  93.                 lampeOben.PositionSetzen(positionX, positionY);
  94.                 lampeUnten.PositionSetzen(positionX + 1, positionY);
  95.                 break;
  96.              default:
  97.                 System. out. println ("Unzulässige Ausrichtung: " +  ausrichtung);
  98.                 break;
  99.          }  
  100.     }
  101.  
  102.    
  103.     void Weiterschalten()
  104.     {
  105.         if (ampelphase == "rot")
  106.         {
  107.             GruenSetzen();
  108.         }
  109.         else
  110.         {
  111.             RotSetzen();
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement