Results 1 to 6 of 6
  1. #1
    Abdo is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default setting up java timer

    I tried everything I know to add timer to me traffic light Java program, nothing works, I want it to be both manual and auto, I already toke care of the manual, can't do the auto timer part, I just want to know how to make it after every x seconds turns from green to red, and red to green..

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    class Signal extends JPanel{
    
       private boolean change;
       private Color on;
       private int r1 = 70;
       private int r2 = 10;
    
    
       Signal(Color color){
          on = color;
          change = true;
       }
    
       public void turnOn(boolean a){
          change = a;
          repaint(); 
       }
    
       public Dimension getPreferredSize(){
          int size = (r1+r2)*2;
          return new Dimension(size,size);
       }
    
       public void paintComponent(Graphics g){
          g.setColor(Color.black);
          g.fillRect(0,0,getWidth(),getHeight());
    
          if (change){
             g.setColor(on);
          } else {
             g.setColor(on.darker().darker().darker()…
          }
    
          g.fillOval(r2,r2,2*r1,2*r1);
       }
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    
    public class TrafficLight extends JFrame implements ActionListener {
       JButton button1, button2;
       Toolkit toolkit;
       Timer timer;
       Signal green = new Signal(Color.green);
       Signal red = new Signal(Color.red);
    
       public TrafficLight(){
          super("Traffic Light");
          getContentPane().setLayout(new GridLayout(2, 1));
          button1 = new JButton("Red");
          button2 = new JButton("Green");
          button1.addActionListener(this);
          button2.addActionListener(this); 
    
          green.turnOn(false);
          red.turnOn(true);
    
          JPanel p1 = new JPanel(new GridLayout(2,1));
          p1.add(red);
          p1.add(green);
          JPanel p2 = new JPanel(new FlowLayout());
          p2.add(button1);
          p2.add(button2);
    
          getContentPane().add(p1);
          getContentPane().add(p2);
          pack();
       }
    
    * *public static void main(String[] args){
          TrafficLight tl = new TrafficLight(); 
          tl.setVisible(true);
       } 
    
       @Override
       public void actionPerformed(ActionEvent e){ 
          if (e.getSource() == button1){
             green.turnOn(false); 
             red.turnOn(true);
          } else if (e.getSource() == button2){
             green.turnOn(true);
             red.turnOn(false);
       }
    }
    }
    Last edited by Abdo; 04-28-2012 at 06:05 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: setting up java timer

    Can we see your attempt to use the Swing Timer class?

    Also, when posting code, please be sure that the code is already formatted so that we can more easily read it and understand it. Since you're asking volunteers for free advice, it's not asking too much for you to try to help us help you, I don't think.

    **Edit**

    Also consider giving your Signal class a public boolean isOn() method so that outside classes may know the signal's state. I'd also change the on variable to onColor, the turnOn method to setOn(boolean) and change the change variable to on, so that the setOn and isOn methods become true getter and setter methods of a boolean class property.

    Then swapping the state would be as simple as calling:

    mySignal.setOn(!mySignal.isOn());

    ** Edit 2**
    Or you could give Signal a public void method called toggleOn() which swaps the Signal object's state.
    Last edited by Fubarable; 04-28-2012 at 06:06 PM.

  3. #3
    Abdo is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: setting up java timer

    Code formatting?? you mean adding tabs to make it more clear??

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: setting up java timer

    Quote Originally Posted by Abdo View Post
    Code formatting?? you mean adding tabs to make it more clear??
    Actually, I usually use three spaces since the forum software displays it more nicely than tabs, but yes, indenting your code. Your current code is formatted as all left-justified making it difficult for folks who didn't write it and are not familiar with it to read and understand. Please also read the edit to my first response to your question for other suggestions.

    Edit:
    If you follow these suggestions, the Timer ActionListener's actionPerformed method will consist of just two lines of code.

    Again, much luck.
    Last edited by Fubarable; 04-28-2012 at 06:08 PM.

  5. #5
    Abdo is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: setting up java timer

    Quote Originally Posted by Fubarable View Post
    Actually, I usually use three spaces since the forum software displays it more nicely than tabs, but yes, indenting your code. Your current code is formatted as all left-justified making it difficult for folks who didn't write it and are not familiar with it to read and understand. Please also read the edit to my first response to your question for other suggestions.

    Again, much luck.
    Well, I just edited it, and will try to apply your suggestions, thanks..

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: setting up java timer

    Quote Originally Posted by Abdo View Post
    Well, I just edited it, and will try to apply your suggestions, thanks..
    Great! If you do so, your Timer's ActionListener's actionPerformed method will have but two lines of code. It doesn't get much simpler than that!

Similar Threads

  1. need help with the Timer in java.
    By couline in forum New To Java
    Replies: 1
    Last Post: 03-21-2012, 04:21 PM
  2. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-04-2010, 11:15 PM
  3. Need Help with timer (Java)
    By acash229 in forum New To Java
    Replies: 58
    Last Post: 09-13-2010, 01:32 PM
  4. New to Java need help with a timer
    By kd0jzi in forum New To Java
    Replies: 3
    Last Post: 03-20-2010, 03:20 AM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 PM

Posting Permissions

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