import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.AbstractAction;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Container;

class TrafficLight {
  String state;
  public TrafficLight() { this.state = "red"; }
  public void next() {
    if(this.state.equals("red")) { this.state = "green"; }
    else if(this.state.equals("green")) { this.state = "yellow"; }
    else { this.state = "red"; }
  }
}

class LightChangeAction extends AbstractAction {
  TrafficLight tl;
  JFrame f;
  public LightChangeAction(TrafficLight tl, JFrame f) {
    this.tl = tl;
    this.f = f;
  }
  public void actionPerformed(ActionEvent e) {
    this.tl.next();
    this.f.repaint();
  }
}

class TrafficLightDrawing extends JPanel {
  TrafficLight tl;
  public TrafficLightDrawing(TrafficLight tl) {
    this.setMinimumSize(new Dimension(200, 300));
    this.tl = tl;
  }
  public void paintComponent(Graphics g) {

    // Draw the red light
    if(this.tl.state.equals("red")) {
      g.setColor(Color.RED);
      g.drawOval(70, 45, 60, 60);
    }
    else {
      g.setColor(Color.GRAY);
    }
    g.fillOval(75, 50, 50, 50);

    if(this.tl.state.equals("yellow")) {
      g.setColor(Color.YELLOW);
      g.drawOval(70, 120, 60, 60);
    }
    else {
      g.setColor(Color.GRAY);
    }
    g.fillOval(75, 125, 50, 50);

    if(this.tl.state.equals("green")) {
      g.setColor(Color.GREEN);
      g.drawOval(70, 195, 60, 60);
    }
    else {
      g.setColor(Color.GRAY);
    }
    g.fillOval(75, 200, 50, 50);

  }
}

public class GUIFull {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setMinimumSize(new Dimension(600, 400));
    TrafficLight tl = new TrafficLight();
    TrafficLightDrawing tld = new TrafficLightDrawing(tl);

    JButton button = new JButton(new LightChangeAction(tl, frame));
    button.setText("Advance Light");
    button.setPreferredSize(new Dimension(100, 20));

    frame.add(button, BorderLayout.PAGE_START);
    frame.add(tld, BorderLayout.CENTER);
    frame.setVisible(true);
    frame.repaint();

  }
}
