|
| 1 | +package processing.app.ui; |
| 2 | + |
| 3 | +import processing.app.Platform; |
| 4 | + |
| 5 | +import java.awt.Dimension; |
| 6 | +import java.awt.FlowLayout; |
| 7 | +import java.awt.Graphics; |
| 8 | +import java.awt.Image; |
| 9 | +import java.awt.MediaTracker; |
| 10 | +import java.awt.Toolkit; |
| 11 | +import javax.swing.JComponent; |
| 12 | +import javax.swing.JFrame; |
| 13 | +import java.io.File; |
| 14 | + |
| 15 | + |
| 16 | +/** |
| 17 | + * Show a splash screen window. Loosely based on SplashWindow.java from |
| 18 | + * Werner Randelshofer, but rewritten to use Swing because the java.awt |
| 19 | + * version doesn't render properly on Windows. |
| 20 | + */ |
| 21 | +public class Splash extends JFrame { |
| 22 | + static private Splash instance; |
| 23 | + private Image image; |
| 24 | + |
| 25 | + |
| 26 | + private boolean paintCalled = false; |
| 27 | + |
| 28 | + private Splash(File imageFile, boolean hidpi) { |
| 29 | + this.image = |
| 30 | + Toolkit.getDefaultToolkit().createImage(imageFile.getAbsolutePath()); |
| 31 | + |
| 32 | + // Load the image |
| 33 | + MediaTracker mt = new MediaTracker(this); |
| 34 | + mt.addImage(image,0); |
| 35 | + try { |
| 36 | + mt.waitForID(0); |
| 37 | + } catch(InterruptedException ie){} |
| 38 | + |
| 39 | + // Abort on failure |
| 40 | + if (mt.isErrorID(0)) { |
| 41 | + setSize(0,0); |
| 42 | + System.err.println("Warning: SplashWindow couldn't load splash image."); |
| 43 | + synchronized(this) { |
| 44 | + paintCalled = true; |
| 45 | + notifyAll(); |
| 46 | + } |
| 47 | + } else { |
| 48 | + // Center the window on the screen |
| 49 | + final int imgWidth = image.getWidth(this); |
| 50 | + final int imgHeight = image.getHeight(this); |
| 51 | + final int imgScale = hidpi ? 2 : 1; |
| 52 | + |
| 53 | + setUndecorated(true); |
| 54 | + |
| 55 | + JComponent comp = new JComponent() { |
| 56 | + public void paintComponent(Graphics g) { |
| 57 | + System.out.println("drawing " + getSize() + " " + Splash.this.getSize()); |
| 58 | + g.drawImage(image, 0, 0, imgWidth / imgScale, imgHeight / imgScale, this); |
| 59 | + } |
| 60 | + |
| 61 | + public Dimension getPreferredSize() { |
| 62 | + return new Dimension(imgWidth / imgScale, imgHeight / imgScale); |
| 63 | + } |
| 64 | + }; |
| 65 | + comp.setSize(imgWidth, imgHeight); |
| 66 | + setLayout(new FlowLayout()); |
| 67 | + getContentPane().add(comp); |
| 68 | + pack(); |
| 69 | + |
| 70 | + setLocationRelativeTo(null); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + /** Open a splash window using the specified image. */ |
| 76 | + static void showSplash(File imageFile, boolean hidpi) { |
| 77 | + if (instance == null) { |
| 78 | + instance = new Splash(imageFile, hidpi); |
| 79 | + instance.setVisible(true); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + /** Closes the splash window when finished. */ |
| 85 | + static void disposeSplash() { |
| 86 | + if (instance != null) { |
| 87 | + //instance.getOwner().dispose(); |
| 88 | + instance.dispose(); |
| 89 | + instance = null; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + /** |
| 95 | + * Invokes the main method of the provided class name. |
| 96 | + * @param args the command line arguments |
| 97 | + */ |
| 98 | + static void invokeMain(String className, String[] args) { |
| 99 | + try { |
| 100 | + Class.forName(className) |
| 101 | + .getMethod("main", new Class[] {String[].class}) |
| 102 | + .invoke(null, new Object[] { args }); |
| 103 | + |
| 104 | + } catch (Exception e) { |
| 105 | + InternalError error = new InternalError("Failed to invoke main method"); |
| 106 | + error.initCause(e); |
| 107 | + throw error; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + static public void main(String[] args) { |
| 113 | + try { |
| 114 | + final boolean hidpi = processing.app.ui.Toolkit.highResImages(); |
| 115 | + final String filename = "lib/about-" + (hidpi ? 2 : 1) + "x.png"; |
| 116 | + File splashFile = Platform.getContentFile(filename); |
| 117 | + showSplash(splashFile, hidpi); |
| 118 | + invokeMain("processing.app.Base", args); |
| 119 | + disposeSplash(); |
| 120 | + |
| 121 | + } catch (Exception e) { |
| 122 | + e.printStackTrace(); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments