1 21 package proguard.gui.splash; 22 23 import java.awt.*; 24 import java.awt.event.*; 25 import java.lang.reflect.InvocationTargetException ; 26 27 import javax.swing.*; 28 29 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 animationThread; 46 47 48 55 public SplashPanel(Sprite sprite, double processorLoad) 56 { 57 this(sprite, processorLoad, (long)Integer.MAX_VALUE); 58 } 59 60 61 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 addMouseListener(new MouseAdapter() 78 { 79 public void mouseClicked(MouseEvent e) 80 { 81 SplashPanel.this.start(); 82 } 83 }); 84 } 85 86 87 90 public void start() 91 { 92 startTime = System.currentTimeMillis(); 94 95 if (animationThread == null) 97 { 98 animationThread = new Thread (animator); 99 animationThread.start(); 100 } 101 } 102 103 104 107 public void stop() 108 { 109 startTime = 0L; 111 112 animationThread = null; 114 115 try 117 { 118 SwingUtilities.invokeAndWait(repainter); 119 } 120 catch (InterruptedException ex) 121 { 122 } 124 catch (InvocationTargetException ex) 125 { 126 } 128 } 129 130 131 133 public void paintComponent(Graphics graphics) 134 { 135 super.paintComponent(graphics); 136 137 sprite.paint(graphics, System.currentTimeMillis() - startTime); 138 } 139 140 141 145 private class MyAnimator implements Runnable 146 { 147 public void run() 148 { 149 try 150 { 151 while (animationThread != null) 152 { 153 long time = System.currentTimeMillis(); 155 if (time > startTime + stopTime) 156 { 157 animationThread = null; 158 } 159 160 SwingUtilities.invokeAndWait(repainter); 162 163 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 ex) 175 { 176 } 178 catch (InvocationTargetException ex) 179 { 180 } 182 } 183 } 184 185 186 189 private class MyRepainter implements Runnable 190 { 191 public void run() 192 { 193 SplashPanel.this.repaint(); 194 } 195 } 196 197 198 201 public static void main(String [] 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 |