2 * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
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.
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.
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.
21 import java.awt.image.*;
22 import java.awt.event.*;
24 import javax.imageio.ImageIO;
25 import javax.swing.table.*;
29 import java.util.prefs.*;
30 import java.lang.Math;
31 import java.awt.geom.Point2D;
32 import java.awt.geom.Line2D;
34 public class AltosSiteMapTile extends JLayeredPane {
36 double scale_x, scale_y;
37 Point2D.Double coord_pt;
38 Point2D.Double last_pt;
40 AltosSiteMapLabel mapLabel;
47 static final int px_size = 512;
49 private void loadMap() {
50 Point2D.Double map_latlng = latlng(px_size/2, px_size/2);
51 mapLabel.loadMap(map_latlng.x, map_latlng.y, zoom, px_size);
54 private boolean setLocation(double lat, double lng) {
55 Point2D.Double north_step;
57 for (zoom = 3; zoom < 22; zoom++) {
58 coord_pt = pt(lat, lng, new Point2D.Double(0,0), zoom);
59 north_step = pt(lat+step_nm/60.0, lng,
60 new Point2D.Double(0,0), zoom);
61 if (coord_pt.y - north_step.y > px_size/2)
64 coord_pt.x = -px_size * Math.floor(coord_pt.x/px_size + off_x);
65 coord_pt.y = -px_size * Math.floor(coord_pt.y/px_size + off_y);
67 scale_x = 256/360.0 * Math.pow(2, zoom);
68 scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
75 private static double limit(double v, double lo, double hi) {
84 // http://maps.gstatic.com/intl/en_us/mapfiles/api-3/2/10/main.js
85 // search for fromLatLngToPoint and fromPointToLatLng
86 private Point2D.Double pt(double lat, double lng) {
87 return pt(lat, lng, coord_pt, scale_x, scale_y);
90 private static Point2D.Double pt(double lat, double lng,
91 Point2D.Double centre, int zoom)
93 double scale_x = 256/360.0 * Math.pow(2, zoom);
94 double scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
95 return pt(lat, lng, centre, scale_x, scale_y);
98 private static Point2D.Double pt(double lat, double lng,
99 Point2D.Double centre, double scale_x, double scale_y)
101 Point2D.Double res = new Point2D.Double();
104 res.x = centre.x + lng*scale_x;
105 e = limit(Math.sin(Math.toRadians(lat)),-(1-1.0E-15),1-1.0E-15);
106 res.y = centre.y + 0.5*Math.log((1+e)/(1-e))*-scale_y;
110 private Point2D.Double latlng(double x, double y) {
111 return latlng(new Point2D.Double(x,y), coord_pt);
113 private Point2D.Double latlng(Point2D.Double pt) {
114 return latlng(pt, coord_pt);
116 private Point2D.Double latlng(Point2D.Double pt, Point2D.Double centre) {
120 lng = (pt.x - centre.x)/scale_x;
121 rads = 2 * Math.atan(Math.exp((pt.y-centre.y)/-scale_y));
122 lat = Math.toDegrees(rads - Math.PI/2);
124 return new Point2D.Double(lat,lng);
127 static Color stateColors[] = {
128 Color.WHITE, // startup
133 Color.YELLOW, // coast
134 Color.CYAN, // drogue
136 Color.BLACK // landed
139 boolean drawn_landed_circle = false;
140 boolean drawn_boost_circle = false;
141 public void show(AltosState state, int crc_errors) {
142 if (!state.gps_ready) {
143 if (state.pad_lat == 0 && state.pad_lon == 0)
149 if (last_pt == null) {
150 setLocation(state.pad_lat, state.pad_lon);
152 last_pt = pt(state.pad_lat, state.pad_lon);
155 Point2D.Double pt = pt(state.gps.lat, state.gps.lon);
157 if (0 <= state.state && state.state < stateColors.length) {
158 g2d.setColor(stateColors[state.state]);
160 g2d.draw(new Line2D.Double(last_pt, pt));
163 if (0 <= pt.x && pt.x < px_size) {
164 if (0 <= pt.y && pt.y < px_size) {
165 int dx = 500, dy = 250;
166 if (state.state > 2) {
167 dx = Math.min(200, 20 + (int) Math.abs(last_pt.x - pt.x));
168 dy = Math.min(100, 10 + (int) Math.abs(last_pt.y - pt.y));
170 Rectangle r = new Rectangle((int)pt.x-dx, (int)pt.y-dy,
172 scrollRectToVisible(r);
176 if (state.state == 3 && !drawn_boost_circle) {
177 drawn_boost_circle = true;
178 g2d.setColor(Color.RED);
179 g2d.drawOval((int)last_pt.x-5, (int)last_pt.y-5, 10, 10);
180 g2d.drawOval((int)last_pt.x-20, (int)last_pt.y-20, 40, 40);
181 g2d.drawOval((int)last_pt.x-35, (int)last_pt.y-35, 70, 70);
183 if (state.state == 8 && !drawn_landed_circle) {
184 drawn_landed_circle = true;
185 g2d.setColor(Color.BLACK);
186 g2d.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
187 g2d.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
188 g2d.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
195 public static Graphics2D fillLabel(JLabel l, Color c) {
196 BufferedImage img = new BufferedImage(px_size, px_size,
197 BufferedImage.TYPE_INT_ARGB);
198 Graphics2D g = img.createGraphics();
200 g.fillRect(0, 0, px_size, px_size);
201 l.setIcon(new ImageIcon(img));
205 public AltosSiteMapTile(int x_tile_offset, int y_tile_offset) {
206 setPreferredSize(new Dimension(px_size, px_size));
208 mapLabel = new AltosSiteMapLabel();
209 fillLabel(mapLabel, Color.GRAY);
210 mapLabel.setOpaque(true);
211 mapLabel.setBounds(0, 0, px_size, px_size);
212 add(mapLabel, new Integer(0));
215 g2d = fillLabel(draw, new Color(127, 127, 127, 0));
216 draw.setBounds(0, 0, px_size, px_size);
217 draw.setOpaque(false);
219 add(draw, new Integer(1));
221 off_x = x_tile_offset;
222 off_y = y_tile_offset;