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
- /**
- * Class untuk menggambar lampu lalu lintas
- */
- import java.awt.*;
- import javax.swing.*;
- class SignalPane extends JPanel
- {
- Color on;
- int radius = 50;
- int border = 10;
- boolean isOn;
- SignalPane(Color color)
- {
- on = color;
- isOn = false;
- }
- public void turnOn(boolean a)
- {
- isOn = a;
- repaint();
- }
- public Dimension getPrefereedSize()
- {
- int size = (radius+border)*2;
- return new Dimension(size, size);
- }
- protected void paintComponent(Graphics graphics)
- {
- graphics.setColor(Color.black);
- graphics.fillRect(0, 0, getWidth(), getHeight());
- if (isOn)
- {
- graphics.setColor(on);
- }
- else
- {
- graphics.setColor(on.darker().darker().darker());
- }
- graphics.fillOval(border, border, 2*radius, 2*radius);
- }
- }
- /**
- * Write a description of class TrafficLightPane here.
- *
- * @author Sabrina Lydia S
- * @version 19/10/2020
- */
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- public class TrafficLightPane extends JPanel
- {
- int tick = 1;
- int duration = 60;
- int state = 0;
- SignalPane green = new SignalPane(Color.green);
- SignalPane yellow = new SignalPane(Color.yellow);
- SignalPane red = new SignalPane(Color.red);
- DigitPane timerDigit = new DigitPane();
- public TrafficLightPane (int s)
- {
- duration = s;
- setLayout(new GridLayout(4, 1));
- green.turnOn(false);
- yellow.turnOn(false);
- red.turnOn(true);
- timerDigit.setValue(duration);
- add(red);
- add(yellow);
- add(green);
- add(timerDigit);
- Timer timer = new Timer(1000, new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- int timeRemaining = duration - tick;
- if (timeRemaining <= 0)
- {
- tick = 0;
- state++;
- changeSignalState(state);
- }
- timerDigit.setValue(duration - tick);
- tick++;
- }
- });
- timer.setRepeats(true);
- timer.setCoalesce(true);
- timer.start();
- }
- private boolean changeToBool(int state)
- {
- if (state % 3 > 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- private void changeSignalState(int state)
- {
- green.turnOn(changeToBool(state + 2));
- yellow.turnOn(changeToBool(state + 1));
- red.turnOn(changeToBool(state));
- }
- public void setDuration(int s)
- {
- duration = s;
- }
- }
- /**
- * Write a description of class DigitPane here.
- *
- * @author Sabrina Lydia S
- * @version 19/10/2020
- */
- import java.awt.*;
- import javax.swing.*;
- public class DigitPane extends JPanel
- {
- private int second;
- public Dimension getPreferredSize()
- {
- FontMetrics fm = getFontMetrics(getFont());
- return new Dimension(fm.stringWidth("00"), fm.getHeight());
- }
- public void setValue(int newVal)
- {
- if (second != newVal)
- {
- second = newVal;
- repaint();
- }
- }
- public int getValue()
- {
- return second;
- }
- private String pad(int value)
- {
- return String.format("%02d", value);
- }
- protected void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- g.setFont(new Font("LCD", Font.PLAIN, 24));
- FontMetrics fm = getFontMetrics(g.getFont());
- String text = pad(getValue());
- int x = (getWidth() - fm.stringWidth(text)) / 2;
- int y = ((getHeight()- fm.getHeight()) / 2) + fm.getAscent();
- g.drawString(text, x, y);
- }
- }
- /**
- * Write a description of class SignalPane here.
- *
- * @author Sabrina Lydia S
- * @version 19/10/2020
- */
- import java.awt.*;
- import javax.swing.*;
- class SignalPane extends JPanel
- {
- Color on;
- int radius = 60;
- int border = 12;
- boolean isOn;
- SignalPane(Color color)
- {
- on = color;
- isOn = false;
- }
- public void turnOn(boolean a)
- {
- isOn = a;
- repaint();
- }
- public Dimension getPreferredSize()
- {
- int size = (radius+border)*2;
- return new Dimension( size, size );
- }
- protected void paintComponent(Graphics graphics)
- {
- graphics.setColor( Color.black );
- graphics.fillRect(0,0,getWidth(),getHeight());
- if (isOn)
- {
- graphics.setColor( on );
- }
- else
- {
- graphics.setColor( on.darker().darker().darker() );
- }
- graphics.fillOval( border,border,2*radius,2*radius );
- }
- }
4. Output
Komentar
Posting Komentar