altosdroid: initial implementation of telemetry logging.
[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 javax.swing.*;
23 import java.awt.geom.Point2D;
24 import java.awt.geom.Line2D;
25 import org.altusmetrum.AltosLib.*;
26
27 public class AltosSiteMapTile extends JLayeredPane {
28         JLabel mapLabel;
29         JLabel draw;
30         Graphics2D g2d;
31         int px_size;
32
33         public void loadMap(ImageIcon icn) {
34                 mapLabel.setIcon(icn);
35         }
36
37         public void clearMap() {
38                 fillLabel(mapLabel, Color.GRAY, px_size);
39                 g2d = fillLabel(draw, new Color(127,127,127,0), px_size);
40                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
41                                      RenderingHints.VALUE_ANTIALIAS_ON);
42                 g2d.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
43         }
44
45         static Color stateColors[] = {
46                 Color.WHITE,  // startup
47                 Color.WHITE,  // idle
48                 Color.WHITE,  // pad
49                 Color.RED,    // boost
50                 Color.PINK,   // fast
51                 Color.YELLOW, // coast
52                 Color.CYAN,   // drogue
53                 Color.BLUE,   // main
54                 Color.BLACK   // landed
55         };
56
57         private boolean drawn_landed_circle = false;
58         private boolean drawn_boost_circle = false;
59         public synchronized void show(AltosState state, int crc_errors,
60                                       Point2D.Double last_pt, Point2D.Double pt)
61         {
62                 if (0 <= state.state && state.state < stateColors.length) {
63                         g2d.setColor(stateColors[state.state]);
64                 }
65                 g2d.draw(new Line2D.Double(last_pt, pt));
66
67                 if (state.state == 3 && !drawn_boost_circle) {
68                         drawn_boost_circle = true;
69                         g2d.setColor(Color.RED);
70                         g2d.drawOval((int)last_pt.x-5, (int)last_pt.y-5, 10, 10);
71                         g2d.drawOval((int)last_pt.x-20, (int)last_pt.y-20, 40, 40);
72                         g2d.drawOval((int)last_pt.x-35, (int)last_pt.y-35, 70, 70);
73                 }
74                 if (state.state == 8 && !drawn_landed_circle) {
75                         drawn_landed_circle = true;
76                         g2d.setColor(Color.BLACK);
77                         g2d.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
78                         g2d.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
79                         g2d.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
80                 }
81
82                 repaint();
83         }
84
85         public void draw_circle(Point2D.Double pt) {
86                 g2d.setColor(Color.RED);
87                 g2d.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
88                 g2d.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
89                 g2d.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
90         }
91
92         public static Graphics2D fillLabel(JLabel l, Color c, int px_size) {
93                 BufferedImage img = new BufferedImage(px_size, px_size,
94                                                       BufferedImage.TYPE_INT_ARGB);
95                 Graphics2D g = img.createGraphics();
96                 g.setColor(c);
97                 g.fillRect(0, 0, px_size, px_size);
98                 l.setIcon(new ImageIcon(img));
99                 return g;
100         }
101
102         public AltosSiteMapTile(int in_px_size) {
103                 px_size = in_px_size;
104                 setPreferredSize(new Dimension(px_size, px_size));
105
106                 mapLabel = new JLabel();
107                 fillLabel(mapLabel, Color.GRAY, px_size);
108                 mapLabel.setOpaque(true);
109                 mapLabel.setBounds(0, 0, px_size, px_size);
110                 add(mapLabel, new Integer(0));
111
112                 draw = new JLabel();
113                 g2d = fillLabel(draw, new Color(127, 127, 127, 0), px_size);
114                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
115                                      RenderingHints.VALUE_ANTIALIAS_ON);
116                 g2d.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
117                 draw.setBounds(0, 0, px_size, px_size);
118                 draw.setOpaque(false);
119
120                 add(draw, new Integer(1));
121         }
122 }