Reputation: 8686
I'm using Swing in order to create a little java 2D game. I only try to display an image. Since i'm not on the EDT I'm using SwingUtilities.invokeLater() to do the stuff. When I use it, image is not displayed (in fact it's displayed during few miliseconds and disapear). When I don't use SwingUtilities.invokeLater() the image is correctly displayed but I need to use invokeLater().
Here's my basic code :
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
final BreizhFrame frame = new BreizhFrame();
File imgFile = new File("src/test/lvl1-800x450.jpg");
System.out.println(String.valueOf(imgFile.exists()));
System.out.println(SwingUtilities.isEventDispatchThread());
Image i;
try {
i = ImageIO.read(imgFile);
frame.getGraphics().drawImage(i, 0, 0, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (InvocationTargetException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Any idea ?
Thanks.
Upvotes: 1
Views: 259
Reputation: 22241
That's not how you do it in Java. Yes, you are drawing the image, but after a while the control gets invalidated and it's re-drawn without your image.
Upvotes: 3