Developing A Splash Screen Using Java

Splash Screens – An Introduction

You must have noticed that whenever you launch any application, whatever the platform, there is a display of a Logo Image related to the firm/organization which owns the application. This lasts usually for around 3-5 seconds. This image (Logo/Banner) display feature is known as Splash Screen. This is also known as Loading Screen since it appears while the application is loading.

Examples of Splash Screens

We are listing some of the real splash screen examples below. These are splash screens of famous android and PC applications.

AIRTEL THANKS(ANDROID)
AIRTEL THANKS(ANDROID) AMAZON PRIME VIDEO(ANDROID)

MS WORD(WINDOWS)

Building Splash Screens in Java

Imagine how exciting it would be if you were able to create and build your splash screen. Don’t worry, we have got you covered. In today’s article, we will be learning to build a splash screen in java. So, without wasting any time, Let’s start.

Concepts used

To develop a splash screen, you must be aware of the following concepts.

  • Object-Oriented Programming
  • Inheritance
  • Package
  • Multithreading

Let us have a brief look at these concepts step by step,

  1. Object-Oriented Programming
    Object-Oriented Programming, abbreviated as OOP, is a style or we can say way of programming where data along with its associated function are kept together as a single unit known as Objects. These Objects build the foundation of OOP.
     
  2. Inheritance
    Inheritance is a powerful feature of OOP through which any object can acquire and use the properties and functions of another object. This feature is very much helpful in saving time for programmers so that they do not have to rewrite code again and again.
     
  3. Package
    We know that Objects are the building blocks of OOP. These Objects are collectively contained in a structure known as class. i.e., a class contains similar types of objects or a class is a collection of objects. Further, these classes are grouped in a single unit known as a Package. i.e., A Package contains similar types of classes or we can say it is a collection of classes.
     
  4. Multithreading
    Java was the first OOP language to have a Multithreading feature. It is a remarkable feature that allows parallel and faster execution of threads. When a Program starts loading or executing, A process is created. This process can be further subdivided into smaller units called Threads, these threads are small chunks of code that can be executed faster and parallelly.

Now Since You have understood the concepts let us look for the tools, we need for developing a splash screen.

TOOLS

To develop a splash screen, we will be needing following tools.

  • Constructor
  • Swing Package
  • AWT Package

Let us have a brief look at these tools before proceeding to development.

  1. Constructor
    A constructor is a special function of the class which is used to initialize class variables. Whenever the object of a class is created, a constructor is automatically invoked and it provides the required initial values to class variables to use during execution. Constructors have the same name as a class and every class has a constructor.
     
  2. AWT Package
    AWT stands for Abstract Windows Toolkit. It contains classes that help programmers in developing GUI-based apps. It contains heavyweight components.
     
  3. Swing Package
    Swing is a GUI-based Application Programming Interface in java that contains lightweight components and allows Programmers to develop GUI-based Apps in java.

CODE

package splashjava;
/**
 *
 * @author Mr. Deepak Kumar
 */
import java.awt.*;
import javax.swing.*;

public class SplashJava extends JWindow {
   Image splashScreen;
   ImageIcon imageIcon;
   public SplashJava() {
      splashScreen = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Mr. Deepak Kumar\\Desktop\\New folder\\SplashJava\\LOGO.png");
      // Create ImageIcon from Image
      imageIcon = new ImageIcon(splashScreen);
      // Set JWindow size from image size
      setSize(imageIcon.getIconWidth(),imageIcon.getIconHeight());
      // Get current screen size
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      // Get x coordinate on screen for make JWindow locate at center
      int x = (screenSize.width-getSize().width)/2;
      // Get y coordinate on screen for make JWindow locate at center
      int y = (screenSize.height-getSize().height)/2;
      // Set new location for JWindow
      setLocation(x,y);
      // Make JWindow visible
      setVisible(true);
   }
   // Paint image onto JWindow
   public void paint(Graphics g) {
      super.paint(g);
      g.drawImage(splashScreen, 0, 0, this);
   }
   public static void main(String[]args) {
      SplashJava splash = new SplashJava();
      try {
         // Make JWindow appear for 10 seconds before disappear
         Thread.sleep(10000);
         splash.dispose();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

CODE EXPLANATION

While most of the code has been self-explanatory, there are still some key tokens in the code to have an idea about.

  • JWindow() is a class in java used to draw a window anywhere on the screen. It Is different from JFrame as it does not have to minimize, close, etc. buttons.
  • Image is an abstract class in java. An abstract class can not have its own body. Its functions are defined in subclasses.
  • printStackTrace() is an Exception Diagonising method in java that is used in handling and reporting runtime errors and other exceptions. It is defined in java.lang.Throwable class.

SUMMARY

Here we learned about how a splash screen can be developed using concepts and tools in java language. One can also add progress bars to make it more realistic and appealing.


Similar Articles