summaryrefslogtreecommitdiff
path: root/src/com/benburwell/planes/gui/AircraftMapComponent.java
blob: 1992c252e2f5f53d32f08bdab6e5e9a4e2c0b7cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.benburwell.planes.gui;

import com.benburwell.planes.data.AircraftStore;
import com.benburwell.planes.data.AircraftStoreListener;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.ArrayList;

/**
 * Created by ben on 11/18/16.
 */
public class AircraftMapComponent implements ViewComponent {
    private AircraftStore store;
    private AircraftMap mapPanel;

    public AircraftMapComponent(AircraftStore store) {
        this.store = store;
        this.mapPanel = new AircraftMap();
        this.mapPanel.setCenter(40.6188942, -75.4947205);
        this.store.subscribe(new AircraftStoreListener() {
            @Override
            public void aircraftStoreChanged() {
                List<Drawable> planes = new ArrayList<>();
                store.getAircraft().values().forEach(aircraft -> {
                    String name = !aircraft.getCallsign().isEmpty() ? aircraft.getCallsign() : aircraft.getHexIdent();
                    double lat = aircraft.getCurrentPosition().getLatitude();
                    double lon = aircraft.getCurrentPosition().getLongitude();
                    planes.add(new Plane(name, lat, lon));
                });
                mapPanel.setPlanes(planes);
                mapPanel.validate();
                mapPanel.repaint();
            }

            @Override
            public boolean respondTo(String aircraftId) {
                return true;
            }
        });
    }

    @Override
    public JComponent getComponent() {
        return this.mapPanel;
    }

    private class AircraftMap extends JPanel {
        private final float FONT_SIZE = 12;
        private final int TEXT_PADDING = 5;
        private List<Drawable> planes = new ArrayList<>();
        private double centerLatitude;
        private double centerLongitude;

        public AircraftMap() {
            super();
            this.setBackground(Color.black);
            this.setBorder(BorderFactory.createEmptyBorder());
            this.setCenter(0, 0);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            this.planes.forEach(item -> item.drawOn(g, this));
            this.drawPositionAndScale(g);
        }

        public void drawPositionAndScale(Graphics g) {
            Font currentFont = g.getFont();
            Font newFont = currentFont.deriveFont(FONT_SIZE);
            g.setFont(newFont);
            g.setColor(Color.gray);
            g.drawString(String.format("%08.5f N", this.centerLatitude), TEXT_PADDING, (int)FONT_SIZE + TEXT_PADDING);
            g.drawString(String.format("%08.5f E", this.centerLongitude), TEXT_PADDING, (int)FONT_SIZE * 2 + TEXT_PADDING);
        }

        public void setPlanes(List<Drawable> planes) {
            this.planes = planes;
            this.invalidate();
        }

        public void setCenter(double latitude, double longitude) {
            this.centerLatitude = latitude;
            this.centerLongitude = longitude;
        }

        public double getCenterLatitude() {
            return this.centerLatitude;
        }

        public double getCenterLongitude() {
            return this.centerLongitude;
        }

        public double getPixelsPerDegree() {
            return 600;
        }
    }

    private interface Drawable {
        void drawOn(Graphics graphicsContext, AircraftMap map);
    }

    private class GeoPoint {
        private double latitude;
        private double longitude;

        public GeoPoint(double latitude, double longitude) {
            this.latitude = latitude;
            this.longitude = longitude;
        }

        public int getX(AircraftMap map) {
            double degreesFromCenter = map.getCenterLongitude() - this.longitude;
            double pixelsFromCenter = degreesFromCenter * map.getPixelsPerDegree();
            double centerPixels = map.getSize().getWidth() / 2;
            int xPosition = (int)(centerPixels - pixelsFromCenter);

            System.out.println("Degrees from center: " + degreesFromCenter + "; Pixels from center: " + pixelsFromCenter);
            return xPosition;
        }

        public int getY(AircraftMap map) {
            double degreesFromCenter = map.getCenterLatitude() - this.latitude;
            double pixelsFromCenter = degreesFromCenter * map.getPixelsPerDegree();
            double centerPixels = map.getSize().getHeight() / 2;
            int yPosition = (int)(centerPixels + pixelsFromCenter);
            return yPosition;
        }
    }

    private class Plane extends GeoPoint implements Drawable {
        private String name;
        private final int DOT_DIMENSION = 4;
        private final int TEXT_OFFSET_X = 10;
        private final int TEXT_OFFSET_Y = 15;

        public Plane(String name, double latitude, double longitude) {
            super(latitude, longitude);
            this.name = name;
        }

        public void drawOn(Graphics g, AircraftMap map) {
            g.setColor(new Color(200, 90, 0));
            g.fillRect(this.getX(map) - (DOT_DIMENSION / 2), this.getY(map) - (DOT_DIMENSION / 2), DOT_DIMENSION, DOT_DIMENSION);
            g.drawString(this.name, this.getX(map) + TEXT_OFFSET_X, this.getY(map) + TEXT_OFFSET_Y);
        }
    }
}