SplashWindow.java :  » 6.0-JDK-Modules » java-3d » com » db » server » Java Open Source

Java Open Source » 6.0 JDK Modules » java 3d 
java 3d » com » db » server » SplashWindow.java
/*
 *  @(#)SplashWindow.java 1.4 01/04/17 16:17:24
 *
 * Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved.
 */

package com.db.server;

import java.awt.Graphics;
import java.awt.Dimension;
import java.net.URL;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Color;
import java.awt.MediaTracker;
import java.awt.image.BufferedImage;

/**
 * A General purpose Splash Window for display while during
 * application startup.
 *
 * The window contains an image and can display text messages to mark
 * progress of application startup.
 *
 * @author  paulby
 * @version
 */
public class SplashWindow extends javax.swing.JWindow {
    
    private static SplashWindow splashWindow = null;
    private SplashPanel panel = null;
    
    /** Creates new SplashWindow
     * @param imageResourceName is the name of the image file to display on
     * the splash screen in the form of a Java Resource
     */
    public SplashWindow( final String imageResourceName ) {
        initComponents();
        
        URL imageURL = this.getClass().getClassLoader().getResource( imageResourceName );
        Image image = Toolkit.getDefaultToolkit().createImage( imageURL );
        
        Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
        
        MediaTracker tracker = new MediaTracker( this );
        tracker.addImage( image, 1 );
        try {
            tracker.waitForAll();
        } catch(InterruptedException e ) {}
        
        panel = new SplashPanel( image );
        this.getContentPane().add( "Center", panel );
        pack();
        
        Dimension windowDim = getPreferredSize();
        
        setLocation( screenDim.width/2 - windowDim.width/2, screenDim.height/2 - windowDim.height/2 );
    }
    
    /** Creates and show a SplashWindow
     * @param imageResourceName is the name of the image file to display on
     * the splash screen in the form of a Java Resource
     */
    public static void showSplashscreen(final String imageResourceName ) {
        if (splashWindow==null)
            splashWindow = new SplashWindow( imageResourceName );
        
        splashWindow.setVisible( true );
    }
    
    /**
     * Destroy any locally held resource and hide the window
     */
    public static void destroySplashscreen() {
        splashWindow.setVisible( false );
        splashWindow.dispose();
        splashWindow = null;
    }
    
    /**
     * Show this message on the splash screen
     */
    public static void showMessage( final String message ) {
        if (splashWindow!=null)
            splashWindow.actualShowMessage( message );
    }
    
    protected void actualShowMessage( final String message ) {
        panel.showMessage( message );
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        }
        );
    }//GEN-END:initComponents
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
    class SplashPanel extends javax.swing.JPanel {
        private BufferedImage image;
        private BufferedImage messageImage = null;
        private String message="";
        
        private int messageX = 40;
        private int messageY = 55;
        private int messageH = 20;
        private int messageW = 200;
        private int ascent;
        
        public SplashPanel( final Image im ) {
            int width = im.getWidth( null );
            int height = im.getHeight( null);
            
            image = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
            image.getGraphics().drawImage( im, 0, 0, null );
            
            
            Dimension d = new Dimension( width, height );
            setPreferredSize( d );
            setMinimumSize( d );
            
            ascent = getFontMetrics(getFont()).getMaxAscent();
            messageH = getFontMetrics(getFont()).getMaxDescent()+ascent;
            messageW = width-messageX;
            
            messageImage = image.getSubimage( messageX, messageY-ascent, messageW, messageH );
        }
        
        public void showMessage( final String message ) {
            this.message = message;
            Graphics g = getGraphics();
            g.drawImage( messageImage, messageX, messageY-ascent, null );
            g.drawString( message, messageX, messageY );
        }
        
        public void paint( Graphics g ) {
            g.drawImage( image, 0, 0, null );
            
            g.drawString( message, messageX, messageY );
        }
        
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.