How to create a Splash Screen
For anyone who's unfamiliar with the term, a splash screen is the graphic that displays the title etc. for a few seconds when you first execute a program.
What's the best way to develop one? I'm using applets for my program.
Could I just create a frame with an image on it and use the Timer class and a TimerEvent for it to display for a couple seconds?
Thanks,
Shawn
[404 byte] By [
Basti30] at [2007-9-27 20:17:13]

Use class Window instead of Frame.Windows do not have title bars, unlike the Frames.
Excellent. How do I go about displaying the image, and activating the timer?I'm getting a nullPointerException for:myImage = getImage(getDocumentBase(),"R2Logo.jpg");Thanks,Shawn
For applications use
Image im = Toolkit.getDefaultToolkit().getImage("image.gif");
But it is easier to use ImageIcons for splash screens.
ImageIcon icon = new ImageIcon("image.gif");
window.getContentPane().add(new JLabel(icon, SwingConstants.CENTER));
window.setSize(x,y);
window.show();
You can finish loading your application and than
window.dispose();
Or if your application does not take any time to load, wait 5 second with
Thread.sleep(5000);
window.getContentPane().add(new JLabel(icon, SwingConstants.CENTER));This line is not compiling. I've imported javax.swing.* and of course I've created a Window object.Thanks,Shawn
try this code
import java.awt.event.*;import javax.swing.*;import java.awt.*;public class SplashScreen extends JWindow {private JPanel main = new JPanel(new BorderLayout());//Constructorpublic SplashScreen(int time) { //ADD EVERYTHING TO MAIN JPANEL!!! getContentPane().add(main); //Show the window pack(); centerScreen(); show(); try {Thread.sleep(time);dispose(); } catch (InterruptedException ie) {}}//Simple method to center in screenprivate void centerScreen() { Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); int x = (int) ((d.getWidth() - getWidth()) / 2); int y = (int) ((d.getHeight() - getHeight()) / 2); setLocation(x, y);}}
It uses a JWindow so that you do not see any title bar etc. You use this just like a JFrame.
Example:
In you main program, BEFORE YOU DO A pack() show() (if a JFrame) do this:
SplashScreen splash = new SplashScreen(time in milliseconds);
Hope this helps
sorry this maybe clearer
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class SplashScreen extends JWindow
{
private JPanel main = new JPanel(new BorderLayout());
//Constructor
public SplashScreen(int time)
{
//ADD EVERYTHING TO MAIN JPANEL!!!
getContentPane().add(main);
//Show the window
pack();
centerScreen();
show();
try
{
Thread.sleep(time);
dispose();
} catch (InterruptedException ie) {}
}
//Simple method to center in screen
private void centerScreen()
{
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((d.getWidth() - getWidth()) / 2);
int y = (int) ((d.getHeight() - getHeight()) / 2);
setLocation(x, y);
}
}
It uses a JWindow so that you do not see any title bar etc. You use this just like a JFrame.
Example:
In you main program, BEFORE YOU DO A pack() show() (if a JFrame) do this:
SplashScreen splash = new SplashScreen(time in milliseconds);
Hope this helps
My code examples above were for JWindow not for Window.*FatihC*
Thats all fine. But what If I want a JLabel below the image that says "Loading..." and the dots should go on increasing and say, after 20 dots are displayed, then the dots should start back from one ? I am having major refreshing problems. I am using the repaint method, but the label just doesn't get refreshed.
I having a problem displaying the splash screen but I am doing things a little differently...
I have a login JDialog window that comes up to log someone in to a database. After the login is successful, I create a splash screen (JWindow) and dispose of the login screen. My splash screen never shows up. Basically I am processing a database query and creating a new JFrame from it and want a splash screen saying "Loading...". After the JFrame is complete, I dispose of the splash screen. Please tell me why the splash screen never shows up and give me a better solution.
Thanks.
> sorry this maybe clearer
>
> import java.awt.event.*;
> import javax.swing.*;
> import java.awt.*;
>
> public class SplashScreen extends JWindow
> {
> private JPanel main = new JPanel(new
> new BorderLayout());
>
>//Constructor
>public SplashScreen(int time)
>{
>//ADD EVERYTHING TO MAIN JPANEL!!!
>getContentPane().add(main);
>
>//Show the window
>pack();
>centerScreen();
>show();
>try
>{
> Thread.sleep(time);
> dispose();
>} catch (InterruptedException ie) {}
> }
> //Simple method to center in screen
> private void centerScreen()
> {
> Dimension d =
> = Toolkit.getDefaultToolkit().getScreenSize();
>int x = (int) ((d.getWidth() - getWidth()) / 2);
>int y = (int) ((d.getHeight() - getHeight()) / 2);
>setLocation(x, y);
> }
>
> }
>
> It uses a JWindow so that you do not see any title bar
> etc. You use this just like a JFrame.
>
> Example:
>
> In you main program, BEFORE YOU DO A pack() show() (if
> a JFrame) do this:
>
> SplashScreen splash = new SplashScreen(time in
> milliseconds);
>
> Hope this helps
