Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / altosui / AltosSiteMapTile.java
1 /*
2  * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package AltosUI;
19
20 import java.awt.*;
21 import java.awt.image.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import javax.imageio.ImageIO;
25 import javax.swing.table.*;
26 import java.io.*;
27 import java.util.*;
28 import java.text.*;
29 import java.util.prefs.*;
30 import java.lang.Math;
31 import java.awt.geom.Point2D;
32 import java.awt.geom.Line2D;
33 import org.altusmetrum.AltosLib.*;
34
35 public class AltosSiteMapTile extends JLayeredPane {
36         JLabel mapLabel;
37         JLabel draw;
38         Graphics2D g2d;
39         int px_size;
40
41         public void loadMap(ImageIcon icn) {
42                 mapLabel.setIcon(icn);
43         }
44
45         public void clearMap() {
46                 fillLabel(mapLabel, Color.GRAY, px_size);
47                 g2d = fillLabel(draw, new Color(127,127,127,0), px_size);
48                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
49                                      RenderingHints.VALUE_ANTIALIAS_ON);
50                 g2d.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
51         }
52
53         static Color stateColors[] = {
54                 Color.WHITE,  // startup
55                 Color.WHITE,  // idle
56                 Color.WHITE,  // pad
57                 Color.RED,    // boost
58                 Color.PINK,   // fast
59                 Color.YELLOW, // coast
60                 Color.CYAN,   // drogue
61                 Color.BLUE,   // main
62                 Color.BLACK   // landed
63         };
64
65         private boolean drawn_landed_circle = false;
66         private boolean drawn_boost_circle = false;
67         public synchronized void show(AltosState state, int crc_errors,
68                                       Point2D.Double last_pt, Point2D.Double pt)
69         {
70                 if (0 <= state.state && state.state < stateColors.length) {
71                         g2d.setColor(stateColors[state.state]);
72                 }
73                 g2d.draw(new Line2D.Double(last_pt, pt));
74
75                 if (state.state == 3 && !drawn_boost_circle) {
76                         drawn_boost_circle = true;
77                         g2d.setColor(Color.RED);
78                         g2d.drawOval((int)last_pt.x-5, (int)last_pt.y-5, 10, 10);
79                         g2d.drawOval((int)last_pt.x-20, (int)last_pt.y-20, 40, 40);
80                         g2d.drawOval((int)last_pt.x-35, (int)last_pt.y-35, 70, 70);
81                 }
82                 if (state.state == 8 && !drawn_landed_circle) {
83                         drawn_landed_circle = true;
84                         g2d.setColor(Color.BLACK);
85                         g2d.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
86                         g2d.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
87                         g2d.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
88                 }
89
90                 repaint();
91         }
92
93         public void draw_circle(Point2D.Double pt) {
94                 g2d.setColor(Color.RED);
95                 g2d.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
96                 g2d.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
97                 g2d.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
98         }
99
100         public static Graphics2D fillLabel(JLabel l, Color c, int px_size) {
101                 BufferedImage img = new BufferedImage(px_size, px_size,
102                                                       BufferedImage.TYPE_INT_ARGB);
103                 Graphics2D g = img.createGraphics();
104                 g.setColor(c);
105                 g.fillRect(0, 0, px_size, px_size);
106                 l.setIcon(new ImageIcon(img));
107                 return g;
108         }
109
110         public AltosSiteMapTile(int in_px_size) {
111                 px_size = in_px_size;
112                 setPreferredSize(new Dimension(px_size, px_size));
113
114                 mapLabel = new JLabel();
115                 fillLabel(mapLabel, Color.GRAY, px_size);
116                 mapLabel.setOpaque(true);
117                 mapLabel.setBounds(0, 0, px_size, px_size);
118                 add(mapLabel, new Integer(0));
119
120                 draw = new JLabel();
121                 g2d = fillLabel(draw, new Color(127, 127, 127, 0), px_size);
122                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
123                                      RenderingHints.VALUE_ANTIALIAS_ON);
124                 g2d.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
125                 draw.setBounds(0, 0, px_size, px_size);
126                 draw.setOpaque(false);
127
128                 add(draw, new Integer(1));
129         }
130 }