下面列出了怎么用java.awt.SplashScreen的API类实例代码及写法,或者点击链接到github查看源代码。
/**
*
* @Title : closeSplashScreen
* @Description: close splash screen
* @Param :
* @Return : void
* @Throws :
*/
public static void closeSplashScreen()
{
SplashScreen ss = SplashScreen.getSplashScreen();
if (null == ss)
{
return;
}
try
{
ss.close();
}
catch(Exception e)
{
// Ignore this exception
}
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
private static void updateSplashMessage(SplashScreen splash, String message) {
// Splash screen may not be present
if (splash != null) {
Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 10);
Graphics2D g = splash.createGraphics();
g.setFont(font);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// Wipe out any previous text
g.setColor(new Color(238, 238, 238)); // #EEEEEE
g.setPaintMode();
g.fillRect(12, 70, 250, 30); // (x,y) is top left corner of area
// Draw next text
g.setColor(new Color(96, 96, 96)); // #606060
g.setPaintMode();
g.drawString(message, 17, 86); // (x,y) is baseline of text
splash.update();
}
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
/**
* Sets splash screen as defined in JAR manifest or via "-splash" command line.
* <p>
* Can override with custom splash by placing "splash.png" in mods folder.
*/
private static void setSplashScreen() {
splash = SplashScreen.getSplashScreen();
if (splash == null) {
System.err.println("Error: no splash image specified on the command line");
} else {
reporter = new SplashProgressReporter(splash);
try {
final File splashFile = MagicFileSystem.getDataPath(DataPath.MODS).resolve("splash.png").toFile();
if (splashFile.exists()) {
splash.setImageURL(splashFile.toURI().toURL());
}
} catch (IOException ex) {
// A problem occurred trying to set custom splash.
// Log error and use default splash screen.
System.err.println(ex);
}
}
}
private static void updateSplashIfNeeded(String message[]) {
boolean headless_check = isHeadless();
if (!headless_check) {
SplashScreen splash = SplashScreen.getSplashScreen();
if (splash == null)
return;
if (splash.isVisible()) {
//Get a graphics overlay for the splash screen
Graphics2D g = splash.createGraphics();
//Do some drawing on the graphics object
//Now update to the splash screen
g.setComposite(AlphaComposite.Clear);
g.fillRect(0,0,400,70);
g.setPaintMode();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.BLACK);
g.setFont(new Font("Arial",Font.BOLD,11));
for (int i=0;i<message.length;i++)
g.drawString(message[i], 13, 16*i+10);
splash.update();
}
}
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
if(splashBounds.width != IMAGE_WIDTH){
throw new RuntimeException(
"SplashScreen#getBounds has wrong width");
}
if(splashBounds.height != IMAGE_HEIGHT){
throw new RuntimeException(
"SplashScreen#getBounds has wrong height");
}
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
if(splashBounds.width != IMAGE_WIDTH){
throw new RuntimeException(
"SplashScreen#getBounds has wrong width");
}
if(splashBounds.height != IMAGE_HEIGHT){
throw new RuntimeException(
"SplashScreen#getBounds has wrong height");
}
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
/**
* Decorate the splash screen with the version and date.
*
* @param splash
* the splash screen
*/
protected static void decorateVersionDate(SplashScreen splash) {
if (splash != null) {
Graphics2D g = splash.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(new Color(242, 130, 38));
g.setFont(new Font("SansSerif", Font.BOLD, 11));
g.drawString("version " + getVersion(), 170, 76);
g.drawString(getDateISO(), 170, 91);
splash.update();
}
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
if(splashBounds.width != IMAGE_WIDTH){
throw new RuntimeException(
"SplashScreen#getBounds has wrong width");
}
if(splashBounds.height != IMAGE_HEIGHT){
throw new RuntimeException(
"SplashScreen#getBounds has wrong height");
}
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
if(splashBounds.width != IMAGE_WIDTH){
throw new RuntimeException(
"SplashScreen#getBounds has wrong width");
}
if(splashBounds.height != IMAGE_HEIGHT){
throw new RuntimeException(
"SplashScreen#getBounds has wrong height");
}
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
if (splashBounds.width != IMAGE_WIDTH) {
throw new RuntimeException(
"SplashScreen#getBounds has wrong width");
}
if (splashBounds.height != IMAGE_HEIGHT) {
throw new RuntimeException(
"SplashScreen#getBounds has wrong height");
}
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
if(splashBounds.width != IMAGE_WIDTH){
throw new RuntimeException(
"SplashScreen#getBounds has wrong width");
}
if(splashBounds.height != IMAGE_HEIGHT){
throw new RuntimeException(
"SplashScreen#getBounds has wrong height");
}
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
static void testSplash(ImageInfo test) throws Exception {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
if (splashScreen == null) {
throw new RuntimeException("Splash screen is not shown!");
}
Graphics2D g = splashScreen.createGraphics();
Rectangle splashBounds = splashScreen.getBounds();
int screenX = (int) splashBounds.getCenterX();
int screenY = (int) splashBounds.getCenterY();
if(splashBounds.width != IMAGE_WIDTH){
throw new RuntimeException(
"SplashScreen#getBounds has wrong width");
}
if(splashBounds.height != IMAGE_HEIGHT){
throw new RuntimeException(
"SplashScreen#getBounds has wrong height");
}
Robot robot = new Robot();
Color splashScreenColor = robot.getPixelColor(screenX, screenY);
float scaleFactor = getScaleFactor();
Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
if (!compare(testColor, splashScreenColor)) {
throw new RuntimeException(
"Image with wrong resolution is used for splash screen!");
}
}
/** Splash initialise. */
public static void splashInit() {
splashScreenObj = SplashScreen.getSplashScreen();
if (splashScreenObj != null) {
createTextArea();
// create the Graphics environment for drawing status info
splashGraphics = splashScreenObj.createGraphics();
splashGraphics.setFont(font);
// Display the version number
splashText(getVersionString());
}
}
private static boolean executeSplashInteractor(Runnable splashInteraction) {
if (SplashScreen.getSplashScreen() == null)
return false;
// Non-MacOS
if (!Util.isMac()) {
splashInteraction.run();
closeSplash();
return true;
}
// MacOS
Display display = Display.getDefault();
final Semaphore sem = new Semaphore(0);
Thread splashInteractor = new Thread(() -> {
splashInteraction.run();
sem.release();
display.asyncExec(() -> {});
closeSplash();
});
splashInteractor.start();
// Interact with splash screen
while (!display.isDisposed() && !sem.tryAcquire())
if (!display.readAndDispatch())
display.sleep();
return true;
}
@Override
public void run() {
// Get App Icons
ImageManager imageManager = new ImageManager();
List<Image> appIcons = new ArrayList<Image>();
appIcons.add(imageManager.getImage("icon/app/icon 128x128.png"));
appIcons.add(imageManager.getImage("icon/app/icon 64x64.png"));
appIcons.add(imageManager.getImage("icon/app/icon 32x32.png"));
appIcons.add(imageManager.getImage("icon/app/icon 16x16.png"));
// Hide Splash Screen so the JFrame does not hide when appearing
SplashScreen s = SplashScreen.getSplashScreen();
if (s != null){
s.close();
}
// Initialize Frame
JFrame frame = new JFrame(TinkerTimeLauncher.FULL_NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setIconImages(appIcons);
frame.setJMenuBar(menuBar);
frame.add(toolBar, BorderLayout.NORTH);
frame.add(modSelectorPanelController.getComponent(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.toFront();
}
private static void closeSplashIfneeded() {
if (isHeadless())
return;
SplashScreen splash = SplashScreen.getSplashScreen();
//Check if we have specified any splash screen
if (splash == null) {
return;
}
if (splash.isVisible())
splash.close();
}
private static void closeSplash() {
try {
final SplashScreen splash = SplashScreen.getSplashScreen();
if (splash != null) {
splash.close();
}
} catch (final IllegalStateException e) {
logger.error("Error closing the splash window", e); //$NON-NLS-1$
}
}
public ConsoleUserInterface()
{
SplashScreen.getSplashScreen().close();
}
public SplashDecorator(SplashScreen splash) {
this.splash = splash;
}
public SplashScreen getSplash() {
return splash;
}
/**
* Start the KeyStore Explorer application. Takes one optional argument -
* the location of a KeyStore file to open upon startup.
*
* @param args
* the command line arguments
*/
public static void main(String args[]) {
try {
// To take affect these must be set before the splash screen is instantiated
if (OperatingSystem.isMacOs()) {
setAppleSystemProperties();
} else if (OperatingSystem.isWindows7() || OperatingSystem.isWindows8() || OperatingSystem.isWindows10()) {
String appId = props.getString("KSE.AppUserModelId");
Shell32 shell32 = Native.loadLibrary("shell32", Shell32.class);
shell32.SetCurrentProcessExplicitAppUserModelID(new WString(appId)).longValue();
} else if (OperatingSystem.isLinux()) {
fixAppClassName();
}
setInstallDirProperty();
SplashScreen splash = SplashScreen.getSplashScreen();
updateSplashMessage(splash, res.getString("KSE.LoadingApplicationSettings.splash.message"));
ApplicationSettings applicationSettings = ApplicationSettings.getInstance();
setCurrentDirectory(applicationSettings);
String languageCode = applicationSettings.getLanguage();
if (!ApplicationSettings.SYSTEM_LANGUAGE.equals(languageCode)) {
Locale.setDefault(new Locale(languageCode));
}
updateSplashMessage(splash, res.getString("KSE.InitializingSecurity.splash.message"));
initialiseSecurity();
// list of files to open after start
List<File> parameterFiles = new ArrayList<>();
for (String arg : args) {
File parameterFile = new File(arg);
if (parameterFile.exists()) {
parameterFiles.add(parameterFile);
}
}
// Create application GUI on the event handler thread
updateSplashMessage(splash, res.getString("KSE.CreatingApplicationGui.splash.message"));
SwingUtilities.invokeLater(new CreateApplicationGui(applicationSettings, splash, parameterFiles));
} catch (Throwable t) {
DError dError = new DError(new JFrame(), t);
dError.setLocationRelativeTo(null);
dError.setVisible(true);
System.exit(1);
}
}
private static void closeSplash() {
SplashScreen splash = SplashScreen.getSplashScreen();
if (splash != null)
splash.close();
}
private static void launch(String[] args) {
Display.setAppName(Version.getAppName());
Display.setAppVersion(Version.getVersion());
final Display display = new Display();
OpenDocumentEventProcessor openDocProcessor = new OpenDocumentEventProcessor();
display.addListener(SWT.OpenDocument, openDocProcessor);
openDocProcessor.addFilesToOpen(args);
if (Util.isMac())
executeSplashInteractor(() -> {
try {
Thread.sleep(300);
} catch (InterruptedException e1) {
}
});
try {
Class.forName("org.reldb.rel.Rel");
localRel = true;
} catch (ClassNotFoundException cnfe) {
localRel = false;
}
OSSpecific.launch(Version.getAppName(),
event -> quit(),
event -> new AboutDialog(shell).open(),
event -> new Preferences(shell).show()
);
if (!Util.isMac()) {
SplashScreen splash = SplashScreen.getSplashScreen();
if (splash != null && localRel && !Preferences.getPreferenceBoolean(PreferencePageGeneral.SKIP_DEFAULT_DB_LOAD)) {
Graphics2D gc = splash.createGraphics();
Rectangle rect = splash.getBounds();
int barWidth = rect.width - 20;
int barHeight = 10;
Rectangle progressBarRect = new Rectangle(10, rect.height - 20, barWidth, barHeight);
gc.draw3DRect(progressBarRect.x, progressBarRect.y, progressBarRect.width, progressBarRect.height, false);
gc.setColor(Color.green);
(new Thread(() -> {
while (SplashScreen.getSplashScreen() != null) {
int percent = Loading.getPercentageOfExpectedMessages();
int drawExtent = Math.min(barWidth * percent / 100, barWidth);
gc.fillRect(progressBarRect.x, progressBarRect.y, drawExtent, barHeight);
splash.update();
try {
Thread.sleep(250);
} catch (InterruptedException e) {
}
}
})).start();
}
}
shell = createShell();
shell.setImage(IconLoader.loadIcon("RelIcon"));
shell.setImages(loadIcons(display));
shell.setText(Version.getAppID());
shell.addListener(SWT.Close, e -> {
shell.dispose();
});
shell.addDisposeListener(e -> quit());
shell.layout();
Loading.start();
Core.launch(openDocProcessor, shell);
if (!Util.isMac())
closeSplash();
shell.open();
while (!display.isDisposed()) {
try {
if (!display.readAndDispatch())
display.sleep();
} catch (Throwable t) {
System.out.println("DBrowser: Exception: " + t);
t.printStackTrace();
}
}
}
public SplashProgressReporter(final SplashScreen splash) {
this.splash = splash;
}
private void closeSplash() {
Optional.ofNullable(SplashScreen.getSplashScreen()).ifPresent(SplashScreen::close);
}