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.

Page 4 of 5 FirstFirst ... 2345 LastLast
Results 76 to 100 of 106

Thread: Car racing

  1. #76
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    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.

  2. #77
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    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);
            }
    }
    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?

  3. #78
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    I can't make the car to go for 3 laps
    If you can make it go for 2 laps, it should be easy to continue going for the third lap.
    What happens after it goes for the first two laps?

  4. #79
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    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);
            }
    }
    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?
    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

  5. #80
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    If you can make it go for 2 laps, it should be easy to continue going for the third lap.
    What happens after it goes for the first two laps?
    The car will still continue without stopping.
    Because my project requirements needs all the 3 cars to run for 3 laps.

  6. #81
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    If you can make it go for 2 laps, it should be easy to continue going for the third lap.
    What happens after it goes for the first two 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.

  7. #82
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    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 {

  8. #83
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    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 {
    Because today I ask my lecturer on modification should be made.
    He said 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.

  9. #84
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    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.
    Does your code do that? What are the steps you need to do to do that?
    Here is your code, slightly modified:
            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();    //?????
            }
    Please explain line by line what this code is supposed to do.
    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.

  10. #85
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    Does your code do that? What are the steps you need to do to do that?
    Here is your code, slightly modified:
            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();    //?????
            }
    Please explain line by line what this code is supposed to do.
    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?
    if(count <= 3) {
    This is to indicate that the race need to be run for 3 laps
    t1 = new Timer((int)s1, new ActionListener() {
    This is to make the car move at random speed
    if (x1 <= 1000) {
    Make the car delay for i milli second
    x1 += 10; 
                        }
    To make the car move with the timer
    t1.stop();  // ???
                        count++;
    After 3 laps the car will stop, as the count starts from 0 until 3.
    else {
                  t1.start();
    To start the car.

  11. #86
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    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?

  12. #87
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    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.

  13. #88
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    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?
    Because I want to show the that the race will stop after 3 laps.
    For count value part I was confused with the coding.}\
    Thr run() just call once then it will loop the race until 3 laps then stop.

  14. #89
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    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.
    Ya..once I remove everything already then they will keep looping without stopping.
    If I insert the for loop inside the there will it be ok..??

  15. #90
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    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?

  16. #91
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    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)

  17. #92
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    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?

  18. #93
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    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?
    By using the the count++.At first by declaring int count = 0 on top.Then, if the count is 3 then it will stop the race for each car.
    Once all the cars stopped after 3 laps by stopping the car timer.

  19. #94
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    Yes you will use a counter.
    Will there be a counter for each car?
    Where will you increment the counter?

    Once all the cars stopped
    How will you know that all the cards are stopped?

    You need lots more details on how you are going to do this.

  20. #95
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    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?

    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.

  21. #96
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    will be increased inside the timer section by using the if-else statement.
    What test do you make with the if statement to determine if the counter should be incremented?

    Because of the counter that I set for 3 laps and this is how I know all the cars are stopped.
    Where is this counter's value incremented?

  22. #97
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    What test do you make with the if statement to determine if the counter should be incremented?


    Where is this counter's value incremented?
    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();
            }
    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.
    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.

  23. #98
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    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.

  24. #99
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    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.
    In this case, can I use System.out.println to display the message?
    How to set the n count in the message since I'm using this method?So that the n will generate automatically inside the message.

  25. #100
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    can I use System.out.println to display the message?
    Yes, that is how I would do it.

    n will generate automatically inside the message.
    No, do the increment outside of the println in its own statement.

    A sample of printing:
    int laps = 3; // for testing only
    System.out.println("Car laps=" + laps); // print the value of laps

Page 4 of 5 FirstFirst ... 2345 LastLast