Forcing images to be drawn

Hello. I am working on a program that will display an image for a tenth of a second and then allow the user to click a button representing the image they thought they saw. I am having trouble because it seems like Java draws the images when it wants to instead of when I tell it to. I have extended the JPanel class as seen below. I have also written a class called ImageTimer which just waits a tenth of a second before notifying the semaphore it was created with (I know this part of the code works right). The ShapePanel constructor is passed a blank image, and I call the changeImage(Image) method every time a button is clicked.

publicclass ShapePanelextends JPanel{

private Image currentImage, blankImage;

private Semaphore semaphore =new Semaphore(0,true);

public ShapePanel(Image b){

blankImage = b;

currentImage = b;

setBackground(Color.WHITE);

}

publicvoid changeImage(Image a){

currentImage = a;

update(getGraphics());

ImageTimerTask task =new ImageTimerTask(semaphore);

Thread thread =new Thread(task);

thread.start();

try{

semaphore.acquire();

}catch(InterruptedException e){

}

currentImage = blankImage;

update(getGraphics());

}

publicvoid paintComponent(Graphics g){

super.paintComponent(g);

g.drawImage(currentImage, 0, 0,this);

}

}

The problem is that the "drawImage(Image, int, int, ImageObserver)" method doesn't draw the first image, but rather waits until the ActionListener code for the button has been completed, and then draws both images immediately (so I don't ever end up seeing anything but the blank image). If anyone has any idea how I can force the image to be drawn when I want it to, it would be greatly appreciated. Thanks:

[2711 byte] By [SegFaulta] at [2007-11-26 12:45:33]
# 1
> it seems like Java draws the images when it wants to instead of when I tell it toThis is exactly correct. Read this tutorial: http://java.sun.com/products/jfc/tsc/articles/threads/threads1.htmland please in future post Swing questions in the Swing forum.
DrClapa at 2007-7-7 16:24:29 > top of Java-index,Java Essentials,Java Programming...
# 2

> but rather waits until the ActionListener code for the button has been completed,

What button? I don't see any code for a button. The code you posted doesn't really help without knowing the entire context of you program.

I don't know why you extended JPanel to draw an image. Just add add an ImageIcon to a JLabel. Every time you use setIcon it will repaint itself automatically. In your button you would probably set the icon then start a Swing Timer. The Timer would fire in a tenth of a second and then use a new icon for the label. You should never use update(...);

If for some reason you want to do custom painting on a JPanel instead of using a JLabel with an ImageIcon, then you use repaint() to for the repainting of the panel.

camickra at 2007-7-7 16:24:29 > top of Java-index,Java Essentials,Java Programming...
# 3

I tried fooling around with doing things using JLabels with ImageIcons before but had the same problem.

>In your button you would probably set the icon then start a Swing Timer.

That's the part that fixed it. The timer that I was originally using was not a Swing timer. When I realized this and fixed it things worked just fine.

Thanks for the help guys!

SegFaulta at 2007-7-7 16:24:29 > top of Java-index,Java Essentials,Java Programming...