1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.ui;
20
21 import java.awt.AlphaComposite;
22 import java.awt.Color;
23 import java.awt.Graphics;
24 import java.awt.Graphics2D;
25 import java.awt.Image;
26
27 import javax.swing.JPanel;
28
29 /***
30 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
31 * @since 0.90
32 */
33 public class TimerGlassPane extends JPanel {
34
35 private static final int SIZE_HEIGHT = 105;
36
37 private static final int SIZE_WIDTH = 105;
38
39 /***
40 * Composite for all paints of this component.
41 */
42 private AlphaComposite composite;
43
44 /***
45 * Create a new instance of this class.
46 */
47 public TimerGlassPane() {
48 setOpaque(false);
49 composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
50 }
51
52 /***
53 * Enable/disable timer picture on this zone
54 *
55 * @param timerPicture
56 * the picture to display on front of this zone.
57 * @param gifPicture
58 * list of pictures to use for make an animated picture. May be null.
59 */
60 public void setTimerPiture(Image timerPicture, Image[] gifPicture) {
61 if (timerPicture == null) {
62 setVisible(false);
63 } else {
64 if (this.timerPicture != timerPicture) {
65 this.timerPicture = timerPicture;
66 this.gifPicture = gifPicture;
67 gifCounter = 0;
68 if (isVisible()) {
69 repaint();
70 }
71 } else if (this.gifPicture != gifPicture
72 && (gifPicture == null || this.gifPicture == null)) {
73 this.gifPicture = gifPicture;
74 gifCounter++;
75 if (isVisible()) {
76 repaint();
77 }
78 }
79 }
80 }
81
82 @Override
83 public void paintComponent(Graphics g) {
84 if (timerPicture != null && isVisible()) {
85 final int x = getSize().width / 2 - SIZE_WIDTH / 2;
86 final int y = getSize().height / 2 - SIZE_HEIGHT / 2;
87 final Graphics2D g2d = (Graphics2D) g;
88 g2d.setComposite(composite);
89 g.setColor(Color.BLACK);
90 g.drawRoundRect(x, y, SIZE_WIDTH + 1, SIZE_HEIGHT + 1, 2, 2);
91 g.drawImage(timerPicture, x + 1, y + 1, null);
92 if (gifPicture != null) {
93 g.drawImage(gifPicture[gifCounter % gifPicture.length], x
94 + timerPicture.getWidth(null) - 20, y
95 + timerPicture.getHeight(null) - 20, null);
96 }
97 g.dispose();
98 }
99 }
100
101 /***
102 * The gif counter used to draw some animated pictures.
103 */
104 private int gifCounter;
105
106 /***
107 * List of pictures to use for make an animated picture. May be null.
108 */
109 private Image[] gifPicture;
110
111 /***
112 * The picture currently displayed on front of this zone.
113 */
114 private Image timerPicture = null;
115 }