¿Cómo uso SplashScreen sin lanzar una NullPointerException?


No importa lo que intente,SplashScreen.getSplashScreen() essiempre null.

Desde la búsqueda en línea veo que este es un problema común y que tiene algo que ver con no dar elSplashScreen Una imagen para usar ... Por lo tanto, al navegar por los métodos me pareció quesetImageURL(URL) debería ser usado. Esto todavía no funciona.

Hay preguntas similares en SO, tales comoesta, que no son útiles y parecen sugerir el uso de una gran cantidad de complementos o la creación de una clase desde cero que se extiende desdeFrame. Incluso elTutorial de Oracle es críptico, y no describe cada paso lógico en el usoSplashScreen correctamente...

Si no es posible o innecesariamente difícil de usarSplashScreen, ¿hay alguna alternativa para hacer esto? ¿O alguien ha eliminado un enfoque simple de este problema?

Aquí está mi intento:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.SplashScreen;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;

/**
 */
public final class MainGUI implements ActionListener {

    /**
     * @throws IOException 
     * @throws IllegalStateException 
     * @throws NullPointerException 
     */
    private final static void showSplashScreen() throws NullPointerException, IllegalStateException, IOException {
        final SplashScreen splash = SplashScreen.getSplashScreen();
        Graphics2D graphics = splash.createGraphics();

        // adding image here:
        URL imageSource = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/7/76/Space_Shuttle_Atlantis_approaching_the_Kennedy_Space_Center_to_land_following_STS-122.jpg/800px-Space_Shuttle_Atlantis_approaching_the_Kennedy_Space_Center_to_land_following_STS-122.jpg");
        splash.setImageURL(imageSource);

        // coordinates and dimensions:
        int x = 100, y = x;
        int width = 500, height = width;

        // (x, y), width, height:
        graphics.create(x, y, width, height);

        graphics.setBackground(Color.BLUE);

        // adding and centering the text:
        graphics.drawString("centered text", (x + width)/2, (y + height)/2);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            showSplashScreen();
        } catch (NullPointerException | IllegalStateException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
} // end of MainGUI

Respuestas a la pregunta(1)

Su respuesta a la pregunta