001: /*
002: * @(#)SplashWindow.java 1.4 01/04/17 16:17:24
003: *
004: * Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved.
005: */
006:
007: package com.db.client;
008:
009: import java.awt.Graphics;
010: import java.awt.Dimension;
011: import java.net.URL;
012: import java.awt.Image;
013: import java.awt.Toolkit;
014: import java.awt.Color;
015: import java.awt.MediaTracker;
016: import java.awt.image.BufferedImage;
017:
018: /**
019: * A General purpose Splash Window for display while during
020: * application startup.
021: *
022: * The window contains an image and can display text messages to mark
023: * progress of application startup.
024: *
025: * @author paulby
026: * @version
027: */
028: public class SplashWindow extends javax.swing.JWindow {
029:
030: private static SplashWindow splashWindow = null;
031: private SplashPanel panel = null;
032:
033: /** Creates new SplashWindow
034: * @param imageResourceName is the name of the image file to display on
035: * the splash screen in the form of a Java Resource
036: */
037: public SplashWindow(final String imageResourceName) {
038: initComponents();
039:
040: URL imageURL = this .getClass().getClassLoader().getResource(
041: imageResourceName);
042: Image image = Toolkit.getDefaultToolkit().createImage(imageURL);
043:
044: Dimension screenDim = Toolkit.getDefaultToolkit()
045: .getScreenSize();
046:
047: MediaTracker tracker = new MediaTracker(this );
048: tracker.addImage(image, 1);
049: try {
050: tracker.waitForAll();
051: } catch (InterruptedException e) {
052: }
053:
054: panel = new SplashPanel(image);
055: this .getContentPane().add("Center", panel);
056: pack();
057:
058: Dimension windowDim = getPreferredSize();
059:
060: setLocation(screenDim.width / 2 - windowDim.width / 2,
061: screenDim.height / 2 - windowDim.height / 2);
062: }
063:
064: /** Creates and show a SplashWindow
065: * @param imageResourceName is the name of the image file to display on
066: * the splash screen in the form of a Java Resource
067: */
068: public static void showSplashscreen(final String imageResourceName) {
069: if (splashWindow == null)
070: splashWindow = new SplashWindow(imageResourceName);
071:
072: splashWindow.setVisible(true);
073: }
074:
075: /**
076: * Destroy any locally held resource and hide the window
077: */
078: public static void destroySplashscreen() {
079: splashWindow.setVisible(false);
080: splashWindow.dispose();
081: splashWindow = null;
082: }
083:
084: /**
085: * Show this message on the splash screen
086: */
087: public static void showMessage(final String message) {
088: if (splashWindow != null)
089: splashWindow.actualShowMessage(message);
090: }
091:
092: protected void actualShowMessage(final String message) {
093: panel.showMessage(message);
094: }
095:
096: /** This method is called from within the constructor to
097: * initialize the form.
098: * WARNING: Do NOT modify this code. The content of this method is
099: * always regenerated by the FormEditor.
100: */
101: private void initComponents() {//GEN-BEGIN:initComponents
102: addWindowListener(new java.awt.event.WindowAdapter() {
103: public void windowClosing(java.awt.event.WindowEvent evt) {
104: exitForm(evt);
105: }
106: });
107: }//GEN-END:initComponents
108:
109: /** Exit the Application */
110: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
111: System.exit(0);
112: }//GEN-LAST:event_exitForm
113:
114: // Variables declaration - do not modify//GEN-BEGIN:variables
115: // End of variables declaration//GEN-END:variables
116:
117: class SplashPanel extends javax.swing.JPanel {
118: private BufferedImage image;
119: private BufferedImage messageImage = null;
120: private String message = "";
121:
122: private int messageX = 40;
123: private int messageY = 55;
124: private int messageH = 20;
125: private int messageW = 200;
126: private int ascent;
127:
128: public SplashPanel(final Image im) {
129: int width = im.getWidth(null);
130: int height = im.getHeight(null);
131:
132: image = new BufferedImage(width, height,
133: BufferedImage.TYPE_INT_RGB);
134: image.getGraphics().drawImage(im, 0, 0, null);
135:
136: Dimension d = new Dimension(width, height);
137: setPreferredSize(d);
138: setMinimumSize(d);
139:
140: ascent = getFontMetrics(getFont()).getMaxAscent();
141: messageH = getFontMetrics(getFont()).getMaxDescent()
142: + ascent;
143: messageW = width - messageX;
144:
145: messageImage = image.getSubimage(messageX, messageY
146: - ascent, messageW, messageH);
147: }
148:
149: public void showMessage(final String message) {
150: this .message = message;
151: Graphics g = getGraphics();
152: g
153: .drawImage(messageImage, messageX, messageY
154: - ascent, null);
155: g.drawString(message, messageX, messageY);
156: }
157:
158: public void paint(Graphics g) {
159: g.drawImage(image, 0, 0, null);
160:
161: g.drawString(message, messageX, messageY);
162: }
163:
164: }
165: }
|