Tugas Simulasi Traffic Light

   Project berikut merupakan simulasi dari lampu lalu lintas. Sebagaimana yang kita tahu, lampu lalu lintas terdiri dari tiga warna yaitu merah, kuning, dan hijau, dan menyala bergantian dengan durasi tertentu. Dalam kasus ini, saya membuat durasi 10 detik untuk masing-masing lampu. Dimana, ketika suatu lampu sudah menyala selama 10 detik, maka lampu tersebut akan mati dan digantikan oleh lampu berwarna lain, dengan urutan merah-hijau-kuning dan akan berulang. Disini, saya memberi output dalam bentuk grafis  dengan menggunakan Graphical Unit Interface (GUI), yaitu java swing.


1.  Diagram Class

   



2. Diagram Object


3. Implementasi dalam Source Code


AppFrame

  1.  /**
  2.  * Class untuk menggambar lampu lalu lintas
  3.  */
  4.  
  5. import java.awt.*;  
  6. import javax.swing.*;
  7. class SignalPane extends JPanel
  8. {
  9.     Color on;
  10.     int radius = 50;
  11.     int border = 10;
  12.     boolean isOn;
  13.    
  14.     SignalPane(Color color)
  15.     {
  16.         on = color;
  17.         isOn = false;
  18.     }
  19.    
  20.     public void turnOn(boolean a)
  21.     {
  22.         isOn = a;
  23.         repaint();
  24.     }
  25.    
  26.     public Dimension getPrefereedSize()
  27.     {
  28.         int size = (radius+border)*2;
  29.         return new Dimension(size, size);
  30.     }
  31.    
  32.     protected void paintComponent(Graphics graphics)
  33.     {
  34.         graphics.setColor(Color.black);
  35.         graphics.fillRect(0, 0, getWidth(), getHeight());
  36.        
  37.         if (isOn)
  38.         {
  39.             graphics.setColor(on);
  40.         }
  41.         else
  42.         {
  43.             graphics.setColor(on.darker().darker().darker());
  44.         }
  45.      
  46.        
  47.         graphics.fillOval(border, border, 2*radius, 2*radius);
  48.     }
  49. }

TrafficLightPane

  1. /**
  2. * Write a description of class TrafficLightPane here.
  3. *
  4. * @author Sabrina Lydia S
  5. * @version 19/10/2020
  6. */
  7. import java.awt.*;
  8. import javax.swing.*;
  9. import java.awt.event.*;
  10. public class TrafficLightPane extends JPanel
  11. {
  12. int tick = 1;
  13. int duration = 60;
  14. int state = 0;
  15. SignalPane green = new SignalPane(Color.green);
  16. SignalPane yellow = new SignalPane(Color.yellow);
  17. SignalPane red = new SignalPane(Color.red);
  18. DigitPane timerDigit = new DigitPane();
  19.  
  20. public TrafficLightPane (int s)
  21. {
  22. duration = s;
  23. setLayout(new GridLayout(4, 1));
  24. green.turnOn(false);
  25. yellow.turnOn(false);
  26. red.turnOn(true);
  27. timerDigit.setValue(duration);
  28. add(red);
  29. add(yellow);
  30. add(green);
  31. add(timerDigit);
  32. Timer timer = new Timer(1000, new ActionListener() {
  33. public void actionPerformed(ActionEvent e)
  34. {
  35. int timeRemaining = duration - tick;
  36.  
  37. if (timeRemaining <= 0)
  38. {
  39. tick = 0;
  40. state++;
  41. changeSignalState(state);
  42. }
  43.  
  44. timerDigit.setValue(duration - tick);
  45. tick++;
  46. }
  47. });
  48. timer.setRepeats(true);
  49. timer.setCoalesce(true);
  50. timer.start();
  51. }
  52.  
  53.  
  54. private boolean changeToBool(int state)
  55. {
  56. if (state % 3 > 0)
  57. {
  58. return false;
  59. }
  60. else
  61. {
  62. return true;
  63. }
  64. }
  65.  
  66. private void changeSignalState(int state)
  67. {
  68. green.turnOn(changeToBool(state + 2));
  69. yellow.turnOn(changeToBool(state + 1));
  70. red.turnOn(changeToBool(state));
  71. }
  72.  
  73. public void setDuration(int s)
  74. {
  75. duration = s;
  76. }
  77. }

DigitPane

  1. /**
  2. * Write a description of class DigitPane here.
  3. *
  4. * @author Sabrina Lydia S
  5. * @version 19/10/2020
  6. */
  7. import java.awt.*;
  8. import javax.swing.*;
  9.  
  10. public class DigitPane extends JPanel
  11. {
  12. private int second;
  13.  
  14. public Dimension getPreferredSize()
  15. {
  16. FontMetrics fm = getFontMetrics(getFont());
  17. return new Dimension(fm.stringWidth("00"), fm.getHeight());
  18. }
  19.  
  20. public void setValue(int newVal)
  21. {
  22. if (second != newVal)
  23. {
  24. second = newVal;
  25. repaint();
  26. }
  27. }
  28. public int getValue()
  29. {
  30. return second;
  31. }
  32.  
  33. private String pad(int value)
  34. {
  35. return String.format("%02d", value);
  36. }
  37.  
  38. protected void paintComponent(Graphics g)
  39. {
  40. super.paintComponent(g);
  41. g.setFont(new Font("LCD", Font.PLAIN, 24));
  42. FontMetrics fm = getFontMetrics(g.getFont());
  43. String text = pad(getValue());
  44. int x = (getWidth() - fm.stringWidth(text)) / 2;
  45. int y = ((getHeight()- fm.getHeight()) / 2) + fm.getAscent();
  46. g.drawString(text, x, y);
  47. }
  48. }

SignalPane

  1. /**
  2. * Write a description of class SignalPane here.
  3. *
  4. * @author Sabrina Lydia S
  5. * @version 19/10/2020
  6. */
  7. import java.awt.*;
  8. import javax.swing.*;
  9.  
  10. class SignalPane extends JPanel
  11. {
  12. Color on;
  13. int radius = 60;
  14. int border = 12;
  15. boolean isOn;
  16.  
  17. SignalPane(Color color)
  18. {
  19. on = color;
  20. isOn = false;
  21. }
  22.  
  23. public void turnOn(boolean a)
  24. {
  25. isOn = a;
  26. repaint();
  27. }
  28.  
  29. public Dimension getPreferredSize()
  30. {
  31. int size = (radius+border)*2;
  32. return new Dimension( size, size );
  33. }
  34.  
  35. protected void paintComponent(Graphics graphics)
  36. {
  37. graphics.setColor( Color.black );
  38. graphics.fillRect(0,0,getWidth(),getHeight());
  39. if (isOn)
  40. {
  41. graphics.setColor( on );
  42. }
  43. else
  44. {
  45. graphics.setColor( on.darker().darker().darker() );
  46. }
  47. graphics.fillOval( border,border,2*radius,2*radius );
  48. }
  49. }

4. Output



Komentar

Postingan populer dari blog ini

Ticket Machine

ETS PWEB-A

Tugas 10 PBKK