001: /*
002: * Copyright (c) 2005, Romain Guy <romain.guy@jext.org>
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
006: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
007: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
008: * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
009: *
010: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
011: *
012: * Code from Romain Guy's webblog - http://www.jroller.com/page/gfx
013: * Subject to the BSD license.
014: *
015: * For questions, suggestions, bug-reports, enhancement-requests etc.
016: * visit http://www.quickserver.org
017: */
018: package chatserver.client;
019:
020: import java.awt.Color;
021: import java.awt.Graphics;
022: import java.awt.Graphics2D;
023: import java.awt.RenderingHints;
024: import java.awt.event.MouseEvent;
025: import java.awt.event.MouseListener;
026: import java.awt.font.FontRenderContext;
027: import java.awt.font.TextLayout;
028: import java.awt.geom.AffineTransform;
029: import java.awt.geom.Area;
030: import java.awt.geom.Ellipse2D;
031: import java.awt.geom.Point2D;
032: import java.awt.geom.Rectangle2D;
033:
034: import javax.swing.JComponent;
035:
036: /**
037: *
038: * An infinite progress panel displays a rotating figure and
039: * a message to notice the user of a long, duration unknown
040: * task. The shape and the text are drawn upon a white veil
041: * which alpha level (or shield value) lets the underlying
042: * component shine through. This panel is meant to be used
043: * asa <i>glass pane</i> in the window performing the long
044: * operation.
045: * <br /><br />
046: * On the contrary to regular glass panes, you don't need to
047: * set it visible or not by yourself. Once you've started the
048: * animation all the mouse events are intercepted by this
049: * panel, preventing them from being forwared to the
050: * underlying components.
051: * <br /><br />
052: * The panel can be controlled by the <code>start()</code>,
053: * <code>stop()</code> and <code>interrupt()</code> methods.
054: * <br /><br />
055: * Example:
056: * <br /><br />
057: * <pre>InfiniteProgressPanel pane = new InfiniteProgressPanel();
058: * frame.setGlassPane(pane);
059: * pane.start()</pre>
060: * <br /><br />
061: * Several properties can be configured at creation time. The
062: * message and its font can be changed at runtime. Changing the
063: * font can be done using <code>setFont()</code> and
064: * <code>setForeground()</code>.
065: *
066: * Code from Romain Guy's webblog - http://www.jroller.com/page/gfx (BSD license.)
067: *
068: * @author Romain Guy
069: * @version 1.0
070: */
071: public class InfiniteProgressPanel extends JComponent implements
072: MouseListener {
073: /** Contains the bars composing the circular shape. */
074: protected Area[] ticker = null;
075: /** The animation thread is responsible for fade in/out and rotation. */
076: protected Thread animation = null;
077: /** Notifies whether the animation is running or not. */
078: protected boolean started = false;
079: /** Alpha level of the veil, used for fade in/out. */
080: protected int alphaLevel = 0;
081: /** Duration of the veil's fade in/out. */
082: protected int rampDelay = 300;
083: /** Alpha level of the veil. */
084: protected float shield = 0.70f;
085: /** Message displayed below the circular shape. */
086: protected String text = "";
087: /** Amount of bars composing the circular shape. */
088: protected int barsCount = 14;
089: /** Amount of frames per seconde. Lowers this to save CPU. */
090: protected float fps = 15.0f;
091: /** Rendering hints to set anti aliasing. */
092: protected RenderingHints hints = null;
093:
094: /**
095: * Creates a new progress panel with default values:<br />
096: * <ul>
097: * <li>No message</li>
098: * <li>14 bars</li>
099: * <li>Veil's alpha level is 70%</li>
100: * <li>15 frames per second</li>
101: * <li>Fade in/out last 300 ms</li>
102: * </ul>
103: */
104: public InfiniteProgressPanel() {
105: this ("");
106: }
107:
108: /**
109: * Creates a new progress panel with default values:<br />
110: * <ul>
111: * <li>14 bars</li>
112: * <li>Veil's alpha level is 70%</li>
113: * <li>15 frames per second</li>
114: * <li>Fade in/out last 300 ms</li>
115: * </ul>
116: * @param text The message to be displayed. Can be null or empty.
117: */
118: public InfiniteProgressPanel(String text) {
119: this (text, 14);
120: }
121:
122: /**
123: * Creates a new progress panel with default values:<br />
124: * <ul>
125: * <li>Veil's alpha level is 70%</li>
126: * <li>15 frames per second</li>
127: * <li>Fade in/out last 300 ms</li>
128: * </ul>
129: * @param text The message to be displayed. Can be null or empty.
130: * @param barsCount The amount of bars composing the circular shape
131: */
132: public InfiniteProgressPanel(String text, int barsCount) {
133: this (text, barsCount, 0.70f);
134: }
135:
136: /**
137: * Creates a new progress panel with default values:<br />
138: * <ul>
139: * <li>15 frames per second</li>
140: * <li>Fade in/out last 300 ms</li>
141: * </ul>
142: * @param text The message to be displayed. Can be null or empty.
143: * @param barsCount The amount of bars composing the circular shape.
144: * @param shield The alpha level between 0.0 and 1.0 of the colored
145: * shield (or veil).
146: */
147: public InfiniteProgressPanel(String text, int barsCount,
148: float shield) {
149: this (text, barsCount, shield, 15.0f);
150: }
151:
152: /**
153: * Creates a new progress panel with default values:<br />
154: * <ul>
155: * <li>Fade in/out last 300 ms</li>
156: * </ul>
157: * @param text The message to be displayed. Can be null or empty.
158: * @param barsCount The amount of bars composing the circular shape.
159: * @param shield The alpha level between 0.0 and 1.0 of the colored
160: * shield (or veil).
161: * @param fps The number of frames per second. Lower this value to
162: * decrease CPU usage.
163: */
164: public InfiniteProgressPanel(String text, int barsCount,
165: float shield, float fps) {
166: this (text, barsCount, shield, fps, 300);
167: }
168:
169: /**
170: * Creates a new progress panel.
171: * @param text The message to be displayed. Can be null or empty.
172: * @param barsCount The amount of bars composing the circular shape.
173: * @param shield The alpha level between 0.0 and 1.0 of the colored
174: * shield (or veil).
175: * @param fps The number of frames per second. Lower this value to
176: * decrease CPU usage.
177: * @param rampDelay The duration, in milli seconds, of the fade in and
178: * the fade out of the veil.
179: */
180: public InfiniteProgressPanel(String text, int barsCount,
181: float shield, float fps, int rampDelay) {
182: this .text = text;
183: this .rampDelay = rampDelay >= 0 ? rampDelay : 0;
184: this .shield = shield >= 0.0f ? shield : 0.0f;
185: this .fps = fps > 0.0f ? fps : 15.0f;
186: this .barsCount = barsCount > 0 ? barsCount : 14;
187:
188: this .hints = new RenderingHints(RenderingHints.KEY_RENDERING,
189: RenderingHints.VALUE_RENDER_QUALITY);
190: this .hints.put(RenderingHints.KEY_ANTIALIASING,
191: RenderingHints.VALUE_ANTIALIAS_ON);
192: this .hints.put(RenderingHints.KEY_FRACTIONALMETRICS,
193: RenderingHints.VALUE_FRACTIONALMETRICS_ON);
194: }
195:
196: /**
197: * Changes the displayed message at runtime.
198: *
199: * @param text The message to be displayed. Can be null or empty.
200: */
201: public void setText(String text) {
202: this .text = text;
203: repaint();
204: }
205:
206: /**
207: * Returns the current displayed message.
208: */
209: public String getText() {
210: return text;
211: }
212:
213: /**
214: * Starts the waiting animation by fading the veil in, then
215: * rotating the shapes. This method handles the visibility
216: * of the glass pane.
217: */
218: public void start() {
219: addMouseListener(this );
220: setVisible(true);
221: ticker = buildTicker();
222: animation = new Thread(new Animator(true));
223: animation.start();
224: }
225:
226: /**
227: * Stops the waiting animation by stopping the rotation
228: * of the circular shape and then by fading out the veil.
229: * This methods sets the panel invisible at the end.
230: */
231: public void stop() {
232: if (animation != null) {
233: animation.interrupt();
234: animation = null;
235: animation = new Thread(new Animator(false));
236: animation.start();
237: }
238: }
239:
240: /**
241: * Interrupts the animation, whatever its state is. You
242: * can use it when you need to stop the animation without
243: * running the fade out phase.
244: * This methods sets the panel invisible at the end.
245: */
246: public void interrupt() {
247: if (animation != null) {
248: animation.interrupt();
249: animation = null;
250:
251: removeMouseListener(this );
252: setVisible(false);
253: }
254: }
255:
256: public void paintComponent(Graphics g) {
257: if (started) {
258: int width = getWidth();
259: int height = getHeight();
260:
261: double maxY = 0.0;
262:
263: Graphics2D g2 = (Graphics2D) g;
264: g2.setRenderingHints(hints);
265:
266: g2.setColor(new Color(255, 255, 255,
267: (int) (alphaLevel * shield)));
268: g2.fillRect(0, 0, getWidth(), getHeight());
269:
270: for (int i = 0; i < ticker.length; i++) {
271: int channel = 224 - 128 / (i + 1);
272: g2.setColor(new Color(channel, channel, channel,
273: alphaLevel));
274: g2.fill(ticker[i]);
275:
276: Rectangle2D bounds = ticker[i].getBounds2D();
277: if (bounds.getMaxY() > maxY)
278: maxY = bounds.getMaxY();
279: }
280:
281: if (text != null && text.length() > 0) {
282: FontRenderContext context = g2.getFontRenderContext();
283: TextLayout layout = new TextLayout(text, getFont(),
284: context);
285: Rectangle2D bounds = layout.getBounds();
286: g2.setColor(getForeground());
287: layout
288: .draw(
289: g2,
290: (float) (width - bounds.getWidth()) / 2,
291: (float) (maxY + layout.getLeading() + 2 * layout
292: .getAscent()));
293: }
294: }
295: }
296:
297: /**
298: * Builds the circular shape and returns the result as an array of
299: * <code>Area</code>. Each <code>Area</code> is one of the bars
300: * composing the shape.
301: */
302: private Area[] buildTicker() {
303: Area[] ticker = new Area[barsCount];
304: Point2D.Double center = new Point2D.Double(
305: (double) getWidth() / 2, (double) getHeight() / 2);
306: double fixedAngle = 2.0 * Math.PI / ((double) barsCount);
307:
308: for (double i = 0.0; i < (double) barsCount; i++) {
309: Area primitive = buildPrimitive();
310:
311: AffineTransform toCenter = AffineTransform
312: .getTranslateInstance(center.getX(), center.getY());
313: AffineTransform toBorder = AffineTransform
314: .getTranslateInstance(45.0, -6.0);
315: AffineTransform toCircle = AffineTransform
316: .getRotateInstance(-i * fixedAngle, center.getX(),
317: center.getY());
318:
319: AffineTransform toWheel = new AffineTransform();
320: toWheel.concatenate(toCenter);
321: toWheel.concatenate(toBorder);
322:
323: primitive.transform(toWheel);
324: primitive.transform(toCircle);
325:
326: ticker[(int) i] = primitive;
327: }
328:
329: return ticker;
330: }
331:
332: /**
333: * Builds a bar.
334: */
335: private Area buildPrimitive() {
336: Rectangle2D.Double body = new Rectangle2D.Double(6, 0, 30, 12);
337: Ellipse2D.Double head = new Ellipse2D.Double(0, 0, 12, 12);
338: Ellipse2D.Double tail = new Ellipse2D.Double(30, 0, 12, 12);
339:
340: Area tick = new Area(body);
341: tick.add(new Area(head));
342: tick.add(new Area(tail));
343:
344: return tick;
345: }
346:
347: /**
348: * Animation thread.
349: */
350: private class Animator implements Runnable {
351: private boolean rampUp = true;
352:
353: protected Animator(boolean rampUp) {
354: this .rampUp = rampUp;
355: }
356:
357: public void run() {
358: Point2D.Double center = new Point2D.Double(
359: (double) getWidth() / 2, (double) getHeight() / 2);
360: double fixedIncrement = 2.0 * Math.PI
361: / ((double) barsCount);
362: AffineTransform toCircle = AffineTransform
363: .getRotateInstance(fixedIncrement, center.getX(),
364: center.getY());
365:
366: long start = System.currentTimeMillis();
367: if (rampDelay == 0)
368: alphaLevel = rampUp ? 255 : 0;
369:
370: started = true;
371: boolean inRamp = rampUp;
372:
373: while (!Thread.interrupted()) {
374: if (!inRamp) {
375: for (int i = 0; i < ticker.length; i++)
376: ticker[i].transform(toCircle);
377: }
378:
379: repaint();
380:
381: if (rampUp) {
382: if (alphaLevel < 255) {
383: alphaLevel = (int) (255 * (System
384: .currentTimeMillis() - start) / rampDelay);
385: if (alphaLevel >= 255) {
386: alphaLevel = 255;
387: inRamp = false;
388: }
389: }
390: } else if (alphaLevel > 0) {
391: alphaLevel = (int) (255 - (255 * (System
392: .currentTimeMillis() - start) / rampDelay));
393: if (alphaLevel <= 0) {
394: alphaLevel = 0;
395: break;
396: }
397: }
398:
399: try {
400: Thread.sleep(inRamp ? 10 : (int) (1000 / fps));
401: } catch (InterruptedException ie) {
402: break;
403: }
404: Thread.yield();
405: }
406:
407: if (!rampUp) {
408: started = false;
409: repaint();
410:
411: setVisible(false);
412: removeMouseListener(InfiniteProgressPanel.this );
413: }
414: }
415: }
416:
417: public void mouseClicked(MouseEvent e) {
418: }
419:
420: public void mousePressed(MouseEvent e) {
421: }
422:
423: public void mouseReleased(MouseEvent e) {
424: }
425:
426: public void mouseEntered(MouseEvent e) {
427: }
428:
429: public void mouseExited(MouseEvent e) {
430: }
431: }
|