1 18 package chatserver.client; 19 20 import java.awt.Color ; 21 import java.awt.Graphics ; 22 import java.awt.Graphics2D ; 23 import java.awt.RenderingHints ; 24 import java.awt.event.MouseEvent ; 25 import java.awt.event.MouseListener ; 26 import java.awt.font.FontRenderContext ; 27 import java.awt.font.TextLayout ; 28 import java.awt.geom.AffineTransform ; 29 import java.awt.geom.Area ; 30 import java.awt.geom.Ellipse2D ; 31 import java.awt.geom.Point2D ; 32 import java.awt.geom.Rectangle2D ; 33 34 import javax.swing.JComponent ; 35 36 71 public class InfiniteProgressPanel extends JComponent implements MouseListener { 72 73 protected Area [] ticker = null; 74 75 protected Thread animation = null; 76 77 protected boolean started = false; 78 79 protected int alphaLevel = 0; 80 81 protected int rampDelay = 300; 82 83 protected float shield = 0.70f; 84 85 protected String text = ""; 86 87 protected int barsCount = 14; 88 89 protected float fps = 15.0f; 90 91 protected RenderingHints hints = null; 92 93 103 public InfiniteProgressPanel() { 104 this(""); 105 } 106 107 117 public InfiniteProgressPanel(String text) { 118 this(text, 14); 119 } 120 121 131 public InfiniteProgressPanel(String text, int barsCount) { 132 this(text, barsCount, 0.70f); 133 } 134 135 146 public InfiniteProgressPanel(String text, int barsCount, float shield) { 147 this(text, barsCount, shield, 15.0f); 148 } 149 150 162 public InfiniteProgressPanel(String text, int barsCount, float shield, float fps) { 163 this(text, barsCount, shield, fps, 300); 164 } 165 166 177 public InfiniteProgressPanel(String text, int barsCount, float shield, float fps, int rampDelay) { 178 this.text = text; 179 this.rampDelay = rampDelay >= 0 ? rampDelay : 0; 180 this.shield = shield >= 0.0f ? shield : 0.0f; 181 this.fps = fps > 0.0f ? fps : 15.0f; 182 this.barsCount = barsCount > 0 ? barsCount : 14; 183 184 this.hints = new RenderingHints (RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 185 this.hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 186 this.hints.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); 187 } 188 189 194 public void setText(String text) { 195 this.text = text; 196 repaint(); 197 } 198 199 202 public String getText() { 203 return text; 204 } 205 206 211 public void start() { 212 addMouseListener(this); 213 setVisible(true); 214 ticker = buildTicker(); 215 animation = new Thread (new Animator(true)); 216 animation.start(); 217 } 218 219 224 public void stop() { 225 if (animation != null) { 226 animation.interrupt(); 227 animation = null; 228 animation = new Thread (new Animator(false)); 229 animation.start(); 230 } 231 } 232 233 239 public void interrupt() { 240 if (animation != null) { 241 animation.interrupt(); 242 animation = null; 243 244 removeMouseListener(this); 245 setVisible(false); 246 } 247 } 248 249 public void paintComponent(Graphics g) { 250 if (started) { 251 int width = getWidth(); 252 int height = getHeight(); 253 254 double maxY = 0.0; 255 256 Graphics2D g2 = (Graphics2D ) g; 257 g2.setRenderingHints(hints); 258 259 g2.setColor(new Color (255, 255, 255, (int) (alphaLevel * shield))); 260 g2.fillRect(0, 0, getWidth(), getHeight()); 261 262 for (int i = 0; i < ticker.length; i++) { 263 int channel = 224 - 128 / (i + 1); 264 g2.setColor(new Color (channel, channel, channel, alphaLevel)); 265 g2.fill(ticker[i]); 266 267 Rectangle2D bounds = ticker[i].getBounds2D(); 268 if (bounds.getMaxY() > maxY) 269 maxY = bounds.getMaxY(); 270 } 271 272 if (text != null && text.length() > 0) { 273 FontRenderContext context = g2.getFontRenderContext(); 274 TextLayout layout = new TextLayout (text, getFont(), context); 275 Rectangle2D bounds = layout.getBounds(); 276 g2.setColor(getForeground()); 277 layout.draw(g2, (float) (width - bounds.getWidth()) / 2, 278 (float) (maxY + layout.getLeading() + 2 * layout.getAscent())); 279 } 280 } 281 } 282 283 288 private Area [] buildTicker() { 289 Area [] ticker = new Area [barsCount]; 290 Point2D.Double center = new Point2D.Double ((double) getWidth() / 2, (double) getHeight() / 2); 291 double fixedAngle = 2.0 * Math.PI / ((double) barsCount); 292 293 for (double i = 0.0; i < (double) barsCount; i++) { 294 Area primitive = buildPrimitive(); 295 296 AffineTransform toCenter = AffineTransform.getTranslateInstance(center.getX(), center.getY()); 297 AffineTransform toBorder = AffineTransform.getTranslateInstance(45.0, -6.0); 298 AffineTransform toCircle = AffineTransform.getRotateInstance(-i * fixedAngle, center.getX(), center.getY()); 299 300 AffineTransform toWheel = new AffineTransform (); 301 toWheel.concatenate(toCenter); 302 toWheel.concatenate(toBorder); 303 304 primitive.transform(toWheel); 305 primitive.transform(toCircle); 306 307 ticker[(int) i] = primitive; 308 } 309 310 return ticker; 311 } 312 313 316 private Area buildPrimitive() { 317 Rectangle2D.Double body = new Rectangle2D.Double (6, 0, 30, 12); 318 Ellipse2D.Double head = new Ellipse2D.Double (0, 0, 12, 12); 319 Ellipse2D.Double tail = new Ellipse2D.Double (30, 0, 12, 12); 320 321 Area tick = new Area (body); 322 tick.add(new Area (head)); 323 tick.add(new Area (tail)); 324 325 return tick; 326 } 327 328 331 private class Animator implements Runnable { 332 private boolean rampUp = true; 333 334 protected Animator(boolean rampUp) { 335 this.rampUp = rampUp; 336 } 337 338 public void run() { 339 Point2D.Double center = new Point2D.Double ((double) getWidth() / 2, (double) getHeight() / 2); 340 double fixedIncrement = 2.0 * Math.PI / ((double) barsCount); 341 AffineTransform toCircle = AffineTransform.getRotateInstance(fixedIncrement, center.getX(), center.getY()); 342 343 long start = System.currentTimeMillis(); 344 if (rampDelay == 0) 345 alphaLevel = rampUp ? 255 : 0; 346 347 started = true; 348 boolean inRamp = rampUp; 349 350 while (!Thread.interrupted()) { 351 if (!inRamp) { 352 for (int i = 0; i < ticker.length; i++) 353 ticker[i].transform(toCircle); 354 } 355 356 repaint(); 357 358 if (rampUp) { 359 if (alphaLevel < 255) { 360 alphaLevel = (int) (255 * (System.currentTimeMillis() - start) / rampDelay); 361 if (alphaLevel >= 255) { 362 alphaLevel = 255; 363 inRamp = false; 364 } 365 } 366 } else if (alphaLevel > 0) { 367 alphaLevel = (int) (255 - (255 * (System.currentTimeMillis() - start) / rampDelay)); 368 if (alphaLevel <= 0) { 369 alphaLevel = 0; 370 break; 371 } 372 } 373 374 try { 375 Thread.sleep(inRamp ? 10 : (int) (1000 / fps)); 376 } catch (InterruptedException ie) { 377 break; 378 } 379 Thread.yield(); 380 } 381 382 if (!rampUp) { 383 started = false; 384 repaint(); 385 386 setVisible(false); 387 removeMouseListener(InfiniteProgressPanel.this); 388 } 389 } 390 } 391 392 public void mouseClicked(MouseEvent e) { 393 } 394 395 public void mousePressed(MouseEvent e) { 396 } 397 398 public void mouseReleased(MouseEvent e) { 399 } 400 401 public void mouseEntered(MouseEvent e) { 402 } 403 404 public void mouseExited(MouseEvent e) { 405 } 406 } 407 | Popular Tags |