Swing Utilities
Iam trying to make a splashscreen for my application.Iam instantiating MyFrame class inside SwingUtilities.invokeLater.Inside MyFrame a Splashscreen class extended from JWindow is invoked.I have overridden paint function inorder to draw a image.This paint is not getting called.
However when i donot use SwingUtilities.invokeLater,the same gets called and things are working fine.
Please help me with a solution as iam required to use swingutilities.
Attached is the MyFrame and Splashscreen classes
MyFrame.class
public class MyFrame extends JFrame {
SnmsSplashScreen mySplash;
public MyFrame(String title) {
super(title);
mySplash = new SnmsSplashScreen(this, "C:\\pics\\DSC00048.JPG");
// dummy delay so we can see the Splash!
for (int i = 0; i < 18000; i++) {
System.out.println(i);
}
setSize(200, 200);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new dummy("A Dummy Frame");
}
});
}
}
SplashScreen.java
public class SplashScreen
extends Window {
private Image splashImage;
private int imgWidth, imgHeight;
private String imgName;
private static final int BORDERSIZE = 5;
private static final Color BORDERCOLOR = Color.blue;
Toolkit tk;
public SplashScreen(Frame f, String imgName) {
super(f);
this.imgName = imgName;
tk = Toolkit.getDefaultToolkit();
splashImage = loadSplashImage();
repaint();
showSplashScreen();
f.addWindowListener(new WindowListener());
}
public void repaint(Graphics g) {
g.drawImage(splashImage, BORDERSIZE, BORDERSIZE,
imgWidth, imgHeight, this);
}
public Image loadSplashImage() {
MediaTracker tracker = new MediaTracker(this);
Image result =tk.getImage(imgName);
tracker.addImage(result, 0);
try {
tracker.waitForAll();
}
catch (Exception e) {
e.printStackTrace();
}
imgWidth = result.getWidth(this);
imgHeight = result.getHeight(this);
return (result);
}
public void showSplashScreen() {
Dimension screenSize = tk.getScreenSize();
setBackground(BORDERCOLOR);
int w = imgWidth + (BORDERSIZE * 2);
int h = imgHeight + (BORDERSIZE * 2);
int x = (screenSize.width - w) / 2;
int y = (screenSize.height - h) / 2;
setBounds(x, y, w, h);
setVisible(true);
}
public void paint(Graphics g) {
System.out.println(
"paint called");
g.drawImage(splashImage, BORDERSIZE, BORDERSIZE,
imgWidth, imgHeight, this);
}
class WindowListener
extends WindowAdapter {
public void windowOpened(WindowEvent we) {
( (Frame) getParent()).removeWindowListener(this);
setVisible(false);
dispose();
}
}
}

