I am trying to create a traffic light using a Java GUI, where it displays only one circle and it changes colours from red, to yellow, to green. There should be a timer and only yellow should be changing to green within 3 seconds. I have set up a circle and a colour red, but I am unable to change it to the colours yellow, and green respectively using a timer.
Btw I am really new to GUI and could not find helpful sources online, although I still watched a couple of youtube videos but did not find anything useful or relevant to this task. Any help would be much appreciated!
Code:
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
public class Main extends Canvas {
public static void main(String[] args) {
JFrame frame = new JFrame();
Canvas canvas = new Main();
canvas.setSize(700, 700);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
}
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillOval(200, 200, 300, 300);
}
}
Expected Output:
It should be only one circle