th0m45s5helby

traffic light

Dec 12th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. /*
  2. <applet code="Job1.class" width=500 height=1000>
  3. </applet>
  4. */
  5.  
  6.  
  7.  
  8. import java.awt.*;
  9. import javax.swing.*;
  10. import java.awt.event.*;
  11. import java.applet.*;
  12.  
  13.  
  14. public class TrafficLight extends JFrame implements ActionListener {
  15.     JButton buttonRed, buttonYellow, buttonGreen;
  16.  
  17.       Signal green = new Signal(Color.green);
  18.       Signal yellow = new Signal(Color.yellow);
  19.       Signal red = new Signal(Color.red);
  20.  
  21.     public TrafficLight(){
  22.         super("Java Traffic Light Program");
  23.         getContentPane().setLayout(new GridLayout(2, 1));
  24.         buttonRed = new JButton("Red");
  25.         buttonYellow = new JButton("Yellow");
  26.         buttonGreen = new JButton("Green");
  27.         buttonRed.addActionListener(this);
  28.         buttonYellow.addActionListener(this);
  29.         buttonGreen.addActionListener(this);      
  30.  
  31.         green.turnOn(false);
  32.         yellow.turnOn(false);
  33.         red.turnOn(true);
  34.  
  35.         JPanel trafficPanel = new JPanel(new GridLayout(3,1));
  36.         trafficPanel.add(red);
  37.         trafficPanel.add(yellow);
  38.         trafficPanel.add(green);
  39.         JPanel lightPanel = new JPanel(new FlowLayout());
  40.         lightPanel.add(buttonRed);
  41.         lightPanel.add(buttonYellow);
  42.         lightPanel.add(buttonGreen);
  43.  
  44.         getContentPane().add(trafficPanel);
  45.         getContentPane().add(lightPanel);
  46.         pack();
  47.         }
  48.  
  49.  
  50.     public static void main(String[] args){
  51.         TrafficLight trafficLight = new TrafficLight();      
  52.         trafficLight.setVisible(true);
  53.     }  
  54.     public void actionPerformed(ActionEvent e){      
  55.         if (e.getSource() == buttonRed){
  56.             green.turnOn(false);          
  57.             yellow.turnOn(false);
  58.             red.turnOn(true);
  59.         } else if (e.getSource() == buttonYellow){
  60.             yellow.turnOn(true);          
  61.             green.turnOn(false);
  62.             red.turnOn(false);
  63.         } else if (e.getSource() == buttonGreen){
  64.             red.turnOn(false);          
  65.             yellow.turnOn(false);
  66.             green.turnOn(true);
  67.         }
  68.     }
  69. }  
  70. class Signal extends JPanel{
  71.  
  72.     Color on;
  73.     int radius = 40;
  74.     int border = 10;
  75.     boolean change;
  76.  
  77.     Signal(Color color){
  78.         on = color;
  79.         change = true;
  80.     }
  81.  
  82.     public void turnOn(boolean a){
  83.         change = a;
  84.         repaint();      
  85.     }
  86.  
  87.     public Dimension getPreferredSize(){
  88.         int size = (radius+border)*2;
  89.         return new Dimension( size, size );
  90.     }
  91.  
  92.     public void paintComponent(Graphics graphics){
  93.         graphics.setColor( Color.black );
  94.         graphics.fillRect(0,0,getWidth(),getHeight());
  95.  
  96.         if (change){
  97.             graphics.setColor( on );
  98.         } else {
  99.             graphics.setColor( on.darker().darker().darker() );
  100.         }
  101.         graphics.fillOval( border,border,2*radius,2*radius );
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment