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_11;
21 import java.awt.event.*;
22 import java.awt.image.*;
25 import java.lang.Math;
26 import java.awt.geom.*;
28 import java.util.concurrent.*;
29 import javax.imageio.*;
30 import org.altusmetrum.altoslib_11.*;
32 public class AltosUIMap extends JComponent implements AltosFlightDisplay, AltosMapInterface {
39 static Point2D.Double point2d(AltosPointDouble pt) {
40 return new Point2D.Double(pt.x, pt.y);
43 static final AltosPointDouble point_double(Point pt) {
44 return new AltosPointDouble(pt.x, pt.y);
47 class MapMark extends AltosMapMark {
48 public void paint(AltosMapTransform t) {
49 AltosPointDouble pt = t.screen(lat_lon);
51 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
52 RenderingHints.VALUE_ANTIALIAS_ON);
53 g.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
55 if (0 <= state && state < AltosUIMap.stateColors.length)
56 g.setColor(AltosUIMap.stateColors[state]);
58 g.setColor(AltosUIMap.stateColors[AltosLib.ao_flight_invalid]);
60 g.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
61 g.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
62 g.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
65 MapMark(double lat, double lon, int state) {
66 super(lat, lon, state);
70 class MapView extends JComponent implements MouseMotionListener, MouseListener, ComponentListener, MouseWheelListener {
72 private VolatileImage create_back_buffer() {
73 return getGraphicsConfiguration().createCompatibleVolatileImage(getWidth(), getHeight());
76 private void do_paint(Graphics my_g) {
77 g = (Graphics2D) my_g;
82 public void paint(Graphics my_g) {
83 VolatileImage back_buffer = create_back_buffer();
85 Graphics2D top_g = (Graphics2D) my_g;
88 GraphicsConfiguration gc = getGraphicsConfiguration();
89 int code = back_buffer.validate(gc);
90 if (code == VolatileImage.IMAGE_INCOMPATIBLE)
91 back_buffer = create_back_buffer();
93 Graphics g_back = back_buffer.getGraphics();
94 g_back.setClip(top_g.getClip());
98 top_g.drawImage(back_buffer, 0, 0, this);
99 } while (back_buffer.contentsLost());
103 public void repaint(AltosRectangle damage) {
104 repaint(damage.x, damage.y, damage.width, damage.height);
107 private boolean is_drag_event(MouseEvent e) {
108 return e.getModifiers() == InputEvent.BUTTON1_MASK;
111 /* MouseMotionListener methods */
113 public void mouseDragged(MouseEvent e) {
114 map.touch_continue(e.getPoint().x, e.getPoint().y, is_drag_event(e));
117 public void mouseMoved(MouseEvent e) {
120 /* MouseListener methods */
121 public void mouseClicked(MouseEvent e) {
124 public void mouseEntered(MouseEvent e) {
127 public void mouseExited(MouseEvent e) {
130 public void mousePressed(MouseEvent e) {
131 map.touch_start(e.getPoint().x, e.getPoint().y, is_drag_event(e));
134 public void mouseReleased(MouseEvent e) {
137 /* MouseWheelListener methods */
139 public void mouseWheelMoved(MouseWheelEvent e) {
140 int zoom_change = e.getWheelRotation();
142 map.set_zoom_centre(map.get_zoom() - zoom_change, new AltosPointInt(e.getPoint().x, e.getPoint().y));
145 /* ComponentListener methods */
147 public void componentHidden(ComponentEvent e) {
150 public void componentMoved(ComponentEvent e) {
153 public void componentResized(ComponentEvent e) {
157 public void componentShown(ComponentEvent e) {
162 addComponentListener(this);
163 addMouseMotionListener(this);
164 addMouseListener(this);
165 addMouseWheelListener(this);
169 class MapLine extends AltosMapLine {
171 public void paint(AltosMapTransform t) {
173 if (start == null || end == null)
176 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
178 Line2D.Double line = new Line2D.Double(point2d(t.screen(start)),
179 point2d(t.screen(end)));
181 g.setColor(Color.WHITE);
182 g.setStroke(new BasicStroke(stroke_width+4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
185 g.setColor(Color.BLUE);
186 g.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
189 String message = line_dist();
191 bounds = line_font.getStringBounds(message, g.getFontRenderContext());
193 float x = (float) line.x1;
194 float y = (float) line.y1 + (float) bounds.getHeight() / 2.0f;
196 if (line.x1 < line.x2) {
197 x -= (float) bounds.getWidth() + 2.0f;
202 g.setFont(line_font);
203 g.setColor(Color.WHITE);
204 for (int dy = -2; dy <= 2; dy += 2)
205 for (int dx = -2; dx <= 2; dx += 2)
206 g.drawString(message, x + dx, y + dy);
207 g.setColor(Color.BLUE);
208 g.drawString(message, x, y);
215 class MapPath extends AltosMapPath {
216 public void paint(AltosMapTransform t) {
217 Point2D.Double prev = null;
219 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
220 RenderingHints.VALUE_ANTIALIAS_ON);
221 g.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
223 for (AltosMapPathPoint point : points) {
224 Point2D.Double cur = point2d(t.screen(point.lat_lon));
226 Line2D.Double line = new Line2D.Double (prev, cur);
227 Rectangle bounds = line.getBounds();
229 if (g.hitClip(bounds.x, bounds.y, bounds.width, bounds.height)) {
230 if (0 <= point.state && point.state < AltosUIMap.stateColors.length)
231 g.setColor(AltosUIMap.stateColors[point.state]);
233 g.setColor(AltosUIMap.stateColors[AltosLib.ao_flight_invalid]);
243 class MapTile extends AltosMapTile {
244 public MapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
245 super(cache, upper_left, center, zoom, maptype, px_size, scale);
248 public void paint(AltosMapTransform t) {
250 AltosPointDouble point_double = t.screen(upper_left);
251 Point point = new Point((int) (point_double.x + 0.5),
252 (int) (point_double.y + 0.5));
254 if (!g.hitClip(point.x, point.y, px_size, px_size))
257 AltosImage altos_image = get_image();
258 AltosUIImage ui_image = (AltosUIImage) altos_image;
261 if (ui_image != null)
262 image = ui_image.image;
265 g.drawImage(image, point.x, point.y, null);
267 g.setColor(Color.GRAY);
268 g.fillRect(point.x, point.y, px_size, px_size);
270 if (t.has_location()) {
271 String message = null;
273 case AltosMapTile.fetching:
274 message = "Fetching...";
276 case AltosMapTile.bad_request:
277 message = "Internal error";
279 case AltosMapTile.failed:
280 message = "Network error, check connection";
282 case AltosMapTile.forbidden:
283 message = "Too many requests, try later";
286 if (message != null && tile_font != null) {
287 g.setFont(tile_font);
288 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
289 Rectangle2D bounds = tile_font.getStringBounds(message, g.getFontRenderContext());
291 float x = px_size / 2.0f;
292 float y = px_size / 2.0f;
293 x = x - (float) bounds.getWidth() / 2.0f;
294 y = y + (float) bounds.getHeight() / 2.0f;
295 g.setColor(Color.BLACK);
296 g.drawString(message, (float) point_double.x + x, (float) point_double.y + y);
303 public static final Color stateColors[] = {
304 Color.WHITE, // startup
309 Color.YELLOW, // coast
310 Color.CYAN, // drogue
312 Color.BLACK, // landed
313 Color.BLACK, // invalid
314 Color.CYAN, // stateless
317 /* AltosMapInterface functions */
319 public AltosMapPath new_path() {
320 return new MapPath();
323 public AltosMapLine new_line() {
324 return new MapLine();
327 public AltosImage load_image(File file) throws Exception {
328 return new AltosUIImage(ImageIO.read(file));
331 public AltosMapMark new_mark(double lat, double lon, int state) {
332 return new MapMark(lat, lon, state);
335 public AltosMapTile new_tile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
336 return new MapTile(cache, upper_left, center, zoom, maptype, px_size, scale);
340 return view.getWidth();
343 public int height() {
344 return view.getHeight();
347 public void repaint() {
351 public void repaint(AltosRectangle damage) {
352 view.repaint(damage);
355 public void set_zoom_label(String label) {
356 zoom_label.setText(label);
359 public void select_object(AltosLatLon latlon) {
360 debug("select at %f,%f\n", latlon.lat, latlon.lon);
363 public void debug(String format, Object ... arguments) {
364 if (AltosUIPreferences.serial_debug())
365 System.out.printf(format, arguments);
369 /* AltosFlightDisplay interface */
371 public void set_font() {
372 tile_font = AltosUILib.value_font;
373 line_font = AltosUILib.status_font;
376 public void font_size_changed(int font_size) {
381 public void units_changed(boolean imperial_units) {
387 public void set_maptype(int type) {
388 map.set_maptype(type);
389 maptype_combo.setSelectedIndex(type);
392 /* AltosUIMapPreload functions */
394 public void set_zoom(int zoom) {
398 public void add_mark(double lat, double lon, int status) {
399 map.add_mark(lat, lon, status);
402 public void clear_marks() {
406 /* AltosFlightDisplay interface */
407 public void reset() {
411 public void show(AltosState state, AltosListenerState listener_state) {
412 map.show(state, listener_state);
415 public String getName() {
419 /* AltosGraphUI interface */
420 public void centre(AltosState state) {
424 /* internal layout bits */
425 private GridBagLayout layout = new GridBagLayout();
427 JComboBox<String> maptype_combo;
431 public AltosUIMap() {
435 view = new MapView();
437 view.setPreferredSize(new Dimension(500,500));
438 view.setVisible(true);
439 view.setEnabled(true);
441 GridBagLayout my_layout = new GridBagLayout();
443 setLayout(my_layout);
445 GridBagConstraints c = new GridBagConstraints();
446 c.anchor = GridBagConstraints.CENTER;
447 c.fill = GridBagConstraints.BOTH;
458 zoom_label = new JLabel("", JLabel.CENTER);
460 c = new GridBagConstraints();
461 c.anchor = GridBagConstraints.CENTER;
462 c.fill = GridBagConstraints.HORIZONTAL;
469 JButton zoom_reset = new JButton("0");
470 zoom_reset.addActionListener(new ActionListener() {
471 public void actionPerformed(ActionEvent e) {
472 map.set_zoom(map.default_zoom);
476 c = new GridBagConstraints();
477 c.anchor = GridBagConstraints.CENTER;
478 c.fill = GridBagConstraints.HORIZONTAL;
485 JButton zoom_in = new JButton("+");
486 zoom_in.addActionListener(new ActionListener() {
487 public void actionPerformed(ActionEvent e) {
488 map.set_zoom(map.get_zoom() + 1);
492 c = new GridBagConstraints();
493 c.anchor = GridBagConstraints.CENTER;
494 c.fill = GridBagConstraints.HORIZONTAL;
501 JButton zoom_out = new JButton("-");
502 zoom_out.addActionListener(new ActionListener() {
503 public void actionPerformed(ActionEvent e) {
504 map.set_zoom(map.get_zoom() - 1);
507 c = new GridBagConstraints();
508 c.anchor = GridBagConstraints.CENTER;
509 c.fill = GridBagConstraints.HORIZONTAL;
516 maptype_combo = new JComboBox<String>(map.maptype_labels);
518 maptype_combo.setEditable(false);
519 maptype_combo.setMaximumRowCount(maptype_combo.getItemCount());
520 maptype_combo.addItemListener(new ItemListener() {
521 public void itemStateChanged(ItemEvent e) {
522 map.set_maptype(maptype_combo.getSelectedIndex());
526 c = new GridBagConstraints();
527 c.anchor = GridBagConstraints.CENTER;
528 c.fill = GridBagConstraints.HORIZONTAL;
533 add(maptype_combo, c);
535 map = new AltosMap(this);