Splash Screen Woes

I am getting inconsistant results when I try to create a Splash Screen. Here are the two versions of what I get:

Basically this is what I am doing:
-=Main Class=-


public TextTest()
{
	splashThread = new Thread(new SplashTest(3, progressString, this));
	splashThread.start();

	progressString = "CreateScreenManager"
	screen = new ScreenManager("TextureTester",WIDTH,HEIGHT,fullscreen,vsync);
	progress++;

	progressString = "Creating Font";
	text = new FontTranslator(fontname, fontstyle, fontsize);
	progress++;

	progressString = "Setting ZMatch";
	text.setZMatch(45.0f, HEIGHT);
	progress++;

	splashThread = null;
	startloop();
}

-=Splash Class=-


public class SplashTest extends JFrame implements Runnable
{
	private Image splashImage;
	private JPanel imagePanel;
	private JProgressBar progressBar;

	public SplashTest(int maxProgress, String initialString)
	{
		setUndecorated(true);
		setBackground(Color.BLACK);
		setResizable(false);
		setSize(640, 261);

		setAlwaysOnTop(true);
		
		Container box = getContentPane();
		box.setLayout(new BorderLayout());
		
		splashImage = getToolkit().createImage("Splash.jpg");
		MediaTracker mediaTracker = new MediaTracker(this);
		mediaTracker.addImage(splashImage, 0);
		try
		{
			mediaTracker.waitForID(0);
		}
		catch (InterruptedException ie)
		{
			System.err.println(ie);
			System.exit(1);
		}
		
		imagePanel = new JPanel();
		imagePanel.setSize(splashImage.getWidth(null), splashImage.getHeight(null));		
		box.add(imagePanel, BorderLayout.CENTER);
		
		progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, maxProgress);
		progressBar.setBackground(Color.BLACK);
		progressBar.setForeground(new Color(128, 0, 0));
		progressBar.setStringPainted(true);
		progressBar.setString(initialString);
		progressBar.setBorderPainted(false);
		box.add(progressBar, BorderLayout.SOUTH);
		
		setVisible(true);
		
		imagePanel.getGraphics().drawImage(splashImage, 0, 0, null);
	}

	public void run()
	{
		Thread myThread = Thread.currentThread();
		 while (parent.splashThread == myThread)
		{
			progressBar.setValue(parent.progress);
			progressBar.setString(parent.progressString);
			try
			{
				Thread.sleep(1);
			}
			catch(InterruptedException e)
			{
				e.printStackTrace();
			}
		}
		dispose();
	}
}

What thread is the first “main” bit running in? Are you starving the AWT thread? Blocking it? Painting an image before it is fully loaded and never repainting it when it is loaded? So much that we can’t tell from the code you posted. Maybe you need to use SwingUtilities.invokeLater for something?

When the program starts it creates an instance of TextTest. The first thing that it does is create a thread to run an instance of the SplashTest class and starts it. The painting of the image actually happens in the SplashTest constructor, where it builds the rest of the splash screen, as shown in the code snippet above. To prevent from painting the image before its fully loaded I have used a mediatracker. What I would really like to know is has onyone does a splash screen before? I’m sure they have! How have they done it? Is this the totally wrong approach?

Casey

But what does your imagePanel do in it’s paintComponent() method? Answer- nothing. It has to repaint your splash graphic.

Also you shouldn’t be manipulating all those Swing components outside of the event thread. Use Swingutilities.invokeAndWait().