简体   繁体   中英

Timer loop to constantly loop series of images [Java-NetBeans]

I'm trying to create a loop using Java Swing Timer to constantly cycle through a set of images (i1, i2, i3....in where n is total number of images).

Each of the images is exactly the same size and must be displayed on a Label (say, l1).

There must be a delay of ten seconds between each image being displayed.

Any idea how I can go about this without using the Java TumbleItem applet> It's seems much too complicated for a simple implementation such as mine. (Displaying special deals posters on an online storefront application for school).

I am open to this being achieved in any other way.

Help would be greatly appreciated. Thanks in advance!

I'm trying to create a loop using Java Swing Timer to constantly cycle through a set of images

When you use a Timer you don't use a loop. When the Timer fires you just change the image. So somewhere you would need to keep a List of the images to display and an index of the currently displayed image.

Any idea how I can go about this without using the Java TumbleItem applet> It's seems much too complicated for a simple implementation such as mine

How is it complicated? It displays a series of images, which is close to what you want.

Yes, there is some extra code that loads the images and doesn't start the animation until all the images are loaded. So you could easily simplify the code by not worry about that. Also, there is code that does animation from from left-to-right and then right-to-left. You also don't need that part of the code. Also, there is code that configures the animation speed. Again you can hard code that.

So if you start with that example and then simplify the code you will have a simple solution. Give it a try and then post your code when you encounter a problem.

This is very simple. Use a timer like this:

    Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {

   public void run() {
     //codehere
   }

}, 0, delayInMillis)

Use can use an integer to specify in image.

public int image = 1;

in the run() function, use this to switch between the image

if(image = 1) {
 image = 2;
} else if(image = 2) {
 image = 3;
} if(image = 3) {
 image = 0;
}

Now, wherever you are drawing your images, use this:

if(image == 1) {
  //draw first image
} else if(image == 2) {
  //draw second image
} else if(image == 3) {
  //draw third image
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM