telegps: Add graph display
[fw/altos] / altosuilib / AltosSiteMapTile.java
index 9610b248c994e5856dc5cb446e8fb41b6913e49a..f8b924a82cd7444ed74abf08a380271eb62a0a5b 100644 (file)
@@ -44,15 +44,22 @@ public class AltosSiteMapTile extends JComponent {
 
        Point2D.Double  boost;
        Point2D.Double  landed;
+       Line2D.Double   line;
+       double          line_course;
+       double          line_dist;
 
        LinkedList<AltosPoint>  points;
 
        public synchronized void queue_repaint() {
-               SwingUtilities.invokeLater(new Runnable() {
-                               public void run() {
-                                       repaint();
-                               }
-                       });
+               if (SwingUtilities.isEventDispatchThread())
+                       repaint();
+               else {
+                       SwingUtilities.invokeLater(new Runnable() {
+                                       public void run() {
+                                               repaint();
+                                       }
+                               });
+               }
        }
 
        public void load_map(File pngFile) {
@@ -65,13 +72,14 @@ public class AltosSiteMapTile extends JComponent {
        public void set_font(Font font) {
                this.font = font;
                this.status = AltosSiteMapCache.success;
-               queue_repaint();
        }
 
        public void set_status(int status) {
-               file = null;
-               this.status = status;
-               queue_repaint();
+               if (status != this.status || file != null) {
+                       file = null;
+                       this.status = status;
+                       queue_repaint();
+               }
        }
 
        public void clearMap() {
@@ -80,7 +88,7 @@ public class AltosSiteMapTile extends JComponent {
                points = null;
                file = null;
                status = AltosSiteMapCache.success;
-               queue_repaint();
+               line = null;
        }
 
        static Color stateColors[] = {
@@ -106,17 +114,62 @@ public class AltosSiteMapTile extends JComponent {
                queue_repaint();
        }
 
-       public void paint(Graphics g) {
-               Graphics2D      g2d = (Graphics2D) g;
-               AltosPoint      prev = null;
-               Image           img = null;
+       public void set_line(Line2D.Double line, double distance) {
+               this.line = line;
+               line_dist = distance;
+               queue_repaint();
+       }
 
-               if (file != null)
-                       img = AltosSiteMapCache.get_image(this, file, px_size, px_size);
+       private String line_dist() {
+               String  format;
+               double  distance = line_dist;
 
-               if (img != null) {
-                       g2d.drawImage(img, 0, 0, null);
+               if (AltosConvert.imperial_units) {
+                       distance = AltosConvert.meters_to_feet(distance);
+                       if (distance < 10000) {
+                               format = "%4.0fft";
+                       } else {
+                               distance /= 5280;
+                               if (distance < 10)
+                                       format = "%5.3fmi";
+                               else if (distance < 100)
+                                       format = "%5.2fmi";
+                               else if (distance < 1000)
+                                       format = "%5.1fmi";
+                               else
+                                       format = "%5.0fmi";
+                       }
                } else {
+                       if (distance < 10000) {
+                               format = "%4.0fm";
+                       } else {
+                               distance /= 1000;
+                               if (distance < 100)
+                                       format = "%5.2fkm";
+                               else if (distance < 1000)
+                                       format = "%5.1fkm";
+                               else
+                                       format = "%5.0fkm";
+                       }
+               }
+               return String.format(format, distance);
+       }
+
+       int     painting_serial;
+       int     painted_serial;
+
+       public void paint_graphics(Graphics2D g2d, Image image, int serial) {
+
+               if (serial < painted_serial)
+                       return;
+
+               painted_serial = serial;
+
+               if (image != null) {
+                       AltosSiteMap.debug_component(this, "paint_graphics");
+                       g2d.drawImage(image, 0, 0, null);
+               } else {
+                       AltosSiteMap.debug_component(this, "erase_graphics");
                        g2d.setColor(Color.GRAY);
                        g2d.fillRect(0, 0, getWidth(), getHeight());
                        String  message = null;
@@ -143,7 +196,7 @@ public class AltosSiteMapTile extends JComponent {
                                float x = getWidth() / 2.0f;
                                float y = getHeight() / 2.0f;
                                x = x - (float) bounds.getWidth() / 2.0f;
-                               y = y - (float) bounds.getHeight() / 2.0f;
+                               y = y + (float) bounds.getHeight() / 2.0f;
                                g2d.setColor(Color.BLACK);
                                g2d.drawString(message, x, y);
                        }
@@ -154,6 +207,7 @@ public class AltosSiteMapTile extends JComponent {
                g2d.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
 
                if (points != null) {
+                       AltosPoint              prev = null;
                        for (AltosPoint point : points) {
                                if (prev != null) {
                                        if (0 <= point.state && point.state < stateColors.length)
@@ -171,9 +225,54 @@ public class AltosSiteMapTile extends JComponent {
                        g2d.setColor(Color.BLACK);
                        draw_circle(g2d, landed);
                }
+
+               if (line != null) {
+                       g2d.setColor(Color.BLUE);
+                       g2d.draw(line);
+
+                       String  message = line_dist();
+                       g2d.setFont(font);
+                       g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+                       Rectangle2D     bounds;
+                       bounds = font.getStringBounds(message, g2d.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;
+                       }
+                       g2d.drawString(message, x, y);
+               }
+       }
+
+       public void paint(Graphics g) {
+               Graphics2D              g2d = (Graphics2D) g;
+               Image                   image = null;
+               boolean                 queued = false;
+
+               AltosSiteMap.debug_component(this, "paint");
+
+               ++painting_serial;
+
+               if (file != null) {
+                       AltosSiteMapImage       aimage;
+
+                       aimage = AltosSiteMapCache.get_image(this, file, px_size, px_size);
+                       if (aimage != null) {
+                               if (aimage.validate(painting_serial))
+                                       image = aimage.image;
+                               else
+                                       queued = true;
+                       }
+               }
+               if (!queued)
+                       paint_graphics(g2d, image, painting_serial);
        }
 
-       public synchronized void show(int state, Point2D.Double last_pt, Point2D.Double pt)
+       public void show(int state, Point2D.Double last_pt, Point2D.Double pt)
        {
                if (points == null)
                        points = new LinkedList<AltosPoint>();