Results 1 to 4 of 4
  1. #1
    DaveMc is offline Member
    Join Date
    Dec 2012
    Location
    Kerry, Ireland
    Posts
    9
    Rep Power
    0

    Default Change Circle Color on timer

    Hey There! :)

    So I've been trying to code this GUI using Swing, a mock traffic lights setup, just to introduce myself to Swing and Java in general. I've got it to the point where the Red Light appears for 2 seconds, but the other lights don't come on (i.e. the circles don't change colour for me). I'm pretty sure I am handling the timer section and repainting incorrectly but not sure how to resolve. My MainFrame class is just a standard JFrame and it has a LightsPanel on it, which in turn is just a JPanel with some circles drawn onto it. Would anyone be able to help me with changing the colour of the circles based on a 2 second time delay? Many thanks in advance :)

    App.java
    Java Code:
    package mac;
    
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class App {
    
    	public static void main(String[] args) {
    
    		SwingUtilities.invokeLater(new Runnable() {
    			@Override
    			public void run() {
    				MainFrame frame;
    				try {
    
    					frame = new MainFrame();
    					frame.setSize(300, 600);
    					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    					frame.setLocationRelativeTo(null);
    					frame.setVisible(true);
    
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    
    			}
    
    		});
    	}
    }

    MainFrame.java
    Java Code:
    package mac;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.Timer;
    
    public class MainFrame extends JFrame {
    
    	private LightsPanel panel;
    
    	public MainFrame() throws InterruptedException {
    		super("Traffic Lights");
    		this.setLayout(new BorderLayout());
    
    		panel = new LightsPanel();
    		panel.setBackground(Color.WHITE);
    		
    		this.add(panel, BorderLayout.CENTER);
    		
    		Timer timer = new Timer(2000, new ActionListener(){
    			@Override
    			public void actionPerformed(ActionEvent e) { 
    				panel.switchAmber();
    				panel.switchGreen();
    				panel.switchRed();
    			}
    		});
    		
    		timer.setRepeats(false);
    		timer.start();
    	}
    
    }

    LightsPanel.java
    Java Code:
    package mac;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    
    import javax.swing.JPanel;
    
    public class LightsPanel extends JPanel {
    
    	private Color red;
    	private Color green;
    	private Color amber;
    	
    	public LightsPanel() {
    		this.setLayout(new BorderLayout());
    		
    		this.red = Color.BLACK;
    		this.amber = Color.BLACK;
    		this.green = Color.BLACK;
    		repaint();
    		
    	}
    
    	public void switchRed() {
    		this.red = Color.RED;
    		this.green = Color.BLACK;
    		this.amber = Color.BLACK;
    
    		repaint();
    	}
    
    	public void switchGreen() {
    		this.red = Color.BLACK;
    		this.amber = Color.BLACK;
    		this.green = Color.GREEN;
    
    		repaint();
    
    	}
    
    	public void switchAmber() {
    		this.red = Color.BLACK;
    		this.green = Color.BLACK;
    		this.amber = Color.ORANGE;
    
    		repaint();
    
    	}
    
    	public void paintComponent(Graphics graphics) {
    		super.paintComponent(graphics);
    		
    		graphics.setColor(red);
    		graphics.fillOval(100, 100, 100, 100);
    
    		graphics.setColor(amber);
    		graphics.fillOval(100, 200, 100, 100);
    
    		graphics.setColor(green);
    		graphics.fillOval(100, 300, 100, 100);
    
    		graphics.setColor(Color.BLACK);
    		graphics.drawRect(80, 30, 140, 450);
    	}
    
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Change Circle Color on timer

    Java Code:
    public void actionPerformed(ActionEvent e) { 
        panel.switchAmber();
        panel.switchGreen();
        panel.switchRed();
    }
    This will set the panel to amber then green then red, all before any redraw happens, every two seconds.

    You need some form of state for the panel which you can check and use to determine what the new state (ie colour) should be.

    Then you can skip all the red/amber/green stuff in the paint code.
    You just need to know what colour to paint.

    I would suggest an enum, which can hold the nextState as well as a Color that you can use for your painting.

    (Hope that wasn't too much babbling!)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Change Circle Color on timer

    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Change Circle Color on timer

    Yeah, I noticed that...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Drawing a circle with inner and outer color?
    By totalspelnerd in forum Java 2D
    Replies: 4
    Last Post: 06-10-2013, 10:09 AM
  2. change color
    By faraa in forum New To Java
    Replies: 3
    Last Post: 05-19-2013, 05:22 PM
  3. Change color with hex code?
    By boostshock in forum New To Java
    Replies: 1
    Last Post: 09-03-2012, 09:31 PM
  4. How can I change a timer's delay?
    By chrishans in forum New To Java
    Replies: 7
    Last Post: 11-08-2011, 05:07 PM
  5. change thread.sleep() to timer.
    By MaximumDolphin in forum Java Applets
    Replies: 16
    Last Post: 04-15-2011, 12:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •