Go to the API doc for javax.swing.Timer.
There are links there to the tutorial which has examples of how to use the Timer class.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Go to the API doc for javax.swing.Timer.
There are links there to the tutorial which has examples of how to use the Timer class.
I declared int count = 0 on top to indicate the lap.But at the t1 action listener part there, I can't make the car to go for 3 laps.It just stucks at the starting line.Is there any simplest way to make the car to move for 3 laps?import java.awt.*; import java.awt.event.*; import java.applet.Applet; import javax.swing.Timer; import java.applet.*; public class Race extends Applet implements Runnable{ Image img_1; Image img_2; Image img_3; Image img_4; int x1 = 10; int x2 = 10; int x3 = 10; int x4 = 920; int count = 0; Thread animate = null; Timer t1; Timer t2; Timer t3; double s1 = Math.random() * 1000; double s2 = Math.random() * 1000; double s3 = Math.random() * 1000; double a1 = 1000; double a2 = 1000; double a3 = 1000; public void init(){ img_1 = getImage(this.getCodeBase(), "Blue Car.png"); setSize(1000,430); img_2 = getImage(this.getCodeBase(), "Red Car.png"); setSize(1000,430); img_3 = getImage(this.getCodeBase(), "Green Car.png"); setSize(1000,430); img_4 = getImage(this.getCodeBase(), "Finish Line.png"); setSize(1000,430); setBackground(Color.LIGHT_GRAY); } public void start(){ if ( animate == null) { animate = new Thread(this); animate.start(); } } public void run(){ if(count <= 3){ t1 = new Timer((int)s1, new ActionListener() { public void actionPerformed(ActionEvent e){ if (x1 <= 1000) { x1 += 10; } t1.stop(); count++; } });} else t1.start(); repaint(); t2 = new Timer((int)s2, new ActionListener() { public void actionPerformed(ActionEvent e){ if (x2 <= 1000) { x2 += 10; } else x2 = 0; repaint(); } }); t2.start(); t3 = new Timer((int)s3, new ActionListener() { public void actionPerformed(ActionEvent e){ if (x3 <= 1000) { x3 += 10; } else x3 = 0; repaint(); } }); t3.start(); } public void paint( final Graphics g) { g.drawString("Racer 1", 1, 75); g.drawImage(img_1, x1, 50, 50, 40, null); g.drawString("Racer 2", 1, 220); g.drawImage(img_2, x2, 185, 50, 40 , null); g.drawString("Racer 3", 1, 360); g.drawImage(img_3, x3, 325, 50, 40 , null); g.drawLine(50 ,0 ,50 , 420); g.drawImage(img_4, x4, 0, 80, 420, null); g.drawLine(0, 1, 1000,1); g.drawLine(0, 140, 1000,140); g.drawLine(0, 1, 1000,1); g.drawLine(0, 280, 1000,280); g.drawLine(0, 420, 1000,420); } }
If you can make it go for 2 laps, it should be easy to continue going for the third lap.I can't make the car to go for 3 laps
What happens after it goes for the first two laps?
I declared int count = 0 on top to indicate the lap.But at the t1 action listener part there, I can't make the car to go for 3 laps.It just stucks at the starting line.Is there any simplest way to make the car to move for 3 laps and stop after 3 laps?import java.awt.*; import java.awt.event.*; import java.applet.Applet; import javax.swing.Timer; import java.applet.*; public class Race extends Applet implements Runnable{ Image img_1; Image img_2; Image img_3; Image img_4; int x1 = 10; int x2 = 10; int x3 = 10; int x4 = 920; int count = 0; Thread animate = null; Timer t1; Timer t2; Timer t3; double s1 = Math.random() * 1000; double s2 = Math.random() * 1000; double s3 = Math.random() * 1000; double a1 = 1000; double a2 = 1000; double a3 = 1000; public void init(){ img_1 = getImage(this.getCodeBase(), "Blue Car.png"); setSize(1000,430); img_2 = getImage(this.getCodeBase(), "Red Car.png"); setSize(1000,430); img_3 = getImage(this.getCodeBase(), "Green Car.png"); setSize(1000,430); img_4 = getImage(this.getCodeBase(), "Finish Line.png"); setSize(1000,430); setBackground(Color.LIGHT_GRAY); } public void start(){ if ( animate == null) { animate = new Thread(this); animate.start(); } } public void run(){ if(count <= 3){ t1 = new Timer((int)s1, new ActionListener() { public void actionPerformed(ActionEvent e){ if (x1 <= 1000) { x1 += 10; } t1.stop(); count++; } });} else t1.start(); repaint(); t2 = new Timer((int)s2, new ActionListener() { public void actionPerformed(ActionEvent e){ if (x2 <= 1000) { x2 += 10; } else x2 = 0; repaint(); } }); t2.start(); t3 = new Timer((int)s3, new ActionListener() { public void actionPerformed(ActionEvent e){ if (x3 <= 1000) { x3 += 10; } else x3 = 0; repaint(); } }); t3.start(); } public void paint( final Graphics g) { g.drawString("Racer 1", 1, 75); g.drawImage(img_1, x1, 50, 50, 40, null); g.drawString("Racer 2", 1, 220); g.drawImage(img_2, x2, 185, 50, 40 , null); g.drawString("Racer 3", 1, 360); g.drawImage(img_3, x3, 325, 50, 40 , null); g.drawLine(50 ,0 ,50 , 420); g.drawImage(img_4, x4, 0, 80, 420, null); g.drawLine(0, 1, 1000,1); g.drawLine(0, 140, 1000,140); g.drawLine(0, 1, 1000,1); g.drawLine(0, 280, 1000,280); g.drawLine(0, 420, 1000,420); } }
For the race time, is it right to declared int a1 = 1000 on top?How to insert the time inside the t1 part so that the time will start once the race begins and the total time is calculated after 3 laps
If I remove the count variable the cars will be running without stopping just like you said here but without the laps being indicated.
In other words, they will keep on looping.
I feel that something goes wrong for the count declaration along with the codings insde the t1 part.
I've just looked more closely at your code.
I have no idea what the code is trying to do. There are no comments describing what you want it to do.
For example, what are these statements supposed to do and why:t1.stop(); // ??? t1.start(); //?????
Especially why are they where they are?
There are a lot of hidden } at the ends of lines of code vs out where they can be seen and i n line with the statement with the opening/beginning {
Does your code do that? What are the steps you need to do to do that?I should put if(count <= 3) then the car will stop after 3 laps by using the stop method.
Else the car will keep on running with the start method.
Here is your code, slightly modified:
Please explain line by line what this code is supposed to do.if(count <= 3) { t1 = new Timer((int)s1, new ActionListener() { public void actionPerformed(ActionEvent e){ if (x1 <= 1000) { x1 += 10; } t1.stop(); // ??? count++; } }); } else { t1.start(); //????? }
Especially why do you stop the thread immediately when the actionPerformed method is called?
Why do you only start the thread when the count is > 3?
Where is the value of count changed relative to the calls to the run method?
Is run() called more than once?
Last edited by Norm; July 22nd, 2011 at 09:34 AM.
This is to indicate that the race need to be run for 3 lapsif(count <= 3) {
This is to make the car move at random speedt1 = new Timer((int)s1, new ActionListener() {
Make the car delay for i milli secondif (x1 <= 1000) {
To make the car move with the timerx1 += 10; }
After 3 laps the car will stop, as the count starts from 0 until 3.t1.stop(); // ??? count++;
To start the car.else { t1.start();
Why do you only start the thread when the count is > 3?
Where is the value of count changed relative to the calls to the run method?
Is run() called more than once?
I suggest that you remove the code that is now using the count variable and get the three cars to move.
Then think about what you want to do when they each restart the next lap.
What do you want to do as they each restart the next lap?
Should there be a counter for each car?
Should the race stop when ONE car has completed the number of laps?
I want to make them to continue until they reach 3 laps then stop the race.
Ya,because the counter is used to indicate after 3 laps, ALL the cars will be stop.
Nope..the race stops after ALL the cars completed 3 laps..(meaning the race stops after the last car crosses the finish line)
Ok, how are you going to keep track of each car's number of laps?
And how are you going to know when all three cars have finished their three laps?
Yes you will use a counter.
Will there be a counter for each car?
Where will you increment the counter?
How will you know that all the cards are stopped?Once all the cars stopped
You need lots more details on how you are going to do this.
Ya,there will be a counter for each car.
They will be increased inside the timer section by using the if-else statement.
Because of the counter that I set for 3 laps and this is how I know all the cars are stopped.
Mind to help me out?I really appreciate your help.
Because I need to hand in this project on Tuesday.So I not left much time to complete it.
What test do you make with the if statement to determine if the counter should be incremented?will be increased inside the timer section by using the if-else statement.
Where is this counter's value incremented?Because of the counter that I set for 3 laps and this is how I know all the cars are stopped.
I used if(counter <= 3) then the counter will be increased until 3 after each lap.This is to make the race run for 3 laps.If not the race will continue to loop.if(count <= 3) { t1 = new Timer((int)s1, new ActionListener() { public void actionPerformed(ActionEvent e){ if (x1 <= 1000) { x1 += 10; } t1.stop(); count++; } }); } else { t1.start(); }
The counter value I have declared at the top with int count = 0.This will go along with the count++ for the count increment inside the timer class just as my code above.
Last edited by DanielVkc; July 22nd, 2011 at 11:18 AM.
Your last post looks just like the broken code you posted earlier.
Lets start with a simple problem and change it as you go along.
Take the code that has the three cars running the race. Change that code so that as each car completes a lap it prints out a message saying that the "car has just completed lap n" where n is the count variable for that car. Get that code to work so it prints out the message as each car ends its lap.
Yes, that is how I would do it.can I use System.out.println to display the message?
No, do the increment outside of the println in its own statement.n will generate automatically inside the message.
A sample of printing:
int laps = 3; // for testing only
System.out.println("Car laps=" + laps); // print the value of laps