001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/gui/SplashWindow.java,v 1.2 2007/01/29 18:42:03 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is Java 3D(tm) Fly Through.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dfly.utils.gui;
019:
020: import java.awt.Graphics;
021: import java.awt.Dimension;
022: import java.net.URL;
023: import java.awt.Image;
024: import java.awt.Toolkit;
025: import java.awt.Color;
026: import java.awt.MediaTracker;
027: import java.awt.image.BufferedImage;
028:
029: /**
030: * A General purpose Splash Window for display while during
031: * application startup.
032: *
033: * The window contains an image and can display text messages to mark
034: * progress of application startup.
035: *
036: * @author paulby
037: * @version
038: */
039: public class SplashWindow extends javax.swing.JWindow {
040:
041: private static SplashWindow splashWindow = null;
042: private SplashPanel panel = null;
043:
044: /** Creates new SplashWindow
045: * @param imageResourceName is the name of the image file to display on
046: * the splash screen in the form of a Java Resource
047: */
048: public SplashWindow(final String imageResourceName) {
049: initComponents();
050:
051: URL imageURL = this .getClass().getClassLoader().getResource(
052: imageResourceName);
053: Image image = Toolkit.getDefaultToolkit().createImage(imageURL);
054:
055: Dimension screenDim = Toolkit.getDefaultToolkit()
056: .getScreenSize();
057:
058: MediaTracker tracker = new MediaTracker(this );
059: tracker.addImage(image, 1);
060: try {
061: tracker.waitForAll();
062: } catch (InterruptedException e) {
063: }
064:
065: panel = new SplashPanel(image);
066: this .getContentPane().add("Center", panel);
067: pack();
068:
069: Dimension windowDim = getPreferredSize();
070:
071: setLocation(screenDim.width / 2 - windowDim.width / 2,
072: screenDim.height / 2 - windowDim.height / 2);
073: }
074:
075: /** Creates and show a SplashWindow
076: * @param imageResourceName is the name of the image file to display on
077: * the splash screen in the form of a Java Resource
078: */
079: public static void showSplashscreen(final String imageResourceName) {
080: if (splashWindow == null)
081: splashWindow = new SplashWindow(imageResourceName);
082:
083: splashWindow.setVisible(true);
084: }
085:
086: /**
087: * Destroy any locally held resource and hide the window
088: */
089: public static void destroySplashscreen() {
090: splashWindow.setVisible(false);
091:
092: // Dispose call invokeAndWait which deadlocks when
093: // using the editor to look at lg3d. Call dispose
094: // from the swing eventqueue to overcome
095: java.awt.EventQueue.invokeLater(new Runnable() {
096: public void run() {
097: splashWindow.dispose();
098: splashWindow = null;
099: }
100: });
101: }
102:
103: /**
104: * Show this message on the splash screen
105: */
106: public static void showMessage(final String message) {
107: if (splashWindow != null)
108: splashWindow.actualShowMessage(message);
109: }
110:
111: protected void actualShowMessage(final String message) {
112: panel.showMessage(message);
113: }
114:
115: /** This method is called from within the constructor to
116: * initialize the form.
117: * WARNING: Do NOT modify this code. The content of this method is
118: * always regenerated by the FormEditor.
119: */
120: private void initComponents() {//GEN-BEGIN:initComponents
121: addWindowListener(new java.awt.event.WindowAdapter() {
122: public void windowClosing(java.awt.event.WindowEvent evt) {
123: exitForm(evt);
124: }
125: });
126: }//GEN-END:initComponents
127:
128: /** Exit the Application */
129: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
130: System.exit(0);
131: }//GEN-LAST:event_exitForm
132:
133: // Variables declaration - do not modify//GEN-BEGIN:variables
134: // End of variables declaration//GEN-END:variables
135:
136: class SplashPanel extends javax.swing.JPanel {
137: private BufferedImage image;
138: private BufferedImage messageImage = null;
139: private String message = "";
140:
141: private int messageX = 40;
142: private int messageY = 55;
143: private int messageH = 20;
144: private int messageW = 200;
145: private int ascent;
146:
147: public SplashPanel(final Image im) {
148: int width = im.getWidth(null);
149: int height = im.getHeight(null);
150:
151: image = new BufferedImage(width, height,
152: BufferedImage.TYPE_INT_RGB);
153: image.getGraphics().drawImage(im, 0, 0, null);
154:
155: Dimension d = new Dimension(width, height);
156: setPreferredSize(d);
157: setMinimumSize(d);
158:
159: ascent = getFontMetrics(getFont()).getMaxAscent();
160: messageH = getFontMetrics(getFont()).getMaxDescent()
161: + ascent;
162: messageW = width - messageX;
163:
164: messageImage = image.getSubimage(messageX, messageY
165: - ascent, messageW, messageH);
166: }
167:
168: public void showMessage(final String message) {
169: this .message = message;
170: Graphics g = getGraphics();
171: g
172: .drawImage(messageImage, messageX, messageY
173: - ascent, null);
174: g.drawString(message, messageX, messageY);
175: }
176:
177: public void paint(Graphics g) {
178: g.drawImage(image, 0, 0, null);
179:
180: g.drawString(message, messageX, messageY);
181: }
182:
183: }
184: }
|