KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > splash > SplashScreen


1 /*
2  * $Id: SplashScreen.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.splash;
26
27 import java.awt.Dimension JavaDoc;
28 import java.awt.Frame JavaDoc;
29 import java.awt.Graphics JavaDoc;
30 import java.awt.Image JavaDoc;
31 import java.awt.MediaTracker JavaDoc;
32 import java.awt.Rectangle JavaDoc;
33 import java.awt.Toolkit JavaDoc;
34 import java.awt.Window JavaDoc;
35
36 public final class SplashScreen extends Frame JavaDoc {
37
38     private final String JavaDoc fImageId;
39     private MediaTracker JavaDoc fMediaTracker;
40     private Window JavaDoc splashWindow;
41     private Image JavaDoc fImage;
42
43     public SplashScreen(String JavaDoc aImageId) {
44         if (aImageId == null || aImageId.trim().length() == 0) {
45             throw new IllegalArgumentException JavaDoc("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 JavaDoc 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 JavaDoc(this);
73         fImage = Toolkit.getDefaultToolkit().getImage(fImageId);
74     }
75     
76     private void center() {
77         Dimension JavaDoc screen = Toolkit.getDefaultToolkit().getScreenSize();
78         Rectangle JavaDoc frame = getBounds();
79         setLocation((screen.width - frame.width) / 2, (screen.height - frame.height) / 2);
80     }
81
82     private class SplashWindow extends Window JavaDoc {
83
84         private Image JavaDoc fImage;
85
86         public SplashWindow(Frame JavaDoc aParent, Image JavaDoc aImage) {
87             super(aParent);
88             fImage = aImage;
89             setSize(fImage.getWidth(null), fImage.getHeight(null));
90             Dimension JavaDoc screen = Toolkit.getDefaultToolkit().getScreenSize();
91             Rectangle JavaDoc window = getBounds();
92             setLocation((screen.width - window.width) / 2, (screen.height - window.height) / 2);
93             setVisible(true);
94         }
95
96         public void paint(Graphics JavaDoc graphics) {
97             if (fImage != null) {
98                 graphics.drawImage(fImage, 0, 0, this);
99             }
100         }
101     }
102 }
103
Popular Tags