Splash Page Creation

Question:
I would like to show a splash page and have it time out after approximately five seconds. How do I do that?

Answer:
Presumably you would like to display a splash page while the application is loading, rather than freezing the application for five seconds. Any time you start talking about doing two things at once, you’ve got to think threads. You could always display the splash page only while the application is loading, in which case you would not need to use threads. Just show the splash page, load the application, and hide the splash page. But many applications load very quickly,and you would often like the user to see your splash page for a specified period of time, such as the five seconds in your question.

The Thread.sleep method allows you to introduce a delay into your program, suspending the current thread of execution forthe specified amount of time before resuming. There are two versions of Thread.sleep. The first accepts a single argument of type long that specifies the delay in milliseconds. The second accepts an additional integer argument that specifies an additional number of nanoseconds. Now that you know how to make a thread wait for a specified period of time, all you have to do is create a separate thread that shows the splash page, sleeps for five seconds (5000 milliseconds), and hides the splash page after waking up. The following listing demonstrates how to go about doing this using the Swing classes. I use a JDialog for the splash page because the native window manager will adorn it with a border and keep it placed on top of the parent window. JWindow instances have no borders and cannot take advantage of the native window system to keep them placed on top of a frame. Notice how I have to calculate the coordinates of the splash page in order to center it on the application window.

import java.awt.*;import java.awt.event.*;import com.sun.java.swing.*;public class SplashPage {  JFrame __frame;  static class SplashPanel extends JPanel {    private Icon __splashIcon;    SplashPanel(Icon icon) {      __splashIcon = icon;      setPreferredSize(new Dimension(icon.getIconWidth(),				     icon.getIconHeight()));    }    public void paint(Graphics graphics) {      super.paint(graphics);      __splashIcon.paintIcon(this, graphics, 0, 0);    }  }    class SplashThread extends Thread {    private int __delay;    SplashThread(int delay) { __delay = delay; }    // Displays splash page on top of application frame, waits    // __delay milliseconds, and disposes of the splash page.    public void run() {      SplashPanel panel = new SplashPanel(new ImageIcon("splash.gif"));      JDialog splash;      Dimension size1, size2;      Point location;      location = __frame.getLocation();      size1    = __frame.getSize();      splash   = new JDialog(__frame);      splash.getContentPane().add(panel);      splash.pack();      size2     = splash.getSize();      splash.setLocation(location.x + (size1.width - size2.width)/2,			 location.y + (size1.height - size2.height)/2);      splash.setVisible(true);      // Wait five seconds, then dispose of splash.      try {	Thread.sleep(__delay);      } catch(InterruptedException e) {	// Don't do anything      }      splash.setVisible(false);      splash.dispose();    }  }    public SplashPage() {    WindowListener exitListener;    __frame = new JFrame("Splash Demo");    exitListener = new WindowAdapter() {      public void windowClosing(WindowEvent e) {        Window window = e.getWindow();        window.setVisible(false);        window.dispose();        System.exit(0);      }    };        __frame.addWindowListener(exitListener);    // any other application initialization goes here  }  public void loadApplication() {    Thread showSplash;    Dimension size;        size = Toolkit.getDefaultToolkit().getScreenSize();    __frame.setSize(640, 480);    // Center frame on screen.    __frame.setLocation((size.width - 640)/2, (size.height - 480)/2);    __frame.setVisible(true);    // Show the splash page for 5 seconds    showSplash = new SplashThread(5000);    showSplash.start();    // Continue loading your application here while the    // splash page is being displayed.  }  public static final void main(String[] args) {    SplashPage application;    application = new SplashPage();    application.loadApplication();  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a