import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class trafficLight {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Traffic Light Simulation");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 500);

        TrafficLightPanel panel = new TrafficLightPanel();
        frame.add(panel);

        frame.setVisible(true);
    }
}

class TrafficLightPanel extends JPanel implements ActionListener {
    int lightState = 0;
    JButton redButton, orangeButton, greenButton;

    public TrafficLightPanel() {
        setBackground(Color.white);

        redButton = new JButton("Red");
        orangeButton = new JButton("Orange");
        greenButton = new JButton("Green");

        redButton.addActionListener(this);
        orangeButton.addActionListener(this);
        greenButton.addActionListener(this);

        setLayout(new FlowLayout());
        add(redButton);
        add(orangeButton);
        add(greenButton);
    }

    public void actionPerformed(ActionEvent ae) {
        String command = ae.getActionCommand();
        
        if (command.equals("Red")) {
            lightState = 1;
        } else if (command.equals("Orange")) {
            lightState = 2;
        } else if (command.equals("Green")) {
            lightState = 3;
        }
        
        repaint();
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.black);
        g.drawRect(100, 50, 100, 200);
        g.setColor(Color.white);
        g.fillRect(100, 50, 100, 200);

        g.setColor(Color.black);
        g.drawOval(130, 70, 30, 30);
        g.drawOval(130, 130, 30, 30);
        g.drawOval(130, 190, 30, 30);

        if (lightState == 1) {
            g.setColor(Color.red);
            g.fillOval(130, 70, 30, 30);
        } else if (lightState == 2) {
            g.setColor(Color.orange);
            g.fillOval(130, 130, 30, 30);
        } else if (lightState == 3) {
            g.setColor(Color.green);
            g.fillOval(130, 190, 30, 30);
        }
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: