Subversion Repositories Projects

Rev

Rev 1568 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1563 - 1
package dongfang.mkt.ui.offscreendisplay;
2
 
3
/*
4
 * u/l:         geotagged geo:lat=47.327450 geo:lon=8.521657
5
 * l/r:         geotagged geo:lat=47.321749 geo:lon=8.534403
6
 * house:       geotagged geo:lat=47.324614 geo:lon=8.528202
7
 */
8
 
1568 - 9
import java.awt.BasicStroke;
1563 - 10
import java.awt.Color;
1568 - 11
import java.awt.Font;
1563 - 12
import java.awt.Graphics;
1568 - 13
import java.awt.Graphics2D;
1563 - 14
import java.awt.Image;
15
import java.awt.Toolkit;
1568 - 16
import java.util.Formatter;
17
import java.util.Locale;
1563 - 18
 
19
import javax.swing.JPanel;
20
 
21
import dongfang.mkt.datatype.GPSPosition;
1568 - 22
import dongfang.mkt.frames.OSDDataResponseFrame;
1563 - 23
 
1568 - 24
public class MapImageView extends JPanel implements OSDDataConsumer {
1563 - 25
        private Image mapImage;
26
        private Image copterImage;
27
 
1568 - 28
        private static final int MAPWIDTH = (int)(1132);
29
        private static final int MAPHEIGHT = (int)(977);
1563 - 30
 
31
        // It is assumed the map is north/south oriented!
32
        private GPSPosition upperLeft;
33
        private GPSPosition lowerRight;
34
 
1568 - 35
        private GPSPosition copterPosition;
36
        private GPSPosition homePosition;
1563 - 37
 
1568 - 38
        private int noseDirection;
39
        private int flightDirection;
40
        private double speed;
41
        private double height;
42
 
43
        private static final double degToRadianFactor = 360 / 2 / Math.PI;
1563 - 44
 
45
        private double relativeX(GPSPosition pos) {
46
                double lon = pos.getLongitude();
47
                // The sign should be OK here (positive) both east and west.
48
                double span = lowerRight.getLongitude() - upperLeft.getLongitude();
49
                double offset = lon - upperLeft.getLongitude();
1568 - 50
                double result = offset / span;
51
                if (result<0) result = 0; else if(result>1) result=1;
52
                return result;
1563 - 53
        }
54
 
55
        private double relativeY(GPSPosition pos) {
56
                double lat = pos.getLatitude();
57
                // The sign should be OK here (positive) both east and west.
58
                double span = lowerRight.getLatitude() - upperLeft.getLatitude();
59
                double offset = lat - upperLeft.getLatitude();
1568 - 60
                double result = offset / span;
61
                if (result<0) result = 0; else if(result>1) result=1;
62
                return result;
1563 - 63
        }
64
 
65
        public void paintComponent(Graphics g) {
1568 - 66
                int size = 32;
67
                int noseDotSize = 10;
68
 
69
                Graphics2D g2d = (Graphics2D)g;
70
                g2d.setStroke(new BasicStroke(5));
71
 
1563 - 72
                // Draw our Image object.
1568 - 73
 
74
                g2d.drawImage(mapImage, 0, 0, MAPWIDTH, MAPHEIGHT, this); // at location
75
 
76
                if (homePosition != null) {
77
                        g.setColor(Color.blue);
78
                        int x = (int)(MAPWIDTH * relativeX(homePosition) + 0.5);
79
                        int y = (int)(MAPHEIGHT * relativeY(homePosition) + 0.5);
80
                        g.drawRect(x-size/2, y-size/2, size, size);
81
                }
82
 
83
                if (copterPosition != null) {
84
                        int x = (int)(MAPWIDTH * relativeX(copterPosition) + 0.5);
85
                        int y = (int)(MAPHEIGHT * relativeY(copterPosition) + 0.5);
86
 
87
                        //g2d.clearRect(x-100, y-100, 200, 200);
88
 
89
                        double speedfactor = 15;
90
 
91
                        int speedArrowX = x + (int)(speedfactor * speed * Math.cos((90 - flightDirection) / degToRadianFactor));
92
                        int speedArrowY = y - (int)(speedfactor * speed * Math.sin((90 - flightDirection) / degToRadianFactor));
93
 
94
                        int noseDotX = x + (int)(size/2 * Math.cos((90 - noseDirection) / degToRadianFactor));
95
                        int noseDotY = y - (int)(size/2 * Math.sin((90 - noseDirection) / degToRadianFactor));
96
                        int noseDotEndX = x + (int)(size * Math.cos((90 - noseDirection) / degToRadianFactor));
97
                        int noseDotEndY = y - (int)(size * Math.sin((90 - noseDirection) / degToRadianFactor));
98
 
99
                        g2d.setColor(Color.yellow);
100
                        g.drawLine(x, y, speedArrowX, speedArrowY);
101
 
102
                        g2d.setColor(Color.red);
103
                        g.drawArc(x-size/2, y-size/2, size, size, 0, 360);
104
 
105
                        //g2d.setStroke(new BasicStroke(5));
106
                        g.drawLine(noseDotX, noseDotY, noseDotEndX, noseDotEndY);
107
 
108
                        String heightFormatString = "%+10.1f";
109
                        StringBuilder sb = new StringBuilder();
110
                        Formatter formatter = new Formatter(sb, Locale.US);
111
                        formatter.format(heightFormatString, height);
112
 
113
                        g.setColor(Color.green);
114
                        Font f = g2d.getFont();
115
                        f = f.deriveFont(18.0f);
116
                        f = f.deriveFont(Font.BOLD);
117
                        g2d.setFont(f);
118
                        g.drawString(sb.toString(), x-120, y);
119
                }
1563 - 120
        }
121
 
1568 - 122
        public void update(OSDDataResponseFrame data, long timestamp) {
123
                if (data==null) return;
124
                copterPosition = data.getCurrentPosition();
125
                homePosition = data.getHomePosition();
126
                speed = data.getGroundSpeed();
127
                height = data.getHeightByPressure();
128
                noseDirection = data.getCompassHeading();
129
                flightDirection = data.getDirectionOfFlight();
130
 
131
                repaint();
132
        }
133
 
134
        public void init() {
1563 - 135
                GPSPosition upperLeft = new GPSPosition();
136
                GPSPosition lowerRight = new GPSPosition();
137
 
1568 - 138
                upperLeft.setLatitude(47.328355);
139
                upperLeft.setLongitude(8.518231);
1563 - 140
 
141
                // * l/r:               geotagged geo:lat=47.321749 geo:lon=8.534403
1568 - 142
                lowerRight.setLatitude(47.321532);
143
                lowerRight.setLongitude(8.530822);
1563 - 144
 
1570 - 145
//              upperLeft.setLatitude(47.344577);
146
//              upperLeft.setLongitude(8.529304);
1568 - 147
 
148
                // * l/r:               geotagged geo:lat=47.321749 geo:lon=8.534403
1570 - 149
//              lowerRight.setLatitude(47.337576);
150
//              lowerRight.setLongitude(8.542220);
1563 - 151
 
1568 - 152
                setSize(MAPWIDTH, MAPHEIGHT);
153
                this.upperLeft = upperLeft;
154
                this.lowerRight = lowerRight;
1570 - 155
                mapImage = Toolkit.getDefaultToolkit().getImage("flugplatz_large.jpg");
1563 - 156
        }
157
}