+ class MapMark extends AltosMapMark {
+ public void paint(AltosMapTransform t) {
+ AltosPointDouble pt = t.screen(lat_lon);
+
+ g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+ RenderingHints.VALUE_ANTIALIAS_ON);
+ g.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
+
+ if (0 <= state && state < AltosUIMap.stateColors.length)
+ g.setColor(AltosUIMap.stateColors[state]);
+ else
+ g.setColor(AltosUIMap.stateColors[AltosLib.ao_flight_invalid]);
+
+ g.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
+ g.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
+ g.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
+ }
+
+ MapMark(double lat, double lon, int state) {
+ super(lat, lon, state);
+ }
+ }
+
+ class MapView extends JComponent implements MouseMotionListener, MouseListener, ComponentListener, MouseWheelListener {
+
+ private VolatileImage create_back_buffer() {
+ return getGraphicsConfiguration().createCompatibleVolatileImage(getWidth(), getHeight());
+ }
+
+ private void do_paint(Graphics my_g) {
+ g = (Graphics2D) my_g;
+
+ map.paint();
+ }
+
+ public void paint(Graphics my_g) {
+ VolatileImage back_buffer = create_back_buffer();
+
+ Graphics2D top_g = (Graphics2D) my_g;
+
+ do {
+ GraphicsConfiguration gc = getGraphicsConfiguration();
+ int code = back_buffer.validate(gc);
+ if (code == VolatileImage.IMAGE_INCOMPATIBLE)
+ back_buffer = create_back_buffer();
+
+ Graphics g_back = back_buffer.getGraphics();
+ g_back.setClip(top_g.getClip());
+ do_paint(g_back);
+ g_back.dispose();
+
+ top_g.drawImage(back_buffer, 0, 0, this);
+ } while (back_buffer.contentsLost());
+ back_buffer.flush();
+ }
+
+ public void repaint(AltosRectangle damage) {
+ repaint(damage.x, damage.y, damage.width, damage.height);
+ }
+
+ private boolean is_drag_event(MouseEvent e) {
+ return e.getModifiersEx() == InputEvent.BUTTON1_DOWN_MASK;
+ }
+
+ /* MouseMotionListener methods */
+
+ public void mouseDragged(MouseEvent e) {
+ map.touch_continue(e.getPoint().x, e.getPoint().y, is_drag_event(e));
+ }
+
+ String pos(double p, String pos, String neg) {
+ if (p == AltosLib.MISSING)
+ return "";
+ String h = pos;
+ if (p < 0) {
+ h = neg;
+ p = -p;
+ }
+ int deg = (int) Math.floor(p);
+ double min = (p - Math.floor(p)) * 60.0;
+ return String.format("%s %4d° %9.6f'", h, deg, min);
+ }
+
+ String height(double h, String label) {
+ if (h == AltosLib.MISSING)
+ return "";
+ return String.format(" %s%s",
+ AltosConvert.height.show(6, h),
+ label);
+ }
+
+ String speed(double s, String label) {
+ if (s == AltosLib.MISSING)
+ return "";
+ return String.format(" %s%s",
+ AltosConvert.speed.show(6, s),
+ label);
+ }
+
+ public void mouseMoved(MouseEvent e) {
+ AltosMapPathPoint point = map.nearest(e.getPoint().x, e.getPoint().y);
+
+ if (point != null) {
+ if (nearest_mark == null)
+ nearest_mark = map.add_mark(point.gps.lat,
+ point.gps.lon,
+ point.state);
+ else {
+ nearest_mark.lat_lon.lat = point.gps.lat;
+ nearest_mark.lat_lon.lon = point.gps.lon;
+ nearest_mark.state = point.state;
+ }
+ nearest_label.setText(String.format("%9.2f sec %s%s%s%s",
+ point.time,
+ pos(point.gps.lat,
+ "N", "S"),
+ pos(point.gps.lon,
+ "E", "W"),
+ height(point.gps_height, ""),
+ speed(point.gps.ground_speed, "(h)"),
+ speed(point.gps.climb_rate, "(v)")));
+ } else {
+ nearest_label.setText("");
+ }
+ repaint();
+ }
+
+ /* MouseListener methods */
+ public void mouseClicked(MouseEvent e) {
+ }
+
+ public void mouseEntered(MouseEvent e) {
+ }
+
+ public void mouseExited(MouseEvent e) {
+ }
+
+ public void mousePressed(MouseEvent e) {
+ map.touch_start(e.getPoint().x, e.getPoint().y, is_drag_event(e));
+ }
+
+ public void mouseReleased(MouseEvent e) {
+ }
+
+ /* MouseWheelListener methods */
+
+ public void mouseWheelMoved(MouseWheelEvent e) {
+ int zoom_change = e.getWheelRotation();
+
+ map.set_zoom_centre(map.get_zoom() - zoom_change, new AltosPointInt(e.getPoint().x, e.getPoint().y));
+ }
+
+ /* ComponentListener methods */
+
+ public void componentHidden(ComponentEvent e) {
+ }
+
+ public void componentMoved(ComponentEvent e) {
+ }
+
+ public void componentResized(ComponentEvent e) {
+ map.set_transform();
+ }
+
+ public void componentShown(ComponentEvent e) {
+ map.set_transform();
+ }
+
+ MapView() {
+ addComponentListener(this);
+ addMouseMotionListener(this);
+ addMouseListener(this);
+ addMouseWheelListener(this);
+ }
+ }
+
+ class MapLine extends AltosMapLine {
+
+ public void paint(AltosMapTransform t) {
+
+ if (start == null || end == null)
+ return;
+
+ g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+
+ Line2D.Double line = new Line2D.Double(point2d(t.screen(start)),
+ point2d(t.screen(end)));
+
+ g.setColor(Color.WHITE);
+ g.setStroke(new BasicStroke(stroke_width+4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
+ g.draw(line);
+
+ g.setColor(Color.BLUE);
+ g.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
+ g.draw(line);
+
+ String message = line_dist();
+ Rectangle2D bounds;
+ bounds = line_font.getStringBounds(message, g.getFontRenderContext());
+
+ float x = (float) line.x1;
+ float y = (float) line.y1 + (float) bounds.getHeight() / 2.0f;
+
+ if (line.x1 < line.x2) {
+ x -= (float) bounds.getWidth() + 2.0f;
+ } else {
+ x += 2.0f;
+ }
+
+ g.setFont(line_font);
+ g.setColor(Color.WHITE);
+ for (int dy = -2; dy <= 2; dy += 2)
+ for (int dx = -2; dx <= 2; dx += 2)
+ g.drawString(message, x + dx, y + dy);
+ g.setColor(Color.BLUE);
+ g.drawString(message, x, y);
+ }
+
+ public MapLine() {
+ }
+ }
+
+ class MapPath extends AltosMapPath {
+ public void paint(AltosMapTransform t) {
+ Point2D.Double prev = null;
+
+ g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+ RenderingHints.VALUE_ANTIALIAS_ON);
+ g.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
+
+ for (AltosMapPathPoint point : points) {
+ Point2D.Double cur = point2d(t.screen(point.gps.lat, point.gps.lon));
+ if (prev != null) {
+ Line2D.Double line = new Line2D.Double (prev, cur);
+ Rectangle bounds = line.getBounds();
+
+ if (g.hitClip(bounds.x, bounds.y, bounds.width, bounds.height)) {
+ if (0 <= point.state && point.state < AltosUIMap.stateColors.length)
+ g.setColor(AltosUIMap.stateColors[point.state]);
+ else
+ g.setColor(AltosUIMap.stateColors[AltosLib.ao_flight_invalid]);
+
+ g.draw(line);
+ }
+ }
+ prev = cur;
+ }
+ }
+ }
+
+ class MapTile extends AltosMapTile {
+ public MapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
+ super(cache, upper_left, center, zoom, maptype, px_size, scale);
+ }
+
+ public void paint(AltosMapTransform t) {
+
+ AltosPointDouble point_double = t.screen(upper_left);
+ Point point = new Point((int) (point_double.x + 0.5),
+ (int) (point_double.y + 0.5));
+
+ if (!g.hitClip(point.x, point.y, px_size, px_size))
+ return;
+
+ AltosImage altos_image = get_image();
+ AltosUIImage ui_image = (AltosUIImage) altos_image;
+ Image image = null;
+
+ if (ui_image != null)
+ image = ui_image.image;
+
+ if (image != null) {
+ g.drawImage(image, point.x, point.y, null);
+/*
+ * Useful when debugging map fetching problems
+ *
+ String message = String.format("%.6f %.6f", center.lat, center.lon);
+ g.setFont(tile_font);
+ g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+ Rectangle2D bounds = tile_font.getStringBounds(message, g.getFontRenderContext());
+
+ float x = px_size / 2.0f;
+ float y = px_size / 2.0f;
+ x = x - (float) bounds.getWidth() / 2.0f;
+ y = y + (float) bounds.getHeight() / 2.0f;
+ g.setColor(Color.RED);
+ g.drawString(message, (float) point_double.x + x, (float) point_double.y + y);
+*/
+ } else {
+ g.setColor(Color.GRAY);
+ g.fillRect(point.x, point.y, px_size, px_size);
+
+ if (t.has_location()) {
+ String message = null;
+ switch (status) {
+ case AltosMapTile.fetching:
+ message = "Fetching...";
+ break;
+ case AltosMapTile.bad_request:
+ message = "Internal error";
+ break;
+ case AltosMapTile.failed:
+ message = "Network error";
+ break;
+ case AltosMapTile.forbidden:
+ message = "Outside of known launch areas";
+ break;
+ }
+ if (message != null && tile_font != null) {
+ g.setFont(tile_font);
+ g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+ Rectangle2D bounds = tile_font.getStringBounds(message, g.getFontRenderContext());
+
+ float x = px_size / 2.0f;
+ float y = px_size / 2.0f;
+ x = x - (float) bounds.getWidth() / 2.0f;
+ y = y + (float) bounds.getHeight() / 2.0f;
+ g.setColor(Color.BLACK);
+ g.drawString(message, (float) point_double.x + x, (float) point_double.y + y);
+ }
+ }
+ }
+ }
+ }