简体   繁体   English

计时器循环可不断循环一系列图像[Java-NetBeans]

[英]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). 我正在尝试使用Java Swing Timer创建一个循环,以不断循环浏览一组图像(i1,i2,i3 ....,其中n是图像总数)。

Each of the images is exactly the same size and must be displayed on a Label (say, l1). 每个图像的大小完全相同,并且必须显示在标签上(例如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. 我不使用Java TumbleItem applet怎么办呢?对于像我这样的简单实现来说,似乎太复杂了。 (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 我正在尝试使用Java Swing Timer创建循环以不断循环显示一组图像

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 我不使用Java TumbleItem applet怎么办呢?对于像我这样的简单实现来说似乎太复杂了

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 在run()函数中,使用它在图像之间切换

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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