1 25 package org.ofbiz.base.splash; 26 27 import java.awt.Dimension ; 28 import java.awt.Frame ; 29 import java.awt.Graphics ; 30 import java.awt.Image ; 31 import java.awt.MediaTracker ; 32 import java.awt.Rectangle ; 33 import java.awt.Toolkit ; 34 import java.awt.Window ; 35 36 public final class SplashScreen extends Frame { 37 38 private final String fImageId; 39 private MediaTracker fMediaTracker; 40 private Window splashWindow; 41 private Image fImage; 42 43 public SplashScreen(String aImageId) { 44 if (aImageId == null || aImageId.trim().length() == 0) { 45 throw new IllegalArgumentException ("Image Id does not have content."); 46 } 47 fImageId = aImageId; 48 } 49 50 public void splash() { 51 initImageAndTracker(); 52 setSize(fImage.getWidth(null), fImage.getHeight(null)); 53 center(); 54 55 fMediaTracker.addImage(fImage, 0); 56 try { 57 fMediaTracker.waitForID(0); 58 } catch (InterruptedException ie) { 59 System.out.println("Cannot track image load."); 60 } 61 62 splashWindow = new SplashWindow(this, fImage); 63 } 64 65 public void close() { 66 this.dispose(); 67 splashWindow.dispose(); 68 splashWindow = null; 69 } 70 71 private void initImageAndTracker() { 72 fMediaTracker = new MediaTracker (this); 73 fImage = Toolkit.getDefaultToolkit().getImage(fImageId); 74 } 75 76 private void center() { 77 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 78 Rectangle frame = getBounds(); 79 setLocation((screen.width - frame.width) / 2, (screen.height - frame.height) / 2); 80 } 81 82 private class SplashWindow extends Window { 83 84 private Image fImage; 85 86 public SplashWindow(Frame aParent, Image aImage) { 87 super(aParent); 88 fImage = aImage; 89 setSize(fImage.getWidth(null), fImage.getHeight(null)); 90 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 91 Rectangle window = getBounds(); 92 setLocation((screen.width - window.width) / 2, (screen.height - window.height) / 2); 93 setVisible(true); 94 } 95 96 public void paint(Graphics graphics) { 97 if (fImage != null) { 98 graphics.drawImage(fImage, 0, 0, this); 99 } 100 } 101 } 102 } 103 | Popular Tags |