1 7 package java.awt; 8 9 import java.io.IOException ; 10 import java.awt.image.*; 11 import java.net.URL ; 12 import java.net.URLConnection ; 13 import java.io.File ; 14 15 16 67 public final class SplashScreen { 68 69 SplashScreen(long ptr) { splashPtr = ptr; 71 wasClosed = false; 72 } 73 74 85 public static synchronized SplashScreen getSplashScreen() { 86 if (GraphicsEnvironment.isHeadless()) { 87 throw new HeadlessException (); 88 } 89 if (SplashScreen.theInstance == null) { 91 java.security.AccessController.doPrivileged( 92 new sun.security.action.LoadLibraryAction("splashscreen")); 93 long ptr = _getInstance(); 94 if (ptr == 0) { 95 return null; 96 } 97 if (!_isVisible(ptr)) { 98 return null; 99 } 100 SplashScreen.theInstance = new SplashScreen (ptr); 101 } 102 return (theInstance.isVisible() ? theInstance : null); 103 } 104 105 120 public void setImageURL(URL imageURL) throws NullPointerException , IOException , IllegalStateException { 121 checkVisible(); 122 URLConnection connection = imageURL.openConnection(); 123 connection.connect(); 124 int length = connection.getContentLength(); 125 java.io.InputStream stream = connection.getInputStream(); 126 byte[] buf = new byte[length]; 127 int off = 0; 128 while(true) { 129 int available = stream.available(); 131 if (available <= 0) { 132 available = 1; 135 } 136 if (off + available > length) { 139 length = off*2; 140 if (off + available > length) { 141 length = available+off; 142 } 143 byte[] oldBuf = buf; 144 buf = new byte[length]; 145 System.arraycopy(oldBuf, 0, buf, 0, off); 146 } 147 int result = stream.read(buf, off, available); 149 if (result < 0) { 150 break; 151 } 152 off += result; 153 } 154 synchronized(this) { 155 if (!_setImageData(splashPtr, buf)) { 156 throw new IOException ("Bad image format or i/o error when loading image"); 157 } 158 this.imageURL = imageURL; 159 } 160 } 161 162 private void checkVisible() { 163 if (!isVisible()) { 164 throw new IllegalStateException ("no splash screen available"); 165 } 166 } 167 173 public synchronized URL getImageURL() throws IllegalStateException { 174 checkVisible(); 175 if (imageURL == null) { 176 try { 177 String fileName = _getImageFileName(splashPtr); 178 String jarName = _getImageJarName(splashPtr); 179 if (fileName != null) { 180 if (jarName != null) { 181 imageURL = new URL ("jar:"+(new File (jarName).toURL().toString())+"!/"+fileName); 182 } else { 183 imageURL = new File (fileName).toURL(); 184 } 185 } 186 } 187 catch(java.net.MalformedURLException e) { 188 } 190 } 191 return imageURL; 192 } 193 194 205 public Rectangle getBounds() throws IllegalStateException { 206 checkVisible(); 207 return _getBounds(splashPtr); 208 } 209 210 221 public Dimension getSize() throws IllegalStateException { 222 return getBounds().getSize(); 223 } 224 225 238 public Graphics2D createGraphics() throws IllegalStateException { 239 if (image==null) { 240 Dimension dim = getSize(); 241 image = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); 242 } 243 return image.createGraphics(); 244 } 245 246 253 public void update() throws IllegalStateException { 254 checkVisible(); 255 if (image == null) { 256 throw new IllegalStateException ("no overlay image available"); 257 } 258 DataBuffer buf = image.getRaster().getDataBuffer(); 259 if (!(buf instanceof DataBufferInt)) { 260 throw new AssertionError ("Overlay image DataBuffer is of invalid type == "+buf.getClass().getName()); 261 } 262 int numBanks = buf.getNumBanks(); 263 if (numBanks!=1) { 264 throw new AssertionError ("Invalid number of banks =="+numBanks+" in overlay image DataBuffer"); 265 } 266 if (!(image.getSampleModel() instanceof SinglePixelPackedSampleModel)) { 267 throw new AssertionError ("Overlay image has invalid sample model == "+image.getSampleModel().getClass().getName()); 268 } 269 SinglePixelPackedSampleModel sm = (SinglePixelPackedSampleModel)image.getSampleModel(); 270 int scanlineStride = sm.getScanlineStride(); 271 Rectangle rect = image.getRaster().getBounds(); 272 int[] data = ((DataBufferInt)buf).getData(); 273 _update(splashPtr, data, rect.x, rect.y, rect.width, rect.height, scanlineStride); 274 } 275 276 282 public synchronized void close() throws IllegalStateException { 283 checkVisible(); 284 _close(splashPtr); 285 image = null; 286 wasClosed = true; 287 } 291 292 293 301 public boolean isVisible() { 302 return !wasClosed && _isVisible(splashPtr); 303 } 304 305 private BufferedImage image; 307 private long splashPtr; private boolean wasClosed; 309 310 private URL imageURL; 311 312 319 private static SplashScreen theInstance = null; 320 321 private native static void _update(long splashPtr, int[] data, int x, int y, int width, int height, int scanlineStride); 322 private native static boolean _isVisible(long splashPtr); 323 private native static Rectangle _getBounds(long splashPtr); 324 private native static long _getInstance(); 325 private native static void _close(long splashPtr); 326 private native static String _getImageFileName(long splashPtr); 327 private native static String _getImageJarName(long SplashPtr); 328 private native static boolean _setImageData(long SplashPtr, byte[] data); 329 330 }; 331 332 | Popular Tags |