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.
18 package org.altusmetrum.altosuilib_2;
21 import java.awt.image.*;
23 import javax.imageio.*;
24 import java.awt.geom.*;
27 import java.awt.RenderingHints.*;
28 import org.altusmetrum.altoslib_4.*;
34 AltosPoint(Point2D.Double pt, int state) {
40 public class AltosSiteMapTile extends JComponent {
46 Point2D.Double landed;
51 LinkedList<AltosPoint> points;
53 public synchronized void queue_repaint() {
54 if (SwingUtilities.isEventDispatchThread())
57 SwingUtilities.invokeLater(new Runnable() {
65 public void load_map(File pngFile) {
70 private Font font = null;
72 public void set_font(Font font) {
74 this.status = AltosSiteMapCache.success;
77 public void set_status(int status) {
78 if (status != this.status || file != null) {
85 public void clearMap() {
90 status = AltosSiteMapCache.success;
94 static Color stateColors[] = {
95 Color.WHITE, // startup
100 Color.YELLOW, // coast
101 Color.CYAN, // drogue
103 Color.BLACK // landed
106 private void draw_circle(Graphics g, Point2D.Double pt) {
107 g.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
108 g.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
109 g.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
112 public void set_boost(Point2D.Double boost) {
117 public void set_line(Line2D.Double line, double distance) {
119 line_dist = distance;
123 private String line_dist() {
125 double distance = line_dist;
127 if (AltosConvert.imperial_units) {
128 distance = AltosConvert.meters_to_feet(distance);
129 if (distance < 10000) {
135 else if (distance < 100)
137 else if (distance < 1000)
143 if (distance < 10000) {
149 else if (distance < 1000)
155 return String.format(format, distance);
161 public void paint_graphics(Graphics2D g2d, Image image, int serial) {
163 if (serial < painted_serial)
166 painted_serial = serial;
169 AltosSiteMap.debug_component(this, "paint_graphics");
170 g2d.drawImage(image, 0, 0, null);
172 AltosSiteMap.debug_component(this, "erase_graphics");
173 g2d.setColor(Color.GRAY);
174 g2d.fillRect(0, 0, getWidth(), getHeight());
175 String message = null;
177 case AltosSiteMapCache.loading:
178 message = "Loading...";
180 case AltosSiteMapCache.bad_request:
181 message = "Internal error";
183 case AltosSiteMapCache.failed:
184 message = "Network error, check connection";
186 case AltosSiteMapCache.forbidden:
187 message = "Too many requests, try later";
190 if (message != null && font != null) {
192 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
194 bounds = font.getStringBounds(message, g2d.getFontRenderContext());
196 float x = getWidth() / 2.0f;
197 float y = getHeight() / 2.0f;
198 x = x - (float) bounds.getWidth() / 2.0f;
199 y = y + (float) bounds.getHeight() / 2.0f;
200 g2d.setColor(Color.BLACK);
201 g2d.drawString(message, x, y);
205 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
206 RenderingHints.VALUE_ANTIALIAS_ON);
207 g2d.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
209 if (points != null) {
210 AltosPoint prev = null;
211 for (AltosPoint point : points) {
213 if (0 <= point.state && point.state < stateColors.length)
214 g2d.setColor(stateColors[point.state]);
215 g2d.draw(new Line2D.Double(prev.pt, point.pt));
221 g2d.setColor(Color.RED);
222 draw_circle(g2d, boost);
224 if (landed != null) {
225 g2d.setColor(Color.BLACK);
226 draw_circle(g2d, landed);
230 g2d.setColor(Color.BLUE);
233 String message = line_dist();
235 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
237 bounds = font.getStringBounds(message, g2d.getFontRenderContext());
239 float x = (float) line.x1;
240 float y = (float) line.y1 + (float) bounds.getHeight() / 2.0f;
242 if (line.x1 < line.x2) {
243 x -= (float) bounds.getWidth() + 2.0f;
247 g2d.drawString(message, x, y);
251 public void paint(Graphics g) {
252 Graphics2D g2d = (Graphics2D) g;
254 boolean queued = false;
256 AltosSiteMap.debug_component(this, "paint");
261 AltosSiteMapImage aimage;
263 aimage = AltosSiteMapCache.get_image(this, file, px_size, px_size);
264 if (aimage != null) {
265 if (aimage.validate(painting_serial))
266 image = aimage.image;
272 paint_graphics(g2d, image, painting_serial);
275 public void show(int state, Point2D.Double last_pt, Point2D.Double pt)
278 points = new LinkedList<AltosPoint>();
280 points.add(new AltosPoint(pt, state));
282 if (state == AltosLib.ao_flight_boost && boost == null)
284 if (state == AltosLib.ao_flight_landed && landed == null)
289 public AltosSiteMapTile(int in_px_size) {
290 px_size = in_px_size;
291 setPreferredSize(new Dimension(px_size, px_size));