document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. /**
  3.  * Kode untuk menampilkan gambaran utuh sebuah lampu lalu lintas
  4.  * Kode ini akan menentukan lampu mana yang akan menyala dan berapa lama lampu tersebut akan menyala
  5.  */
  6.  import java.awt.*;  
  7.  import javax.swing.*;  
  8.  import java.awt.event.*;  
  9.  
  10.  public class TrafficLightPane extends JPanel
  11.  {  
  12.    int tick = 1;  
  13.    int duration = 10;  
  14.    int state = 0;  
  15.    
  16.    SignalPane green = new SignalPane(Color.green);  
  17.    SignalPane yellow = new SignalPane(Color.yellow);  
  18.    SignalPane red = new SignalPane(Color.red);  
  19.    DigitPane timerDigit = new DigitPane();  
  20.    
  21.    public TrafficLightPane(int s)
  22.    {  
  23.      duration = s;  
  24.      setLayout(new GridLayout(4,1));  
  25.      green.turnOn(false);  
  26.      yellow.turnOn(false);  
  27.      red.turnOn(true);  
  28.      timerDigit.setValue(duration);  
  29.      add(red);  
  30.      add(yellow);  
  31.      add(green);  
  32.      add(timerDigit);  
  33.      Timer timer = new Timer(1000, new ActionListener()
  34.      {  
  35.        public void actionPerformed(ActionEvent e)
  36.        {  
  37.          int timeRemaining = duration - tick;  
  38.          
  39.          if (timeRemaining <= 0)
  40.          {  
  41.            tick = 0;  
  42.            state++;  
  43.            changeSignalState(state);  
  44.          }  
  45.          timerDigit.setValue(duration - tick);  
  46.          tick++;  
  47.        }  
  48.      });  
  49.      timer.setRepeats(true);  
  50.      timer.setCoalesce(true);  
  51.      timer.start();  
  52.    }  
  53.    
  54.    private boolean changeToBool(int state){  
  55.      if (state % 3 > 0 )
  56.      {  
  57.        return false;  
  58.      }
  59.      else
  60.      {  
  61.        return true;  
  62.      }  
  63.    }  
  64.  
  65.    private void changeSignalState(int state)
  66.    {  
  67.      green.turnOn(changeToBool(state + 2));  
  68.      yellow.turnOn(changeToBool(state + 1));  
  69.      red.turnOn(changeToBool(state));  
  70.    }  
  71.    
  72.    public void setDuration(int s)
  73.    {  
  74.      duration = s;  
  75.    }  
  76.  }
');