
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we implement a splash screen using JWindow in Java?\\n
JWindow is a Swing component that is used to create a splash screen easily as splash screen as it displays a screen without a title bar or window management buttons. In this article, we will learn to implement a splash screen using JWindow in Java.
What is a JWindow?
A JWindow is a container that can be displayed anywhere on the user's desktop. It does not have the title bar, window management buttons, etc, like a JFrame. The JWindow contains a JRootPane as its only child class.
The contentPane can be the parent of any children of the JWindow. Like a JFrame, a JWindow is another top-level container, and it is an undecorated JFrame. It does not have features like a title bar, a Windows menu, etc.
What is a Splash Screen?
A splash screen is a graphical user interface component that is displayed during program loading. It's a visual feedback to the application users upon application startup, usually displaying branding information.
How do you implement a Splash Screen using JWindow?
A JWindow can be used as a splash screen window using the getImage() method & overriding the paint() method, which displays once when the application is launched and then automatically disappears after a few seconds.
getImage() Method
The getImage() is a method of the Toolkit Class and is used to return an image that gets its data from the specified file, whose format can be either JPEG, PNG, or GIF. The method accepts a single argument, which is the URL of the file location.
Syntax
The following is the syntax for the getImage() method declaration:
public abstract Image getImage(URL url)
drawImage() Method
The drawImage() is a method of the Graphics Class and is used to draw the currently available image on the frame as per the image specifications. This method takes 4 parameters as input, i.e., the defined image, the x-coordinate, the y-coordinate, and the object that is notified later.
Syntax
The following is the syntax for the drawImage method declaration:
public abstract boolean drawImage(Image img, int x, int y, this)
Implementing a Splash Screen using JWindow
To implement a splash screen using JWindow in Java, first create a CreateSplashScreen class extending the JFrame, then declare one Image and one ImageIcon variable named "splashScreen" & "imageIcon". After that, initialize the constructor for the CreateSplashScreen class.
Then we will create an object for the image(splashScreen) using the getImage() method and make an icon of the same by storing it in the imageIcon variable, then we will specify the size, dimensions, and coordinates for the frame as per the image height and width.
splashScreen = Toolkit.getDefaultToolkit().getImage("path/to/your/image.jpg"); imageIcon = new ImageIcon(splashScreen);
After that, we will override the paint() method so that the background is cleared, and then we will use the drawImage() method to paint the image on the frame, and take above above-specified argument as the input.
public void paint(Graphics g) { super.paint(g); g.drawImage(splashScreen, 0, 0, this); }
At last, we will create an object for the CreateSplashScreen, then we will display the image for only 10 seconds by setting the sleep() method to "10000", and after 10 seconds, we will discard the image using the dispose() method.
public static void main(String[] args) { CreateSplashScreen splash = new CreateSplashScreen(); try { Thread.sleep(10000); splash.dispose(); }
Example
Below is an example of implementing a splash screen using JWindow in Java:
import javax.swing.*; import java.awt.*; public class CreateSplashScreen extends JWindow { Image splashScreen; ImageIcon imageIcon; public CreateSplashScreen() { splashScreen = Toolkit.getDefaultToolkit().getImage("C:/Users/User/Desktop/Java Answers/logo.jpg"); // 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) { CreateSplashScreen splash = new CreateSplashScreen(); try { // Make JWindow appear for 10 seconds before disappear Thread.sleep(10000); splash.dispose(); } catch(Exception e) { e.printStackTrace(); } } }