How to make a splash screen for GUI?
Asked Answered
A

6

11

Hi there I'm new to GUIs in Java and was trying to make a splash screen or an image appear for 3 seconds. Then after that it it will go onto my main program. Does anyone have an ideas how to do this or can link me to any tutorials?

So far I have done this but not sure where to go from here.

public static void main(String[] args)
{
    splashInit();           // initialize splash overlay drawing parameters
    appInit();              // simulate what an application would do 
}
Acuff answered 21/4, 2013 at 18:14 Comment(3)
Try this link , and this exampleVeneration
Simplest way is to sleep the thread for 3000 milliseconds before you show the application. Thread.sleep(3000);Remorseful
@GnomezGrave: better to use a Swing Timer if you don't want to step on the swing event thread.Digress
V
11

Simplest one , is to create JFrame and add your screen on it then use Thread.Sleep(long millies)

Try this code:

JWindow window = new JWindow();
window.getContentPane().add(
    new JLabel("", new ImageIcon(new URL("http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/SplashDemoProject/src/misc/images/splash.gif")), SwingConstants.CENTER));
window.setBounds(500, 150, 300, 200);
window.setVisible(true);
try {
    Thread.sleep(5000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
window.setVisible(false);
JFrame frame = new JFrame();
frame.add(new JLabel("Welcome"));
frame.setVisible(true);
frame.setSize(300,100);
window.dispose();

Or you can Create a Splash Screen by using SplashScreen class

Vestal answered 21/4, 2013 at 18:49 Comment(3)
Right idea, wrong direction, never call Thread.sleep within the EDT or create/modify Swing UI components from out side the EDTWeiman
@Weiman To be fair: The official tutorial also creates the UI on the main thread :-/ I have never used splash screens, but wonder about the proper interplay when it is created on the EDT, considering the ... un-swingy way of how renderSplashFrame looks, is implemented, and called in the SplashDemo.java example...Beatify
@Beatify Yes, what a wonderful world of confusion it has created - there was a wonderful blog post which went through the history of the issue, but needless to say, it ended up at the same point, use EventQueue.invokeLater to spin up the UI, because you can’t be sure what thread main is called on. It’s been awhile since I’ve used splash screen, but I believe it runs off the EDT (takes to long to spin it up and it might cause race conditions), so the rules are different, because consistency would be to easy 🤪🤣Weiman
P
11

See also How to Create a Splash Screen for the AWT based splash functionality.

splash image

Puett answered 22/4, 2013 at 13:55 Comment(0)
M
0

To print messages on the splash screen, you need to add a picture to your application and add it to the manifest.mf:

SplashScreen-Image: path/picture.jpg

Then use code like this:

private static Graphics2D splashGraphics = null;
private static SplashScreen splash;
private static int dpi;

public static void main(String[] args) {
    initSplashMessages();
    splashMessage("Start program...");
    ...
    splashMessage("Load data...");
    ...
}

private static void initSplashMessages() {
    splash = SplashScreen.getSplashScreen();
    if (splash == null) return;
    splashGraphics = splash.createGraphics();
    if (splashGraphics == null) return;
    dpi = Toolkit.getDefaultToolkit().getScreenResolution();
}

public static void splashMessage(String message) {
    if (splashGraphics == null) return;
    Dimension dim = splash.getSize();
    splashGraphics.setColor(Color.LIGHT_GRAY);
    splashGraphics.fillRect(0, dim.height - dpiX(20), dim.width, dpiX(20));
    splashGraphics.setPaintMode();
    splashGraphics.setColor(Color.BLACK);
    splashGraphics.setFont(new Font("Arial",Font.PLAIN, dpiX(12)));
    splashGraphics.drawString(message, dpiX(3), dim.height - dpiX(5));
    splash.update();
}

private static int dpiX(int x) {
    return (int) Math.round(1.0 * x * dpi / 96);
}

In IntelliJ add the picture.jpg to your project (use File): enter image description here

For debugging use "Modify options"/"Add VM options" and write -splash:path/picture.jpg enter image description here

Misgiving answered 13/9, 2022 at 21:36 Comment(0)
C
0

I created a Class to be used with statics methods:

public class SplashScreen {

    public static final JWindow SPLASH_SCREEN;

    static {
        SPLASH_SCREEN = new JWindow();
    }

    public static void show() {
        SPLASH_SCREEN.getContentPane().add(
                new JLabel("Loading..." , SwingConstants.CENTER));
        SPLASH_SCREEN.setSize(300, 200);
        SPLASH_SCREEN.setLocationRelativeTo(null);
        SPLASH_SCREEN.setVisible(true);
        SPLASH_SCREEN.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    }

    public static void close() {
        SPLASH_SCREEN.dispose();
    }
}

and in main():

public static void main(String[] args) {

    SplashScreen.show();
    final Controller controller;
    try {
        controller = loadController();
    } finally {
        SplashScreen.close();
    }
    ...
}
Coatee answered 7/4, 2024 at 9:33 Comment(0)
M
-2

This works fine for me.The functions such as getScreenSize() ,getWidth() and getHeight() can be replaced by own values.

public class splash extends JWindow 
{
  public splash()
  {
     JWindow j=new JWindow();

     Dimension d=Toolkit.getDefaultToolkit().getScreenSize();

     Icon img= new ImageIcon(this.getClass().getResource("2.jpg"));
     JLabel label = new JLabel(img);
     label.setSize(200,300);
     j.getContentPane().add(label);
     j.setBounds(((int)d.getWidth()-722)/2,((int)d.getHeight()-401)/2,722,401);
     j.setVisible(true);
     try
     {
        Thread.sleep(6000);
     }
    catch(InterruptedException e)
    {
        e.printStackTrace();
    }
     j.setVisible(false);

  }
  public static void main(String[] args)
  {
    splash s=new splash();
  }
}
Milson answered 6/8, 2019 at 16:46 Comment(0)
D
-2

I use this code. Maybe you need to change some parts:

import javax.swing.*;
import java.awt.*;

public class SplashScreen {
    private final JWindow window;
    private long startTime;
    private int minimumMilliseconds;

    public SplashScreen() {
        window = new JWindow();
        var image = new ImageIcon("C:\\example.jpg");
        window.getContentPane().add(new JLabel("", image, SwingConstants.CENTER));
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        window.setBounds((int) ((screenSize.getWidth() - image.getIconWidth()) / 2),
                (int) ((screenSize.getHeight() - image.getIconHeight()) / 2),
                image.getIconWidth(), image.getIconHeight());
    }

    public void show(int minimumMilliseconds) {
        this.minimumMilliseconds = minimumMilliseconds;
        window.setVisible(true);
        startTime = System.currentTimeMillis();
    }

    public void hide() {
        long elapsedTime = System.currentTimeMillis() - startTime;
        try {
            Thread.sleep(Math.max(minimumMilliseconds - elapsedTime, 0));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        window.setVisible(false);
    }
}

And here is how to use it:

    var splash = new SplashScreen();
    splash.show(2000);

    // Initializing...

    splash.hide();

This will show the splash at least 2 seconds.

Decennium answered 24/9, 2019 at 17:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.