package com.mindprod.example;
import com.mindprod.common18.ImageViewer;
import javax.swing.JWindow;
import javax.swing.Timer;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import static java.lang.System.*;
/**
* Display splash screen shown during program load.
* <p/>
* To use it, just use new Splash() early in program load.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2013-01-18 initial version
* @since 2013-01-18
*/
public class Splash extends JWindow
{
/**
* constructor
*
* @param splashImage name of image in resource, by default filed in jar under com.mindprod.example.
* parameter to getResource.
* @param splashTime how long to display the splash screen in milliseconds.
*/
public Splash( String splashImage, long splashTime )
{
URL url = Splash.class.getResource( splashImage );
Image image = Toolkit.getDefaultToolkit().getImage( url );
ImageViewer imageViewer = new ImageViewer( image );
this.getContentPane().add( imageViewer, BorderLayout.CENTER );
this.addMouseListener( new MouseAdapter()
{
public void mousePressed( MouseEvent e )
{
setVisible( false );
dispose();
}
} );
this.pack();
final Dimension screen_dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation( ( screen_dim.width - this.getWidth() ) / 2, ( screen_dim.height - this.getHeight() ) / 2 );
this.setVisible( true );
final ActionListener splashCloser = new ActionListener()
{
public void actionPerformed( ActionEvent evt )
{
setVisible( false );
dispose();
}
};
Timer splashTimer = new Timer( ( int ) splashTime, splashCloser );
splashTimer.setRepeats( false );
splashTimer.start();
}
/**
* what you do to invoke a splash Screen.
*
* @param args used in the usual way.
*/
public static void main( String[] args )
{
new Splash( "replicator.png", TimeUnit.SECONDS.toMillis( 10 ) );
out.println( "starting..." );
}
}