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);
160 public void paint_graphics(Graphics2D g2d, Image image) {
162 AltosSiteMap.debug_component(this, "paint_graphics");
163 g2d.drawImage(image, 0, 0, null);
165 AltosSiteMap.debug_component(this, "erase_graphics");
166 g2d.setColor(Color.GRAY);
167 g2d.fillRect(0, 0, getWidth(), getHeight());
168 String message = null;
170 case AltosSiteMapCache.loading:
171 message = "Loading...";
173 case AltosSiteMapCache.bad_request:
174 message = "Internal error";
176 case AltosSiteMapCache.failed:
177 message = "Network error, check connection";
179 case AltosSiteMapCache.forbidden:
180 message = "Too many requests, try later";
183 if (message != null && font != null) {
185 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
187 bounds = font.getStringBounds(message, g2d.getFontRenderContext());
189 float x = getWidth() / 2.0f;
190 float y = getHeight() / 2.0f;
191 x = x - (float) bounds.getWidth() / 2.0f;
192 y = y + (float) bounds.getHeight() / 2.0f;
193 g2d.setColor(Color.BLACK);
194 g2d.drawString(message, x, y);
198 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
199 RenderingHints.VALUE_ANTIALIAS_ON);
200 g2d.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
202 if (points != null) {
203 AltosPoint prev = null;
204 for (AltosPoint point : points) {
206 if (0 <= point.state && point.state < stateColors.length)
207 g2d.setColor(stateColors[point.state]);
208 g2d.draw(new Line2D.Double(prev.pt, point.pt));
214 g2d.setColor(Color.RED);
215 draw_circle(g2d, boost);
217 if (landed != null) {
218 g2d.setColor(Color.BLACK);
219 draw_circle(g2d, landed);
223 g2d.setColor(Color.BLUE);
226 String message = line_dist();
228 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
230 bounds = font.getStringBounds(message, g2d.getFontRenderContext());
232 float x = (float) line.x1;
233 float y = (float) line.y1 + (float) bounds.getHeight() / 2.0f;
235 if (line.x1 < line.x2) {
236 x -= (float) bounds.getWidth() + 2.0f;
240 g2d.drawString(message, x, y);
245 public void paint(Graphics g) {
246 Graphics2D g2d = (Graphics2D) g;
248 boolean queued = false;
251 AltosSiteMap.debug_component(this, "already painting");
254 AltosSiteMap.debug_component(this, "paint");
257 AltosSiteMapImage aimage;
259 aimage = AltosSiteMapCache.get_image(this, file, px_size, px_size);
260 if (aimage != null) {
261 if (aimage.validate())
262 image = aimage.image;
268 paint_graphics(g2d, image);
272 public void show(int state, Point2D.Double last_pt, Point2D.Double pt)
275 points = new LinkedList<AltosPoint>();
277 points.add(new AltosPoint(pt, state));
279 if (state == AltosLib.ao_flight_boost && boost == null)
281 if (state == AltosLib.ao_flight_landed && landed == null)
286 public AltosSiteMapTile(int in_px_size) {
287 px_size = in_px_size;
288 setPreferredSize(new Dimension(px_size, px_size));