View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 }