001: /*
002: * SplashPanel.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.BorderLayout;
025: import java.awt.Canvas;
026: import java.awt.Color;
027: import java.awt.Cursor;
028: import java.awt.Dimension;
029: import java.awt.Font;
030: import java.awt.FontMetrics;
031: import java.awt.Frame;
032: import java.awt.GradientPaint;
033: import java.awt.Graphics;
034: import java.awt.Graphics2D;
035: import java.awt.Image;
036: import java.awt.MediaTracker;
037: import java.awt.RenderingHints;
038: import java.awt.Window;
039: import org.underworldlabs.swing.plaf.UIUtils;
040:
041: /* ----------------------------------------------------------
042: * CVS NOTE: Changes to the CVS repository prior to the
043: * release of version 3.0.0beta1 has meant a
044: * resetting of CVS revision numbers.
045: * ----------------------------------------------------------
046: */
047:
048: /**
049: * This class creates a splash panel to the size of the image to be
050: * displayed. The panel is displayed for as long as is required to
051: * load required classes and build the application frame and associated
052: * components.
053: *
054: * @author Takis Diakoumis
055: * @version $Revision: 1.8 $
056: * @date $Date: 2006/07/15 12:51:52 $
057: */
058: public class SplashPanel extends Canvas {
059:
060: /** This object's font metrics */
061: private FontMetrics fontMetrics;
062:
063: /** The window displayed */
064: private Window window;
065:
066: /** The splash image */
067: private Image image;
068:
069: /** The off-screen image */
070: private Image offscreenImg;
071:
072: /** The off-screen graphics */
073: private Graphics offscreenGfx;
074:
075: /** The startup progress posiiton */
076: private int progress;
077:
078: /** The version info string */
079: private String version;
080:
081: /** The progress bar's colour */
082: private Color progressColour;
083:
084: /** the light gradient colour */
085: private Color gradientColour;
086:
087: /** the x-coord of the version string */
088: private int versionLabelX;
089:
090: /** the y-coord of the version string */
091: private int versionLabelY;
092:
093: /** The progress bar height */
094: private int PROGRESS_HEIGHT = 20;
095:
096: /** Creates a new instance of the splash panel. */
097: public SplashPanel(Color progressBarColour,
098: String imageResourcePath, String versionNumber) {
099: this (progressBarColour, imageResourcePath, versionNumber, -1,
100: -1);
101: }
102:
103: public SplashPanel(Color progressBarColour,
104: String imageResourcePath, String versionNumber,
105: int versionLabelX, int versionLabelY) {
106:
107: this .versionLabelX = versionLabelX;
108: this .versionLabelY = versionLabelY;
109:
110: progressColour = progressBarColour;
111: setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
112: setBackground(Color.white);
113:
114: gradientColour = UIUtils.getBrighter(progressBarColour, 0.65);
115:
116: Font font = new Font("Dialog", Font.PLAIN, 15);
117: setFont(font);
118: fontMetrics = getFontMetrics(font);
119:
120: image = getToolkit().getImage(
121: getClass().getResource(imageResourcePath));
122:
123: MediaTracker tracker = new MediaTracker(this );
124: tracker.addImage(image, 0);
125:
126: if (versionNumber != null) {
127: version = "Version " + versionNumber;
128: }
129:
130: try {
131: tracker.waitForAll();
132: } catch (Exception e) {
133: e.printStackTrace();
134: }
135:
136: window = new Window(new Frame());
137:
138: Dimension screen = getToolkit().getScreenSize();
139: Dimension size = new Dimension(image.getWidth(this ), image
140: .getHeight(this ));
141: window.setSize(size);
142:
143: window.setLayout(new BorderLayout());
144: window.add(BorderLayout.CENTER, this );
145:
146: window.setLocation((screen.width - size.width) / 2,
147: (screen.height - size.height) / 2);
148: window.validate();
149: window.setVisible(true);
150:
151: }
152:
153: public synchronized void advance() {
154: progress++;
155: repaint();
156:
157: // wait for it to be painted to ensure
158: // progress is updated continuously
159: try {
160: wait();
161: } catch (InterruptedException ie) {
162: ie.printStackTrace();
163: }
164:
165: }
166:
167: public synchronized void paint(Graphics g) {
168: Dimension size = getSize();
169:
170: if (offscreenImg == null) {
171: offscreenImg = createImage(size.width, size.height);
172: offscreenGfx = offscreenImg.getGraphics();
173: Font font = new Font("dialog", Font.PLAIN, 12);
174: offscreenGfx.setFont(font);
175: }
176:
177: offscreenGfx.drawImage(image, 0, 0, this );
178:
179: offscreenGfx.setColor(progressColour);
180: /*
181: offscreenGfx.fillRect(0,
182: image.getHeight(this) - PROGRESS_HEIGHT,
183: (window.getWidth() * progress) / 9,
184: PROGRESS_HEIGHT);
185: */
186: ((Graphics2D) offscreenGfx).setPaint(new GradientPaint(0, image
187: .getHeight(this )
188: - PROGRESS_HEIGHT, gradientColour,//new Color(95,95,190),
189: 0, image.getHeight(this ), progressColour));
190:
191: offscreenGfx.fillRect(0, image.getHeight(this )
192: - PROGRESS_HEIGHT, (window.getWidth() * progress) / 9,
193: PROGRESS_HEIGHT);
194:
195: if (version != null) {
196:
197: if (versionLabelX == -1) {
198: versionLabelX = (getWidth() - fontMetrics
199: .stringWidth(version)) / 2;
200: }
201:
202: if (versionLabelY == -1) {
203: // if no y value - set just above progress bar
204: versionLabelY = image.getHeight(this ) - PROGRESS_HEIGHT
205: - fontMetrics.getHeight();
206: }
207:
208: ((Graphics2D) offscreenGfx).setRenderingHint(
209: RenderingHints.KEY_ANTIALIASING,
210: RenderingHints.VALUE_ANTIALIAS_ON);
211:
212: offscreenGfx.setColor(Color.WHITE);
213: offscreenGfx.drawString(version, versionLabelX,
214: versionLabelY);
215: }
216:
217: g.drawImage(offscreenImg, 0, 0, this );
218:
219: notify();
220: }
221:
222: public void dispose() {
223: // wait a moment
224: try {
225: Thread.sleep(600);
226: //Thread.sleep(20000);
227: } catch (Exception e) {
228: }
229: window.dispose();
230: }
231:
232: public void update(Graphics g) {
233: paint(g);
234: }
235:
236: }
|