1 /***
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions are
4 * met :
5 *
6 * . Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 *
9 * . Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * . The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $Id: TileComponent.java,v 1.3 2005/09/11 14:55:42 mat007 Exp $
29 */
30
31 package battlehex.ui.main;
32
33 import java.awt.Color;
34 import java.awt.Graphics;
35 import java.awt.Polygon;
36 import java.awt.Rectangle;
37 import javax.swing.JComponent;
38 import battlehex.model.map.IEntity;
39 import battlehex.model.map.ITile;
40
41 /***
42 * Displays a tile.
43 *
44 * @author Mathieu Champlon
45 * @version $Revision: 1.3 $ $Date: 2005/09/11 14:55:42 $
46 */
47 public class TileComponent extends JComponent
48 {
49 private static final long serialVersionUID = -5554715533299365178L;
50 private final ITile tile;
51 private final Polygon polygon;
52
53 /***
54 * Create a tile component.
55 *
56 * @param tile the tile
57 * @param polygon the polygon representing the tile
58 */
59 public TileComponent( final ITile tile, final Polygon polygon )
60 {
61 if( tile == null )
62 throw new IllegalArgumentException( "argument 'tile' is null" );
63 if( polygon == null )
64 throw new IllegalArgumentException( "argument 'polygon' is null" );
65 this.tile = tile;
66 this.polygon = polygon;
67 final Rectangle bounds = polygon.getBounds();
68 bounds.width += 1;
69 bounds.height += 1;
70 setBounds( bounds );
71 polygon.translate( -polygon.getBounds().x, -polygon.getBounds().y );
72 setForeground( Color.BLACK );
73 }
74
75 /***
76 * {@inheritDoc}
77 */
78 protected final void paintComponent( final Graphics gc )
79 {
80 gc.setColor( tile.getColor() );
81 gc.fillPolygon( polygon );
82 gc.setColor( getForeground() );
83 gc.drawPolygon( polygon );
84 final IEntity entity = tile.getEntity();
85 if( entity != null )
86 {
87 gc.setColor( entity.getColor() );
88 gc.fillPolygon( createTriangleInside( polygon ) );
89 }
90 }
91
92 private Polygon createTriangleInside( final Polygon polygon )
93 {
94 final int[] x = new int[3];
95 final int[] y = new int[3];
96 x[0] = (polygon.xpoints[1] + polygon.xpoints[2]) / 2;
97 y[0] = polygon.ypoints[1];
98 x[1] = polygon.xpoints[4];
99 y[1] = polygon.ypoints[4];
100 x[2] = polygon.xpoints[5];
101 y[2] = polygon.ypoints[5];
102 return new Polygon( x, y, 3 );
103 }
104 }