KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > gui > splash > SplashPanel


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.gui.splash;
22
23 import java.awt.*;
24 import java.awt.event.*;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26
27 import javax.swing.*;
28
29 /**
30  * This JPanel renders an animated Sprite.
31  *
32  * @author Eric Lafortune
33  */

34 public class SplashPanel extends JPanel
35 {
36     private final MyAnimator animator = new MyAnimator();
37     private final MyRepainter repainter = new MyRepainter();
38
39     private Sprite sprite;
40     private double sleepFactor;
41
42     private long startTime = Long.MAX_VALUE;
43     private long stopTime;
44
45     private volatile Thread JavaDoc animationThread;
46
47
48     /**
49      * Creates a new SplashPanel with the given Sprite, which will be animated
50      * indefinitely.
51      * @param sprite the Sprite that will be animated.
52      * @param processorLoad the fraction of processing time to be spend on
53      * animating the Sprite (between 0 and 1).
54      */

55     public SplashPanel(Sprite sprite, double processorLoad)
56     {
57         this(sprite, processorLoad, (long)Integer.MAX_VALUE);
58     }
59
60
61     /**
62      * Creates a new SplashPanel with the given Sprite, which will be animated
63      * for a limited period of time.
64      * @param sprite the Sprite that will be animated.
65      * @param processorLoad the fraction of processing time to be spend on
66      * animating the Sprite (between 0 and 1).
67      * @param stopTime the number of milliseconds after which the
68      * animation will be stopped automatically.
69      */

70     public SplashPanel(Sprite sprite, double processorLoad, long stopTime)
71     {
72         this.sprite = sprite;
73         this.sleepFactor = (1.0-processorLoad) / processorLoad;
74         this.stopTime = stopTime;
75
76         // Restart the animation on a mouse click.
77
addMouseListener(new MouseAdapter()
78         {
79             public void mouseClicked(MouseEvent e)
80             {
81                 SplashPanel.this.start();
82             }
83         });
84     }
85
86
87     /**
88      * Starts the animation.
89      */

90     public void start()
91     {
92         // Go to the beginning of the animation.
93
startTime = System.currentTimeMillis();
94
95         // Make sure we have an animation thread running.
96
if (animationThread == null)
97         {
98             animationThread = new Thread JavaDoc(animator);
99             animationThread.start();
100         }
101     }
102
103
104     /**
105      * Stops the animation.
106      */

107     public void stop()
108     {
109         // Go to the end of the animation.
110
startTime = 0L;
111
112         // Let the animation thread stop itself.
113
animationThread = null;
114
115         // Repaint the SplashPanel one last time.
116
try
117         {
118             SwingUtilities.invokeAndWait(repainter);
119         }
120         catch (InterruptedException JavaDoc ex)
121         {
122             // Nothing.
123
}
124         catch (InvocationTargetException JavaDoc ex)
125         {
126             // Nothing.
127
}
128     }
129
130
131     // Implementation for JPanel.
132

133     public void paintComponent(Graphics graphics)
134     {
135         super.paintComponent(graphics);
136
137         sprite.paint(graphics, System.currentTimeMillis() - startTime);
138     }
139
140
141     /**
142      * This Runnable makes sure its SplashPanel gets repainted regularly,
143      * depending on the targeted processor load.
144      */

145     private class MyAnimator implements Runnable JavaDoc
146     {
147         public void run()
148         {
149             try
150             {
151                 while (animationThread != null)
152                 {
153                     // Check if we should stop the animation.
154
long time = System.currentTimeMillis();
155                     if (time > startTime + stopTime)
156                     {
157                         animationThread = null;
158                     }
159
160                     // Do a repaint and time it.
161
SwingUtilities.invokeAndWait(repainter);
162
163                     // Sleep for a proportional while.
164
long repaintTime = System.currentTimeMillis() - time;
165                     long sleepTime = (long)(sleepFactor * repaintTime);
166                     if (sleepTime < 10L)
167                     {
168                         sleepTime = 10L;
169                     }
170
171                     Thread.sleep(sleepTime);
172                 }
173             }
174             catch (InterruptedException JavaDoc ex)
175             {
176                 // Nothing.
177
}
178             catch (InvocationTargetException JavaDoc ex)
179             {
180                 // Nothing.
181
}
182         }
183     }
184
185
186     /**
187      * This Runnable repaints its SplashPanel.
188      */

189     private class MyRepainter implements Runnable JavaDoc
190     {
191         public void run()
192         {
193             SplashPanel.this.repaint();
194         }
195     }
196
197
198     /**
199      * A main method for testing the splash panel.
200      */

201     public static void main(String JavaDoc[] args)
202     {
203         JFrame frame = new JFrame();
204         frame.setTitle("Animation");
205         frame.setSize(800, 600);
206         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
207         Dimension frameSize = frame.getSize();
208         frame.setLocation((screenSize.width - frameSize.width) / 2,
209                           (screenSize.height - frameSize.height) / 2);
210
211         Sprite sprite =
212             new ClipSprite(
213             new ConstantColor(Color.white),
214             new ConstantColor(Color.lightGray),
215             new CircleSprite(true,
216                              new LinearInt(200, 600, new SineTiming(2345L, 0L)),
217                              new LinearInt(200, 400, new SineTiming(3210L, 0L)),
218                              new ConstantInt(150)),
219             new ColorSprite(new ConstantColor(Color.gray),
220             new FontSprite(new ConstantFont(new Font("sansserif", Font.BOLD, 90)),
221             new TextSprite(new ConstantString("ProGuard"),
222                            new ConstantInt(200),
223                            new ConstantInt(300)))));
224
225         SplashPanel panel = new SplashPanel(sprite, 0.5);
226         panel.setBackground(Color.white);
227
228         frame.getContentPane().add(panel);
229         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
230         frame.setVisible(true);
231
232         panel.start();
233     }
234 }
235
Popular Tags