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 SwingUtilities.invokeLater(new Runnable() {
61 public void load_map(File pngFile) {
66 private Font font = null;
68 public void set_font(Font font) {
70 this.status = AltosSiteMapCache.success;
74 public void set_status(int status) {
80 public void clearMap() {
85 status = AltosSiteMapCache.success;
90 static Color stateColors[] = {
91 Color.WHITE, // startup
96 Color.YELLOW, // coast
102 private void draw_circle(Graphics g, Point2D.Double pt) {
103 g.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
104 g.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
105 g.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
108 public void set_boost(Point2D.Double boost) {
113 public void set_line(Line2D.Double line, double distance) {
115 line_dist = distance;
119 private String line_dist() {
121 double distance = line_dist;
123 if (AltosConvert.imperial_units) {
124 distance = AltosConvert.meters_to_feet(distance);
125 if (distance < 10000) {
131 else if (distance < 100)
133 else if (distance < 1000)
139 if (distance < 10000) {
145 else if (distance < 1000)
151 return String.format(format, distance);
154 public void paint(Graphics g) {
155 Graphics2D g2d = (Graphics2D) g;
156 AltosPoint prev = null;
160 img = AltosSiteMapCache.get_image(this, file, px_size, px_size);
163 g2d.drawImage(img, 0, 0, null);
165 g2d.setColor(Color.GRAY);
166 g2d.fillRect(0, 0, getWidth(), getHeight());
167 String message = null;
169 case AltosSiteMapCache.loading:
170 message = "Loading...";
172 case AltosSiteMapCache.bad_request:
173 message = "Internal error";
175 case AltosSiteMapCache.failed:
176 message = "Network error, check connection";
178 case AltosSiteMapCache.forbidden:
179 message = "Too many requests, try later";
182 if (message != null && font != null) {
184 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
186 bounds = font.getStringBounds(message, g2d.getFontRenderContext());
188 float x = getWidth() / 2.0f;
189 float y = getHeight() / 2.0f;
190 x = x - (float) bounds.getWidth() / 2.0f;
191 y = y + (float) bounds.getHeight() / 2.0f;
192 g2d.setColor(Color.BLACK);
193 g2d.drawString(message, x, y);
197 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
198 RenderingHints.VALUE_ANTIALIAS_ON);
199 g2d.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
201 if (points != null) {
202 for (AltosPoint point : points) {
204 if (0 <= point.state && point.state < stateColors.length)
205 g2d.setColor(stateColors[point.state]);
206 g2d.draw(new Line2D.Double(prev.pt, point.pt));
212 g2d.setColor(Color.RED);
213 draw_circle(g2d, boost);
215 if (landed != null) {
216 g2d.setColor(Color.BLACK);
217 draw_circle(g2d, landed);
221 g2d.setColor(Color.BLUE);
224 String message = line_dist();
226 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
228 bounds = font.getStringBounds(message, g2d.getFontRenderContext());
230 float x = (float) line.x1;
231 float y = (float) line.y1 + (float) bounds.getHeight() / 2.0f;
233 if (line.x1 < line.x2) {
234 x -= (float) bounds.getWidth() + 2.0f;
238 g2d.drawString(message, x, y);
242 public synchronized void show(int state, Point2D.Double last_pt, Point2D.Double pt)
245 points = new LinkedList<AltosPoint>();
247 points.add(new AltosPoint(pt, state));
249 if (state == AltosLib.ao_flight_boost && boost == null)
251 if (state == AltosLib.ao_flight_landed && landed == null)
256 public AltosSiteMapTile(int in_px_size) {
257 px_size = in_px_size;
258 setPreferredSize(new Dimension(px_size, px_size));