Results 1 to 11 of 11

Thread: Slow iteration

  1. #1
    Maarten is offline Member
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    35
    Rep Power
    0

    Default Slow iteration

    Hello,
    I'm trying to make a foto-show-program.
    I have the follwing loop in it:

    Java Code:
     public void showVoorstelling()
        {
            for (Foto deFoto : fotoLijst)
            {
                ImageIcon fototje = deFoto.getFoto();
                dia.setIcon(fototje);
               
               
            }
    Is there a way to slow the loop down, because it shows all the foto's in a split second.
    thanks, Maarten.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Slow iteration

    To allow some time between when each image is shown use a background thread with a timer to load the image and show it every time interval. It's important that the waiting for the next time to show the next image not be done on the EDT as that will keep the JVM from updating the GUI.

    See this tutorial for some ideas: https://docs.oracle.com/javase/tutor...ncy/index.html
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Slow iteration

    Too bad the tutorial page on timers is rather primitive and has an applet as an example rather than a normal client application.

    How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)


    off-topic: Funny how my link is turned into a proper titled one and Norm's wasn't. That has also happened to me recently, I think it worked here because I put my link without any text in front of it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Maarten is offline Member
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    35
    Rep Power
    0

    Default Re: Slow iteration

    Thanks for the replies!
    I tried to implement the Timer mentioned in the tutorial, but it did not work properly. I do get a delay before showing all the pictures, but not in between. I call the start of the timer in the constructor and then the following actionPerformed starts:

    Java Code:
     public void actionPerformed(ActionEvent e)
        {
             for (Foto deFoto : fotoLijst)
            {
                ImageIcon fototje = deFoto.getFoto();
                dia.setIcon(fototje);
                timer.restart();
               
            }
            
        }
    But, as said it only gives the delay in the beginning, not between the pictures, what am I doing wrong here?
    Thanks, Maarten.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Slow iteration

    The code needs to display just one image each time the listener method is called. There needs to be variables and logic to keep track of which image is supposed to be displayed each time the listener method is called.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Slow iteration

    I think that timer.restart() is misplaced; the timer already repeats itself, you have no need to restart it after each action.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: Slow iteration

    like gimbal says, I don't think you need to restart the timer either.
    for that matter if you use a timer I don't even think you necessarily need a loop.
    you could start the timer when you start playing the slideshow and fire an event every 5 or 10 thousand milliseconds to change the picture to the next one.

    not to sound conceited, because I'm pretty much still a beginner myself but when coding there are often various ways to get the same results so if you get stuck with something, like the loop you could try thinking of other ways to get the results you want.

  8. #8
    Maarten is offline Member
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    35
    Rep Power
    0

    Default Re: Slow iteration

    Thanks for the replies!
    With the help I came up with the following, again the constructor fires the timer and then this action performed starts which restarts itself as long as fotoNr < the list of fotos:

    Java Code:
     public void actionPerformed(ActionEvent e)
       {
           if (fotoNr < fotoLijst.size())
            {
                dia.setIcon(fotoLijst.get(fotoNr).getFoto());
                fotoNr++;
                timer.start();
            }
            else
            {
                timer.stop();
                fotoNr = 0;
            }
        }
    Last edited by Maarten; 05-25-2015 at 04:53 PM.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Slow iteration

    I am assuming that this is the actionListener for your timer. Since you need to start the timer somewhere you do not need to restart it within the listener (at a minimum it's bad form). So why not do the following?

    Java Code:
    int fotoNr = 0;
    Timer timer = new Timer(0, reference to listener); // no initial delay.
    timer.setDelay(1000); // 1 second
    timer.start();
    
    // then later...
    public void actionPerformed(ActionEvent e) {
          if (fotoNr < fotoLijst.size()) {
               dia.setIcon(fotoLijst.get(fotoNr).getFoto());
               fotoNr++;
          } else {
               timer.stop();
               fotoNr = 0;
         }
    }
    I believe this was suggested earlier.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Slow iteration

    Quote Originally Posted by jim829 View Post
    Java Code:
    Timer timer = new Timer(0, reference to listener); // no initial delay.
    timer.setDelay(1000); // 1 second
    timer.start();
    Nope.
    Java Code:
    Timer timer = new Timer(1000, reference to listener); // 1 second
    timer.setInitialDelay(0); // no initial delay
    timer.start();
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Slow iteration

    Quote Originally Posted by DarrylBurke View Post
    Nope.
    Java Code:
    Timer timer = new Timer(1000, reference to listener); // 1 second
    timer.setInitialDelay(0); // no initial delay
    timer.start();
    db
    Yep!

    Java Code:
    Timer timer = new Timer(0, (a)->System.out.println("Foo"));
    timer.setDelay(1000); // 1 second timer
    System.out.println("Starting timer");
    timer.start();
    Of course, your way works too!

    Regards,
    Jim
    Last edited by jim829; 05-26-2015 at 03:39 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Recursion to Iteration exponents Help
    By williamsb14 in forum New To Java
    Replies: 1
    Last Post: 02-14-2013, 09:49 AM
  2. Iteration to Recursion
    By fam2315 in forum New To Java
    Replies: 7
    Last Post: 06-22-2011, 02:58 AM
  3. Real life example of iteration
    By fam2315 in forum New To Java
    Replies: 1
    Last Post: 06-19-2011, 08:33 PM
  4. Enum Iteration
    By A.Russell in forum New To Java
    Replies: 1
    Last Post: 08-15-2007, 12:17 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
  •