AltosSiteMap: add autoscroll and grabndrag scroll
authorAnthony Towns <aj@erisian.com.au>
Sat, 20 Nov 2010 11:06:37 +0000 (21:06 +1000)
committerAnthony Towns <aj@erisian.com.au>
Sat, 20 Nov 2010 11:06:37 +0000 (21:06 +1000)
ao-tools/altosui/AltosFlightUI.java
ao-tools/altosui/AltosSiteMap.java
ao-tools/altosui/AltosSiteMapTile.java

index 6db6c67b192d04ad792bd3806be2380412fa4d56..c85fc9776259a150d5eabd8f1ed4ea46de08bb7e 100644 (file)
@@ -46,7 +46,6 @@ public class AltosFlightUI extends JFrame implements AltosFlightDisplay {
 
        private AltosFlightStatus flightStatus;
        private JScrollPane flightInfoPane;
-       private JScrollPane sitemapPane;
        private AltosInfoTable flightInfo;
 
        static final int tab_pad = 1;
@@ -192,8 +191,7 @@ public class AltosFlightUI extends JFrame implements AltosFlightDisplay {
                pane.add("Table", flightInfoPane);
 
         sitemap = new AltosSiteMap();
-               sitemapPane = new JScrollPane(sitemap);
-        pane.add("Site Map", sitemapPane);
+        pane.add("Site Map", sitemap);
 
                c.gridx = 0;
                c.gridy = 2;
index df1cc246a9948b071e814115471264e49be90ee0..1db839597f8e05f0ade863cfa4a03f3f160612c4 100644 (file)
@@ -21,6 +21,7 @@ import java.awt.*;
 import java.awt.image.*;
 import java.awt.event.*;
 import javax.swing.*;
+import javax.swing.event.MouseInputAdapter;
 import javax.imageio.ImageIO;
 import javax.swing.table.*;
 import java.io.*;
@@ -31,7 +32,7 @@ import java.lang.Math;
 import java.awt.geom.Point2D;
 import java.awt.geom.Line2D;
 
-public class AltosSiteMap extends JComponent implements AltosFlightDisplay {
+public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay {
     public void reset() {
         // nothing
     }
@@ -43,10 +44,40 @@ public class AltosSiteMap extends JComponent implements AltosFlightDisplay {
   
     AltosSiteMapTile [] mapTiles = new AltosSiteMapTile[9];
 
+    class GrabNDrag extends MouseInputAdapter {
+        private JComponent scroll;
+        private Point startPt = new Point();
+
+        public GrabNDrag(JComponent parent) {
+            scroll = parent;
+        }
+
+        public void mousePressed(MouseEvent e) {
+            startPt.setLocation(e.getPoint());
+        }
+        public void mouseDragged(MouseEvent e) {
+            int xd = e.getX() - startPt.x;
+            int yd = e.getY() - startPt.y;
+
+            Rectangle r = scroll.getVisibleRect();
+            r.x -= xd;
+            r.y -= yd;
+            scroll.scrollRectToVisible(r);
+        }
+    }
+
     public AltosSiteMap() {
+        JComponent comp = new JComponent() {
+            GrabNDrag scroller = new GrabNDrag(this);
+            {
+                addMouseMotionListener(scroller);
+                addMouseListener(scroller);
+                setAutoscrolls(true);
+            }
+        };
 
         GridBagLayout layout = new GridBagLayout();
-        setLayout(layout);
+        comp.setLayout(layout);
 
         GridBagConstraints c = new GridBagConstraints();
         c.anchor = GridBagConstraints.CENTER;
@@ -56,8 +87,9 @@ public class AltosSiteMap extends JComponent implements AltosFlightDisplay {
             c.gridx = x % 3; c.gridy = x / 3;
             mapTiles[x] = new AltosSiteMapTile((x%3)-1, (x/3)-1);
             layout.setConstraints(mapTiles[x], c);
-            add(mapTiles[x]);
+            comp.add(mapTiles[x]);
         }
+        setViewportView(comp);
     }
 }
 
index c14fb93f0837ef1be9048a328cab8abb1a9329c1..df57aa7dec6cc6f50c611b14a61160f113d8a49c 100644 (file)
@@ -65,21 +65,19 @@ public class AltosSiteMapTile extends JLabel {
         Point2D.Double map_latlng;
         map_latlng = latlng(new Point2D.Double(px_size/2, px_size/2));
 
-        BufferedImage myPicture;
         File pngfile = new File(AltosPreferences.logdir(), 
                                 FileCoord(map_latlng, zoom));
         try {
+            BufferedImage myPicture;
             myPicture = ImageIO.read(pngfile);
+            setIcon(new ImageIcon( myPicture ));
             System.out.printf("# Found file %s\n", pngfile);
+            g2d = myPicture.createGraphics();
         } catch (Exception e) { 
             // throw new RuntimeException(e);
             System.out.printf("# Failed to find file %s\n", pngfile);
             System.out.printf(" wget -O '%s' 'http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&sensor=false&maptype=hybrid&format=png32'\n", pngfile, map_latlng.x, map_latlng.y, zoom, px_size, px_size);
-            myPicture = new BufferedImage(px_size, px_size, 
-                    BufferedImage.TYPE_INT_RGB);
         }
-        setIcon(new ImageIcon( myPicture ));
-        g2d = myPicture.createGraphics();
         return true;
     }
 
@@ -167,8 +165,8 @@ public class AltosSiteMapTile extends JLabel {
             return;
         if (!state.gps_ready && state.pad_lat == 0 && state.pad_lon == 0)
             return;
-        double plat = (int)(state.pad_lat*200)/200.0;
-        double plon = (int)(state.pad_lon*200)/200.0;
+        double plat = state.pad_lat;
+        double plon = state.pad_lon;
 
         if (last_pt == null) {
             if (!setLocation(plat, plon)) {
@@ -185,6 +183,19 @@ public class AltosSiteMapTile extends JLabel {
             g2d.draw(new Line2D.Double(last_pt, pt));
         }
 
+        if (0 <= pt.x && pt.x < px_size) {
+            if (0 <= pt.y && pt.y < px_size) {
+                int dx = 500, dy = 250;
+                if (last_pt != null && state.state > 2) {
+                    dx = Math.min(200, 20 + (int) Math.abs(last_pt.x - pt.x));
+                    dy = Math.min(100, 10 + (int) Math.abs(last_pt.y - pt.y));
+                }
+                Rectangle r = new Rectangle((int)pt.x-dx, (int)pt.y-dy, 
+                                            dx*2, dy*2);
+                scrollRectToVisible(r);
+            }
+        }
+
         if (state.state == 8 && !drawn_landed_circle) {
             drawn_landed_circle = true;
             g2d.setColor(Color.RED);
@@ -198,6 +209,13 @@ public class AltosSiteMapTile extends JLabel {
     }
    
     public AltosSiteMapTile(int x_tile_offset, int y_tile_offset) {
+        BufferedImage myPicture = new BufferedImage(px_size, px_size, 
+                BufferedImage.TYPE_INT_RGB);
+        setIcon(new ImageIcon( myPicture ));
+        g2d = myPicture.createGraphics();
+        g2d.setColor(Color.GRAY);
+        g2d.fillRect(0, 0, px_size, px_size);
+
         off_x = x_tile_offset;
         off_y = y_tile_offset;
     }