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.

Results 1 to 8 of 8

Thread: getting my timer to run multiple times

  1. #1
    Junior Member
    Join Date
    Oct 2018
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default getting my timer to run multiple times

    Hello everyone,

    I am writing code to get my semi that I drew with graphics across the screen. This code works; however, it only runs the movement function once. I want it to continue repeating the timer task until the semi has moved completely off the screen. How can I adjust my code to accomplish that?

    Thank you in advance for your help!

    public class DrivingVehicle extends Application {
     
        int x = 10;
        Semi semi = new Semi();
     
        @Override
        public void start(Stage primaryStage) {
     
            Group root = new Group(semi);
            Timer timer = new Timer();
     
            timer.schedule(task,1500l);
     
            Scene scene = new Scene(root, 500, 400, Color.BLUE);
     
            primaryStage.setTitle("Driving semi");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
     
        public static void main(String[] args) {
            launch(args);
        }
     
        TimerTask task = new TimerTask()
        {
            public void run()
            {
                semi.setTranslateX(x); 
                x+=10;
            }
     
        };
     
    }

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: getting my timer to run multiple times

    Actually, you have your timer configured for a one shot operation. Check out the API and add a third argument for repeated calls.

    Regards,
    Jim

  3. The Following User Says Thank You to jim829 For This Useful Post:

    jacobgallipeau (December 2nd, 2018)

  4. #3
    Junior Member
    Join Date
    Oct 2018
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: getting my timer to run multiple times

    Quote Originally Posted by jim829 View Post
    Actually, you have your timer configured for a one shot operation. Check out the API and add a third argument for repeated calls.

    Regards,
    Jim
    Oh got it! Thank you. One more question: How do I get the program to stop running at a certain point? Say, I create an int with a value of 30 and I want it to stop running the timer task once it has run 30 times. How can I do that?

  5. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: getting my timer to run multiple times

    Why not put in a counter in your timer task and increment it on each call. You can then either exit directly from there or set some flag which you can check later.

    Regards,
    Jim
    Last edited by jim829; December 3rd, 2018 at 05:34 PM.

  6. The Following User Says Thank You to jim829 For This Useful Post:

    jacobgallipeau (December 3rd, 2018)

  7. #5
    Junior Member
    Join Date
    Oct 2018
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: getting my timer to run multiple times

    Quote Originally Posted by jim829 View Post
    Why not put in a counter in your timer task and increment it on each call. You can then either exit directly from their or set some flag which you can check later.

    Regards,
    Jim
    Thanks! I do have that done now. Here's the code for it. However, the idea is to make it look like the semi is driving off the screen. So this method makes the vehicle move too abruptly. Do I need to make a second timer to slow it down or something or how can I do it? I tried putting the loop outside of the timer task and instead putting it around timer.schedule, but it just says the task has already run when it tries to attempt it again. Any suggestions?

  8. #6
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: getting my timer to run multiple times

    You didn't include the code but it doesn't matter. There are two basic aspects to making something move. The time between moves and the distance per move. So increasing the time and shortening the distance may make it smoother. You just need to play with it to see what works best.

    Regards,
    Jim

  9. #7
    Junior Member
    Join Date
    Oct 2018
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: getting my timer to run multiple times

    Quote Originally Posted by jim829 View Post
    You didn't include the code but it doesn't matter. There are two basic aspects to making something move. The time between moves and the distance per move. So increasing the time and shortening the distance may make it smoother. You just need to play with it to see what works best.

    Regards,
    Jim
    Sorry. Here's the code. It doesn't allow for adjusting the time between moves as far as I can think of.

    public class DrivingVehicle extends Application {
     
        int x = 10;
        Semi semi = new Semi();
     
        @Override
        public void start(Stage primaryStage) {
     
            Group root = new Group(semi);
            Timer timer = new Timer();
     
            timer.schedule(task, 1500l, 20);
     
            Scene scene = new Scene(root, 500, 400, Color.BLUE);
     
            primaryStage.setTitle("Driving semi");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
     
        public static void main(String[] args) {
            launch(args);
        }
     
        TimerTask task = new TimerTask()
        {
            public void run()
            {
                for(int i=0;i<30;i++)
                {
                    semi.setTranslateX(x); 
                    x+=10;
                }
            }
     
        };
     
    }

  10. #8
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: getting my timer to run multiple times

    The time between moves is dictated by 20ms in your timer the jerky motion by 10 in your x increment.

    Regards,
    Jim

Similar Threads

  1. Unbale to setText to a label multiple times in same program
    By pradeep24 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 5th, 2014, 08:45 AM
  2. [SOLVED] How to make a program run multiple times.
    By kunimaro15689 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 23rd, 2014, 12:35 PM
  3. [SOLVED] Don't understand why it loops through multiple times...
    By Discoveringmypath in forum Loops & Control Statements
    Replies: 4
    Last Post: January 26th, 2013, 08:00 PM
  4. Using the same scanner multiple times in code help
    By theostorm in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 16th, 2012, 11:30 PM
  5. Replies: 2
    Last Post: January 6th, 2012, 09:50 PM