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