📄 splashwindow.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package eti.bi.alphaminer.ui;
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.MouseEvent;
import java.awt.event.MouseListener;
/**
* @author mli
*
* Splash Window to show an image during startup of an application
*/
public class SplashWindow extends Window implements MouseListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private Image splashImage;
/**
* 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;
/**
* Constructs a splash window and centers it on the
* screen. The user can click on the window to dispose it.
*
* @param owner The frame owning the splash window.
* @param splashImage The splashImage to be displayed.
*/
public SplashWindow(Frame owner, Image splashImage) {
super(owner);
this.splashImage = splashImage;
owner.setEnabled(true);
// Load the image
MediaTracker mt = new MediaTracker(this);
mt.addImage(splashImage, 0);
try {
mt.waitForID(0);
}
catch (InterruptedException ie) {}
//Add mouseListener
this.addMouseListener(this);
this.setAlwaysOnTop(true);
// Center the window on the screen.
int imgWidth = splashImage.getWidth(this);
int imgHeight = splashImage.getHeight(this);
setSize(imgWidth, imgHeight);
Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(
(screenDim.width - imgWidth) / 2,
(screenDim.height - imgHeight) / 2
);
}
/**
* Paints the image on the window.
*/
public void paint(Graphics g) {
g.drawImage(splashImage, 0, 0, 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();
}
}
}
/**
* Constructs and displays a SplashWindow.<p>
* This method is useful for startup splashs.
* Dispose the return frame to get rid of the splash window.<p>
*
* @param splashImage The image to be displayed.
* @return Returns the frame that owns the SplashWindow.
*/
@SuppressWarnings("deprecation")
public static Frame splash(Image splashImage) {
Frame f = new Frame();
SplashWindow w = new SplashWindow(f, splashImage);
// Show the window.
w.toFront();
w.show();
// 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 by the AWT event dispatcher thread.
if (!EventQueue.isDispatchThread()) {
synchronized (w) {
while (!w.paintCalled) {
try {
w.wait();
}
catch (InterruptedException e) {}
}
}
}
return f;
}
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()>1) {
this.dispose();
System.exit(0);
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -