1 12 package org.java.plugin.boot; 13 14 import java.awt.Dimension ; 15 import java.awt.EventQueue ; 16 import java.awt.Frame ; 17 import java.awt.Graphics ; 18 import java.awt.Image ; 19 import java.awt.MediaTracker ; 20 import java.awt.Toolkit ; 21 import java.awt.Window ; 22 import java.awt.event.MouseAdapter ; 23 import java.awt.event.MouseEvent ; 24 import java.net.URL ; 25 26 49 final class SplashWindow extends Window { 50 private static final long serialVersionUID = 7264517933349367876L; 51 52 56 private static SplashWindow instance; 57 58 61 private Image image; 62 63 76 boolean paintCalled = false; 77 78 83 private SplashWindow(final Frame parent, final Image anImage) { 84 super(parent); 85 image = anImage; 86 87 MediaTracker mt = new MediaTracker (this); 89 mt.addImage(image,0); 90 try { 91 mt.waitForID(0); 92 } catch(InterruptedException ie){ 93 } 95 96 int imgWidth = image.getWidth(this); 98 int imgHeight = image.getHeight(this); 99 setSize(imgWidth, imgHeight); 100 Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); 101 setLocation( 102 (screenDim.width - imgWidth) / 2, 103 (screenDim.height - imgHeight) / 2 104 ); 105 106 MouseAdapter disposeOnClick = new MouseAdapter () { 110 public void mouseClicked(final MouseEvent evt) { 111 synchronized(SplashWindow.this) { 117 SplashWindow.this.paintCalled = true; 118 SplashWindow.this.notifyAll(); 119 } 120 dispose(); 121 } 122 }; 123 addMouseListener(disposeOnClick); 124 } 125 126 129 public void update(final Graphics g) { 130 paint(g); 135 } 136 137 140 public void paint(final Graphics g) { 141 g.drawImage(image, 0, 0, this); 142 143 if (!paintCalled) { 148 paintCalled = true; 149 synchronized (this) { 150 notifyAll(); 151 } 152 } 153 } 154 155 159 private static void splash(final Image image) { 160 if (instance == null && image != null) { 161 Frame f = new Frame (); 162 163 instance = new SplashWindow(f, image); 165 166 instance.show(); 168 169 if (! EventQueue.isDispatchThread() 175 && Runtime.getRuntime().availableProcessors() == 1) { 176 synchronized (instance) { 177 while (!instance.paintCalled) { 178 try { 179 instance.wait(); 180 } catch (InterruptedException ie) { 181 } 183 } 184 } 185 } 186 } 187 } 188 192 static void splash(final URL imageURL) { 193 if (imageURL != null) { 194 splash(Toolkit.getDefaultToolkit().createImage(imageURL)); 195 } 196 } 197 198 201 static void disposeSplash() { 202 if (instance != null) { 203 instance.getOwner().dispose(); 204 instance = null; 205 } 206 } 207 } 208 | Popular Tags |