package main;
import java.awt.Dimension;
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;
public class Splash extends Window {
private static Splash instance;
private Image image;
private boolean paintCalled = false;
private Splash(Frame parent, Image image) {
super(parent);
this.image = image;
MediaTracker mt = new MediaTracker(this);
mt.addImage(image,0);
try {
mt.waitForID(0);
} catch(InterruptedException ie){}
int imgWidth = image.getWidth(this);
int imgHeight = image.getHeight(this);
setSize(imgWidth, imgHeight);
Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(
(screenDim.width - imgWidth) / 2,
(screenDim.height - imgHeight) / 2
);
MouseAdapter disposeOnClick = new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
synchronized(Splash.this) {
Splash.this.paintCalled = true;
Splash.this.notifyAll();
}
dispose();
}
};
addMouseListener(disposeOnClick);
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
if (! paintCalled) {
paintCalled = true;
synchronized (this) { notifyAll(); }
}
}
public static void splash(Image image) {
if (instance == null && image != null) {
Frame f = new Frame();
instance = new Splash(f, image);
instance.show();
if (! EventQueue.isDispatchThread()
&& Runtime.getRuntime().availableProcessors() == 1) {
synchronized (instance) {
while (! instance.paintCalled) {
try { instance.wait(); } catch (InterruptedException e) {}
}
}
}
}
}
public static void splash(URL imageURL) {
if (imageURL != null) {
splash(Toolkit.getDefaultToolkit().createImage(imageURL));
}
}
public static void disposeSplash() {
if (instance != null) {
instance.getOwner().dispose();
instance = null;
}
}
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");
error.initCause(e);
throw error;
}
}
}