amk
amk

Reputation: 1

showing images in java with 100ms intervals

I want to show some images with interval times 100 milisecond, but when I show them on the screen, some of them are visible and some not (with random order). here is part of my code :

panel = new ShowImage(Integer.toString(ImageRand+1),kind); // show random images 
//panel.setIgnoreRepaint(false);
frame.getContentPane().add(panel);
frame.validate();
while(panel.isShowing()!=true );
panel.setVisible(true);
start=System.currentTimeMillis();
while(System.currentTimeMillis()-start < 100);

even I have used nanoTime() function but it doesn't work correctly

Edit:

before I add this two lines :

while(panel.isShowing()!=true );

panel.setVisible(true);

my code looked like this :

panel = new ShowImage(Integer.toString(ImageRand+1),kind);

frame.getContentPane().add(panel);

frame.validate();

start=System.currentTimeMillis();

while(System.currentTimeMillis()-start < 100);

it means just showing an image on the screen and then waiting 100 miliseconds, and run this code again (I load each image just once at the beginning of the main program) and my problem is : the intervals between images are not equal then I addwhile(panel.isShowing()!=true );to be sure that my panel is on the screen (with this while I can find out that before I wait 100 ms, the panel is on the screen), but I have the same problem

Upvotes: 0

Views: 273

Answers (1)

obataku
obataku

Reputation: 29646

Your tight loops aren't doing anything -- look at the ; after each.


What is the logic in this, regardless? I'm assuming you meant:

while (!panel.isShowing()) {
  panel.setVisible(true);
}

According to the documentation on Component.isShowing(),

Determines whether this component is showing on screen. This means that the component must be visible, and it must be in a container that is visible and showing.


Aside from that, you should look into using a Timer. You can use an ActionListener which will be run on the event dispatch thread, enabling safe mutation of the UI components.

Upvotes: 2

Related Questions