• Post Reply Bookmark Topic Watch Topic
  • New Topic

Create a loop that loop for x number of seconds. How ?  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create a loop that loop for x number of seconds. How can I do it?
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, it's pretty straightforward -- what have you tried so far? Show us, and we'll give you a hint in the right direction.

Henry
 
Thang Pham
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I look around a lot but I havent find anything that I think might work. I look at java.util.Timer as well as java.Time but no luck there. If you can point me in the right direction then I would appreciate it very much. Here is roughly what I want.

loop(10 seconds) {
//Display random images. Eventually I want to create a random selector that just randomly select picture out of a array and display on the screen within 10 seconds
}
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, it depends on what you want -- if you want to use time to break out of a loop, take a look at the System.currentTimeMillis() method call. With this, you can determine when to break out of a loop. If you just want to not do anything for X seconds, which seems to be what you are describing, then take a look at the Thread.sleep() method call.

And BTW, is this a GUI application? If it is, then neither solution is applicable. You can't hold the event dispatching thread that long. It will freeze your GUI. Just display one picture, then setup another event, X seconds later, for the next picture. Take a look at the javax.swing.Timer class.

Henry
 
Thang Pham
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is a GUI application. Here is my approach, can you see if it encounter the problem you talked about




I want to replace one picture with others as many as possible within 10s. Is it the right approach? Thank you so much for your feedback.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If the code is executed by the event dispatching thread -- meaning part of mousePressed(), keyPressed(), actionPerformed(), etc. etc. etc., then no... the code won't work as it will freeze your GUI.

If the code is in it's own thread, then it should work.... except, you are not allowed to modify the GUI from a thread that is not the event dispatching thread.... so this code.... "//display a picture on a frame".... whatever it is, won't work. You need to move that one line of code back to the event dispatching thread. Take a look at the javax.swing.SwingUtilities.invokeLater() method.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Don't you mean something like this...



Because it is very unlikely that you will hit it at exactly 10 seconds -- to the millisecond.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!