KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > boot > SplashWindow


1 // This class originally taken from
2
// http://www.randelshofer.ch/oop/javasplash/javasplash.html
3
/*
4  * @(#)SplashWindow.java 2.2 2005-04-03
5  *
6  * Copyright (c) 2003-2005 Werner Randelshofer
7  * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
8  * All rights reserved.
9  *
10  * This software is in the public domain.
11  */

12 package org.java.plugin.boot;
13
14 import java.awt.Dimension JavaDoc;
15 import java.awt.EventQueue JavaDoc;
16 import java.awt.Frame JavaDoc;
17 import java.awt.Graphics JavaDoc;
18 import java.awt.Image JavaDoc;
19 import java.awt.MediaTracker JavaDoc;
20 import java.awt.Toolkit JavaDoc;
21 import java.awt.Window JavaDoc;
22 import java.awt.event.MouseAdapter JavaDoc;
23 import java.awt.event.MouseEvent JavaDoc;
24 import java.net.URL JavaDoc;
25
26 /**
27  * A Splash window.
28  * <p>
29  * Usage: MyApplication is your application class. Create a Splasher class which
30  * opens the splash window, invokes the main method of your Application class,
31  * and disposes the splash window afterwards.
32  * Please note that we want to keep the Splasher class and the SplashWindow class
33  * as small as possible. The less code and the less classes must be loaded into
34  * the JVM to open the splash screen, the faster it will appear.
35  * <pre>
36  * class Splasher {
37  * public static void main(String[] args) {
38  * SplashWindow.splash(Startup.class.getResource("splash.gif"));
39  * MyApplication.main(args);
40  * SplashWindow.disposeSplash();
41  * }
42  * }
43  * </pre>
44  *
45  * @author Werner Randelshofer
46  * @version 2.1 2005-04-03 Revised.
47  * @version $Id: SplashWindow.java,v 1.2 2006/08/26 15:14:10 ddimon Exp $
48  */

49 final class SplashWindow extends Window JavaDoc {
50     private static final long serialVersionUID = 7264517933349367876L;
51
52     /**
53      * The current instance of the splash window.
54      * (Singleton design pattern).
55      */

56     private static SplashWindow instance;
57     
58     /**
59      * The splash image which is displayed on the splash window.
60      */

61     private Image JavaDoc image;
62     
63     /**
64      * This attribute indicates whether the method
65      * paint(Graphics) has been called at least once since the
66      * construction of this window.<br>
67      * This attribute is used to notify method splash(Image)
68      * that the window has been drawn at least once
69      * by the AWT event dispatcher thread.<br>
70      * This attribute acts like a latch. Once set to true,
71      * it will never be changed back to false again.
72      *
73      * @see #paint(Graphics)
74      * @see #splash(URL)
75      */

76     boolean paintCalled = false;
77     
78     /**
79      * Creates a new instance.
80      * @param parent the parent of the window.
81      * @param image the splash image.
82      */

83     private SplashWindow(final Frame JavaDoc parent, final Image JavaDoc anImage) {
84         super(parent);
85         image = anImage;
86
87         // Load the image
88
MediaTracker JavaDoc mt = new MediaTracker JavaDoc(this);
89         mt.addImage(image,0);
90         try {
91             mt.waitForID(0);
92         } catch(InterruptedException JavaDoc ie){
93             // ignore
94
}
95         
96         // Center the window on the screen
97
int imgWidth = image.getWidth(this);
98         int imgHeight = image.getHeight(this);
99         setSize(imgWidth, imgHeight);
100         Dimension JavaDoc screenDim = Toolkit.getDefaultToolkit().getScreenSize();
101         setLocation(
102         (screenDim.width - imgWidth) / 2,
103         (screenDim.height - imgHeight) / 2
104         );
105         
106         // Users shall be able to close the splash window by
107
// clicking on its display area. This mouse listener
108
// listens for mouse clicks and disposes the splash window.
109
MouseAdapter JavaDoc disposeOnClick = new MouseAdapter JavaDoc() {
110             public void mouseClicked(final MouseEvent JavaDoc evt) {
111                 // Note: To avoid that method splash hangs, we
112
// must set paintCalled to true and call notifyAll.
113
// This is necessary because the mouse click may
114
// occur before the contents of the window
115
// has been painted.
116
synchronized(SplashWindow.this) {
117                     SplashWindow.this.paintCalled = true;
118                     SplashWindow.this.notifyAll();
119                 }
120                 dispose();
121             }
122         };
123         addMouseListener(disposeOnClick);
124     }
125     
126     /**
127      * @see java.awt.Component#update(java.awt.Graphics)
128      */

129     public void update(final Graphics JavaDoc g) {
130         // Note: Since the paint method is going to draw an
131
// image that covers the complete area of the component we
132
// do not fill the component with its background color
133
// here. This avoids flickering.
134
paint(g);
135     }
136     
137     /**
138      * @see java.awt.Component#paint(java.awt.Graphics)
139      */

140     public void paint(final Graphics JavaDoc g) {
141         g.drawImage(image, 0, 0, this);
142         
143         // Notify method splash that the window
144
// has been painted.
145
// Note: To improve performance we do not enter
146
// the synchronized block unless we have to.
147
if (!paintCalled) {
148             paintCalled = true;
149             synchronized (this) {
150                 notifyAll();
151             }
152         }
153     }
154     
155     /**
156      * Open's a splash window using the specified image.
157      * @param image The splash image.
158      */

159     private static void splash(final Image JavaDoc image) {
160         if (instance == null && image != null) {
161             Frame JavaDoc f = new Frame JavaDoc();
162             
163             // Create the splash image
164
instance = new SplashWindow(f, image);
165             
166             // Show the window.
167
instance.show();
168             
169             // Note: To make sure the user gets a chance to see the
170
// splash window we wait until its paint method has been
171
// called at least once by the AWT event dispatcher thread.
172
// If more than one processor is available, we don't wait,
173
// and maximize CPU throughput instead.
174
if (! EventQueue.isDispatchThread()
175                     && Runtime.getRuntime().availableProcessors() == 1) {
176                 synchronized (instance) {
177                     while (!instance.paintCalled) {
178                         try {
179                             instance.wait();
180                         } catch (InterruptedException JavaDoc ie) {
181                             // ignore
182
}
183                     }
184                 }
185             }
186         }
187     }
188     /**
189      * Open's a splash window using the specified image.
190      * @param imageURL The url of the splash image.
191      */

192     static void splash(final URL JavaDoc imageURL) {
193         if (imageURL != null) {
194             splash(Toolkit.getDefaultToolkit().createImage(imageURL));
195         }
196     }
197     
198     /**
199      * Closes the splash window.
200      */

201     static void disposeSplash() {
202         if (instance != null) {
203             instance.getOwner().dispose();
204             instance = null;
205         }
206     }
207 }
208
Popular Tags