summaryrefslogtreecommitdiff
path: root/src/com/benburwell/planes/gui/aircraftmap/AircraftMap.java
blob: af5b9a0ce2b4a8aa81348c0f41603472f819b3d6 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.benburwell.planes.gui.aircraftmap;

import com.benburwell.planes.gui.GraphicsTheme;

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

/**
 * Created by ben on 11/19/16.
 */
public class AircraftMap extends JPanel {
    // geographic constants
    public final double MAX_LATITUDE = 90.0;
    public final double MIN_LATITUDE = -90.0;
    public final double MAX_LONGITUDE = 180.0;
    public final double MIN_LONGITUDE = -180.0;
    public final double NAUTICAL_MILES_PER_DEGREE_LATITUDE = 60.0;

    // drawing constants
    public final float FONT_SIZE = 12;
    public final int TEXT_PADDING = 5;

    // map manipulation constants
    public final int ZOOM_INTERVAL_MILES = 10;
    public final double PAN_INTERVAL_MILES = 1.0;
    public final int RING_SPACING = 10;

    // instance fields
    private List<Drawable> planes = new ArrayList<>();
    private double centerLatitude;
    private double centerLongitude;
    private int pixelsPerNauticalMile = 10;

    /**
     * Construct a map
     */
    public AircraftMap() {
        super();
        this.setBackground(GraphicsTheme.Colors.BASE_1);
        this.setBorder(BorderFactory.createEmptyBorder());
        this.setCenter(0, 0);
    }

    /**
     * Paint the ViewComponent on a Graphics instance
     *
     * @param g the graphics context to paint on
     */
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.planes.forEach(item -> item.drawOn(g, this));
        this.drawPositionAndScale(g);
        this.drawRange(g);
    }

    /**
     *
     * @param g
     */
    public void drawPositionAndScale(Graphics g) {
        Font currentFont = g.getFont();
        Font newFont = currentFont.deriveFont(FONT_SIZE);
        g.setFont(newFont);
        g.setColor(GraphicsTheme.Colors.BLUE);
        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);
        g.drawString("1 nm", TEXT_PADDING, (int) FONT_SIZE * 3 + TEXT_PADDING);
        g.drawLine(TEXT_PADDING, (int) FONT_SIZE * 3 + TEXT_PADDING, (int) (TEXT_PADDING + this.getPixelsPerNauticalMile()), (int) FONT_SIZE * 3 + TEXT_PADDING);
    }

    public void drawRange(Graphics g) {
        int centerX = this.getWidth() / 2;
        int centerY = this.getHeight() / 2;
        g.setColor(GraphicsTheme.Colors.BASE_3);
        int diameter = (int) this.getPixelsPerNauticalMile() * RING_SPACING * 2;
        int ringNumber = 1;
        while (diameter < this.getWidth() || diameter < this.getHeight()) {
            g.drawOval(centerX - (diameter / 2), centerY - (diameter / 2), diameter, diameter);
            g.drawString(String.format("%d", ringNumber * RING_SPACING), centerX + (diameter / 2) + TEXT_PADDING, (int) (centerY + FONT_SIZE + TEXT_PADDING));
            g.drawString(String.format("%d", ringNumber * RING_SPACING), centerX + TEXT_PADDING, centerY - (int) ((diameter / 2) + FONT_SIZE));
            diameter += this.getPixelsPerNauticalMile() * RING_SPACING * 2;
            ringNumber++;
        }
        g.drawLine(centerX, 0, centerX, this.getHeight());
        g.drawLine(0, centerY, this.getWidth(), centerY);
    }

    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 getPixelsPerDegreeLatitude() {
        return this.pixelsPerNauticalMile * NAUTICAL_MILES_PER_DEGREE_LATITUDE;
    }

    public double getPixelsPerDegreeLongitude() {
        return this.pixelsPerNauticalMile * this.getNauticalMilesPerDegreeLongitude();
    }

    public double getNauticalMilesPerDegreeLongitude() {
        double milesPerDegree = Math.abs(Math.cos(Math.toRadians(this.centerLatitude)) * NAUTICAL_MILES_PER_DEGREE_LATITUDE);
        System.out.println("Miles per degree at " + this.centerLatitude + " N: " + milesPerDegree);
        return milesPerDegree;
    }

    public double getPixelsPerNauticalMile() {
        return this.pixelsPerNauticalMile;
    }

    public void zoomIn() {
        this.pixelsPerNauticalMile += ZOOM_INTERVAL_MILES;
        this.invalidate();
        this.validate();
        this.repaint();
    }

    public void zoomOut() {
        this.pixelsPerNauticalMile -= ZOOM_INTERVAL_MILES;
        this.invalidate();
        this.validate();
        this.repaint();
    }

    public void moveEast() {
        double degreesToMove = PAN_INTERVAL_MILES / this.getNauticalMilesPerDegreeLongitude();
        this.centerLongitude = Math.min(this.centerLongitude + degreesToMove, MAX_LONGITUDE);
        this.invalidate();
        this.validate();
        this.repaint();
    }

    public void moveWest() {
        double degreesToMove = PAN_INTERVAL_MILES / this.getNauticalMilesPerDegreeLongitude();
        this.centerLongitude = Math.max(this.centerLongitude - degreesToMove, MIN_LONGITUDE);
        this.invalidate();
        this.validate();
        this.repaint();
    }

    public void moveNorth() {
        double degreesToMove = PAN_INTERVAL_MILES / NAUTICAL_MILES_PER_DEGREE_LATITUDE;
        this.centerLatitude = Math.min(this.centerLatitude + degreesToMove, MAX_LATITUDE);
        this.invalidate();
        this.validate();
        this.repaint();
    }

    public void moveSouth() {
        double degreesToMove = PAN_INTERVAL_MILES / NAUTICAL_MILES_PER_DEGREE_LATITUDE;
        this.centerLatitude = Math.max(this.centerLatitude - degreesToMove, MIN_LATITUDE);
        this.invalidate();
        this.validate();
        this.repaint();
    }
}