Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to make a splash screen with java

In this article we are going to implement a splash screen which will be shown on the screen for a specified interval.

We are going to implement a splash screen which will be shown on the screen for a specified interval. After that screen leaves your application starts. It can be helpful to advertise or even when the software is heavy and take time to load then until that time user can check the splash screen

Note: You need to paste an image file named splash.png and place in the directory from where you are running the java files

First we will call all the import classes which we are going to use in the program

Listing 1: Import Classes

import javax.swing.JWindow;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

  1. JWindow is used to display a container anywhere on screen. This is without any title bar so is the best option for building a splash screen.
  2. ImageIcon creates an ImageIcon from an array of bytes which were read from an image file
  3. Graphics is used for drawing the image
  4. Image is used for storing an image
  5. Here we use the Toolkit to obtain the image.

Now we will define the main class along with the main function

Listing 2: Defining class and the main method

public class SplashScreen extends JWindow
{

Image img=Toolkit.getDefaultToolkit().getImage("splash.png");

ImageIcon imgicon=new ImageIcon(img);
public static void main(String[]args)
{
SplashScreen sp=new SplashScreen();
}

Here

  1. We define the class name is SplashScreen.
  2. We obtained the splashscreen image using the Toolkit.getDefaultToolkit().getImage(arg) function and store that in an Image object.
  3. Now we make an image icon and pass the image object to this. This will make an image icon.
  4. We define the main method and make an object for this class, which will call its constructor.

Now we will define the constructor

Listing 3: Defining the constructor for the class

public SplashScreen()
{
try
{
	
setSize(633,300);
setLocationRelativeTo(null);
show();
Thread.sleep(5000);
dispose();
javax.swing.JOptionPane.showMessageDialog((java.awt.Component)
        null,"Welcome", "Welcome Screen:",
        javax.swing.JOptionPane.DEFAULT_OPTION);
}
catch(Exception exception)
{
	   javax.swing.JOptionPane.showMessageDialog((java.awt.Component)
               null,"Error"+exception.getMessage(), "Error:",
               javax.swing.JOptionPane.DEFAULT_OPTION);
}
}

Here

  1. We define the code inside try-catch block so that exception can be taken care of.
  2. Now we set size of the JWindow with the setSize method
  3. We set the setLocationRelativeTo as null so that it is displayed at the center of the screen
  4. We call the show method so that this JWindow object which in our case is containing the splashscreen shows up
  5. We call the Thread.sleep(5000) method so that this splashscreen waits for 5 seconds on the screen.
  6. Now we call the dispose method to remove the JWindow(containing the splashscreen) also gets removed from the screen
  7. Now we may start our main application. Here I opened a frame which says welcome.

Now we will define the paint method

Listing 4: Defining the paint method

public void paint(Graphics g)
{
g.drawImage(img,0,0,this);
}
}

Here

Paint method uses the drawImage method to draw the splashscreen on the Screen

Output:

Application result

Figure 1: Application result

Listing 5: Full Source code

package article8;

import javax.swing.JWindow;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;


public class SplashScreen extends JWindow
{

Image img=Toolkit.getDefaultToolkit().getImage("splash.png");

ImageIcon imgicon=new ImageIcon(img);

public SplashScreen()
{
try
{
	
setSize(633,300);
setLocationRelativeTo(null);
show();
Thread.sleep(5000);
dispose();
javax.swing.JOptionPane.showMessageDialog((java.awt.Component)
        null,"Welcome", "Welcome Screen:",
        javax.swing.JOptionPane.DEFAULT_OPTION);
}
catch(Exception exception)
{
	   javax.swing.JOptionPane.showMessageDialog((java.awt.Component)
               null,"Error"+exception.getMessage(), "Error:",
               javax.swing.JOptionPane.DEFAULT_OPTION);
}
}

public void paint(Graphics g)
{
g.drawImage(img,0,0,this);
}

public static void main(String[]args)
{
SplashScreen sp=new SplashScreen();
}
}



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login