This example shows how to create a splash screen in Swing. Below Java code uses JWindow, JFrame, AWT Image components along with Thread for splash screen timeout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import javax.swing.JWindow; import javax.swing.JFrame; import javax.swing.ImageIcon; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.Image; import java.awt.Dimension; public class CreateSplashScreenUsingJWindow extends JWindow { Image imageForSplashScreen; ImageIcon ii; public CreateSplashScreenUsingJWindow() { //Get image for splash screen image. //Put full address for your image location //In my case, my image location is : C:\Documents and Settings\evergreen\Desktop\splashscreenimage.jpg imageForSplashScreen = Toolkit.getDefaultToolkit().getImage("C:\\Documents and Settings\\evergreen\\Desktop\\splashscreenimage.jpg"); //Create ImageIcon from Image ii = new ImageIcon(imageForSplashScreen); //Set JWindow size from image size setSize(ii.getIconWidth(), ii.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(imageForSplashScreen, 0, 0, this); } public static void main(String[] args) { CreateSplashScreenUsingJWindow cssujw = new CreateSplashScreenUsingJWindow(); try { //Make JWindow appear for 5 seconds before disappear Thread.sleep(5000); cssujw.dispose(); } catch (Exception exception) { exception.printStackTrace(); } JFrame frame = new JFrame("Main Window"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); frame.setVisible(true); } } |