Subversion Repositories Projects

Rev

Rev 1563 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package dongfang.mkt.ui.offscreendisplay;

/*
 * u/l:         geotagged geo:lat=47.327450 geo:lon=8.521657
 * l/r:         geotagged geo:lat=47.321749 geo:lon=8.534403
 * house:       geotagged geo:lat=47.324614 geo:lon=8.528202
 */


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;

import dongfang.mkt.datatype.GPSPosition;

public class MapImageView extends JPanel {
        private Image mapImage;
        private Image copterImage;

        private static final int MAPWIDTH = 600;
        private static final int MAPHEIGHT = 400;

        // It is assumed the map is north/south oriented!
        private GPSPosition upperLeft;
        private GPSPosition lowerRight;

        private GPSPosition hightlightPosition;
       
        public MapImageView(GPSPosition upperLeft, GPSPosition lowerRight, String imageFileName) {
                setSize(MAPWIDTH, MAPHEIGHT);
                this.upperLeft = upperLeft;
                this.lowerRight = lowerRight;
                mapImage = Toolkit.getDefaultToolkit().getImage(imageFileName);
        }

        private double relativeX(GPSPosition pos) {
                double lon = pos.getLongitude();
                // The sign should be OK here (positive) both east and west.
                double span = lowerRight.getLongitude() - upperLeft.getLongitude();
                double offset = lon - upperLeft.getLongitude();
                return offset / span;
        }

        private double relativeY(GPSPosition pos) {
                double lat = pos.getLatitude();
                // The sign should be OK here (positive) both east and west.
                double span = lowerRight.getLatitude() - upperLeft.getLatitude();
                double offset = lat - upperLeft.getLatitude();
                return offset / span;
        }

        public void paintComponent(Graphics g) {
                // Draw our Image object.
                g.drawImage(mapImage, 0, 0, MAPWIDTH, MAPHEIGHT, this); // at location
                g.setColor(Color.red);
                int x = (int)(MAPWIDTH * relativeX(hightlightPosition) + 0.5);
                int y = (int)(MAPHEIGHT * relativeY(hightlightPosition) + 0.5);
                g.drawArc(x, y, 20, 20, 0, 360);
        }
       
        public static void main(String[] args) {
                GPSPosition upperLeft = new GPSPosition();
                GPSPosition lowerRight = new GPSPosition();
                GPSPosition highlight = new GPSPosition();
               
                upperLeft.setLatitude(47.327450);
                upperLeft.setLongitude(8.521657);

                // * l/r:               geotagged geo:lat=47.321749 geo:lon=8.534403
                lowerRight.setLatitude(47.321749);
                lowerRight.setLongitude(8.534403);
               
                // geotagged geo:lat=47.324614 geo:lon=8.528202
                highlight.setLatitude(47.324714);
                highlight.setLongitude(8.528102);
               
                MapImageView v = new MapImageView(upperLeft, lowerRight, "flugplatz_small.png");
                v.hightlightPosition = highlight;
               
                JFrame f = new JFrame();
                f.getContentPane().add(v);
                f.setSize(v.getSize());
                f.setVisible(true);
        }
}