No matter what I try, SplashScreen.getSplashScreen()
is always null
.
From searching online I see that this is a common issue, and that it has something to do with not giving the SplashScreen
an image to use... Therefore, in navigating the methods it seemed to me that setImageURL(URL)
should be used. This is still not working.
There are similar questions on SO, such as this, which are not helpful and seem to suggest using a myriad of plugins or creating such a class from scratch extending from Frame
. Even the Oracle tutorial is cryptic, and doesn't outline each logical step in using SplashScreen
correctly...
If it is not possible or needlessly difficult to use SplashScreen
, is there any alternative to doing this? Or has anyone hacked out a simple approach to this problem?
Here is my attempt:
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