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();

}

}

}

[3029 byte] By [shruti_tiet123a] at [2007-10-2 14:03:36]
# 1

First, please use code tags next time.

public void repaint(Graphics g) {

g.drawImage(splashImage, BORDERSIZE, BORDERSIZE,

imgWidth, imgHeight, this);

}

This is bogus. Don't ever override repaint(Graphics) unless you know what you're doing.

DaanSa at 2007-7-13 12:12:02 > top of Java-index,Security,Event Handling...
# 2

import javax.swing.*;

import java.awt.*;

public class Splash

{

public static void main(String[] args)

{

// Throw a nice little title page up on the screen first

showSplash(10000);

System.exit(0); // replace with application code!

}

// A simple little method to show a title screen in the center of the screen for a given amount of time.

public static void showSplash(int duration)

{

JWindow splash = new JWindow();

JPanel content = (JPanel)splash.getContentPane();

// set the window's bounds, centering the window

int width = 240;

int height = 120;

Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

int x = (screen.width-width)/2;

int y = (screen.height-height)/2;

splash.setBounds(x,y,width,height);

// build the splash screen

JLabel label = new JLabel("SPALSH SCREEN");

label.setFont(new Font("Sans-Serif", Font.BOLD, 20));

JLabel copyrt = new JLabel("Any Text", JLabel.CENTER);

copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));

content.add(label, BorderLayout.CENTER);

content.add(copyrt, BorderLayout.SOUTH);

//content.setBorder(BorderFactory.createLineBorder(Color.red, 10));

// display it

splash.setVisible(true);

// Wait a little while, maybe while loading resources

try

{

Thread.sleep(duration);

} catch (Exception e) {}

splash.setVisible(false);

}

}

KapoorSida at 2007-7-13 12:12:02 > top of Java-index,Security,Event Handling...