-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathSplashWindow.java
More file actions
226 lines (206 loc) · 7.21 KB
/
SplashWindow.java
File metadata and controls
226 lines (206 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package processing.app.ui;
/*
* @(#)SplashWindow.java 2.2.1 2006-05-27
*
* Copyright (c) 2003-2010 Werner Randelshofer
* Hausmatt 10, Immensee, CH-6405, Switzerland.
*
* This software is in the public domain.
* You are free to use, adapt, copy and license this work
* without having to attribute to Werner Randelshofer.
*/
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
/**
* A Splash window.
* <p>
* Usage: MyApplication is your application class. Create a Splasher class which
* opens the splash window, invokes the main method of your Application class,
* and disposes the splash window afterwards.
* Please note that we want to keep the Splasher class and the SplashWindow class
* as small as possible. The less code and the less classes must be loaded into
* the JVM to open the splash screen, the faster it will appear.
* <pre>
* class Splasher {
* public static void main(String[] args) {
* SplashWindow.splash(Startup.class.getResource("splash.gif"));
* MyApplication.main(args);
* SplashWindow.disposeSplash();
* }
* }
* </pre>
*
* @author Werner Randelshofer
* @version 2.2.1 2006-05-27 Abort when splash image can not be loaded.
*/
public class SplashWindow extends Window {
/**
* The current instance of the splash window.
* (Singleton design pattern).
*/
private static SplashWindow instance;
/**
* The splash image which is displayed on the splash window.
*/
private Image image;
/**
* This attribute indicates whether the method
* paint(Graphics) has been called at least once since the
* construction of this window.<br>
* This attribute is used to notify method splash(Image)
* that the window has been drawn at least once
* by the AWT event dispatcher thread.<br>
* This attribute acts like a latch. Once set to true,
* it will never be changed back to false again.
*
* @see #paint
* @see #splash
*/
private boolean paintCalled = false;
/**
* Creates a new instance.
* @param parent the parent of the window.
* @param image the splash image.
*/
private SplashWindow(Frame parent, Image image, boolean hidpi) {
super(parent);
this.image = image;
// Load the image
MediaTracker mt = new MediaTracker(this);
mt.addImage(image,0);
try {
mt.waitForID(0);
} catch(InterruptedException ie){}
// Abort on failure
if (mt.isErrorID(0)) {
setSize(0,0);
System.err.println("Warning: SplashWindow couldn't load splash image.");
synchronized(this) {
paintCalled = true;
notifyAll();
}
return;
}
// Center the window on the screen
int imgWidth = image.getWidth(this);
int imgHeight = image.getHeight(this);
if (hidpi) {
imgWidth /= 2;
imgHeight /= 2;
}
setSize(imgWidth, imgHeight);
// Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
// setLocation((screenDim.width - imgWidth) / 2,
// (screenDim.height - imgHeight) / 2);
setLocationRelativeTo(null);
// Users shall be able to close the splash window by
// clicking on its display area. This mouse listener
// listens for mouse clicks and disposes the splash window.
MouseAdapter disposeOnClick = new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
// Note: To avoid that method splash hangs, we
// must set paintCalled to true and call notifyAll.
// This is necessary because the mouse click may
// occur before the contents of the window
// has been painted.
synchronized(SplashWindow.this) {
SplashWindow.this.paintCalled = true;
SplashWindow.this.notifyAll();
}
dispose();
}
};
addMouseListener(disposeOnClick);
}
/**
* Updates the display area of the window.
*/
public void update(Graphics g) {
// Note: Since the paint method is going to draw an
// image that covers the complete area of the component we
// do not fill the component with its background color
// here. This avoids flickering.
paint(g);
}
/**
* Paints the image on the window.
*/
public void paint(Graphics g) {
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
// Notify method splash that the window
// has been painted.
// Note: To improve performance we do not enter
// the synchronized block unless we have to.
if (! paintCalled) {
paintCalled = true;
synchronized (this) { notifyAll(); }
}
}
/**
* Open's a splash window using the specified image.
* @param image The splash image.
*/
public static void splash(Image image, boolean hidpi) {
if (instance == null && image != null) {
Frame f = new Frame();
// Create the splash image
instance = new SplashWindow(f, image, hidpi);
// Show the window.
instance.setVisible(true);
// Note: To make sure the user gets a chance to see the
// splash window we wait until its paint method has been
// called at least once by the AWT event dispatcher thread.
// If more than one processor is available, we don't wait,
// and maximize CPU throughput instead.
if (! EventQueue.isDispatchThread()
&& Runtime.getRuntime().availableProcessors() == 1) {
synchronized (instance) {
while (! instance.paintCalled) {
try { instance.wait(); } catch (InterruptedException e) {}
}
}
}
}
}
/**
* Open's a splash window using the specified image.
* @param imageURL The url of the splash image.
*/
public static void splash(URL imageURL, boolean hidpi) {
if (imageURL != null) {
splash(Toolkit.getDefaultToolkit().createImage(imageURL), hidpi);
}
}
/**
* Closes the splash window.
*/
public static void disposeSplash() {
if (instance != null) {
instance.getOwner().dispose();
instance = null;
}
}
/**
* Invokes the main method of the provided class name.
* @param args the command line arguments
*/
public static void invokeMain(String className, String[] args) {
try {
Class.forName(className)
.getMethod("main", new Class[] {String[].class})
.invoke(null, new Object[] {args});
} catch (Exception e) {
InternalError error = new InternalError("Failed to invoke main method");
error.initCause(e);
throw error;
}
}
}