Results 1 to 6 of 6
Thread: setting up java timer
- 04-28-2012, 05:39 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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.
-
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.
- 04-28-2012, 05:51 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: setting up java timer
Code formatting?? you mean adding tabs to make it more clear??
-
Re: setting up java timer
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.
- 04-28-2012, 06:07 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
-
Re: setting up java timer
Similar Threads
-
need help with the Timer in java.
By couline in forum New To JavaReplies: 1Last Post: 03-21-2012, 04:21 PM -
Stopping a Timer from Inside the timer
By krishnan in forum Java AppletsReplies: 2Last Post: 10-04-2010, 11:15 PM -
Need Help with timer (Java)
By acash229 in forum New To JavaReplies: 58Last Post: 09-13-2010, 01:32 PM -
New to Java need help with a timer
By kd0jzi in forum New To JavaReplies: 3Last Post: 03-20-2010, 03:20 AM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks