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:

