From b91190e2723efd3ca1a567b1ff5cfa24a438e9d1 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Sat, 19 Nov 2016 13:33:59 -0500 Subject: Fix panning/zooming --- .../planes/gui/aircraftmap/AircraftMap.java | 70 +++++++++++++++------- 1 file changed, 49 insertions(+), 21 deletions(-) diff --git a/src/com/benburwell/planes/gui/aircraftmap/AircraftMap.java b/src/com/benburwell/planes/gui/aircraftmap/AircraftMap.java index 127f9c8..af5b9a0 100644 --- a/src/com/benburwell/planes/gui/aircraftmap/AircraftMap.java +++ b/src/com/benburwell/planes/gui/aircraftmap/AircraftMap.java @@ -12,22 +12,30 @@ import java.util.List; */ public class AircraftMap extends JPanel { // geographic constants - private final double MAX_LATITUDE = 90.0; - private final double MIN_LATITUDE = -90.0; - private final double MAX_LONGITUDE = 180.0; - private final double MIN_LONGITUDE = -180.0; - private final double NAUTICAL_MILES_PER_DEGREE_LATITUDE = 60.0; - - private final float FONT_SIZE = 12; - private final int TEXT_PADDING = 5; - private final int ZOOM_INTERVAL = 10; - private final double PAN_INTERVAL = 1.0 / 60.0; - private final int RING_SPACING = 10; + 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 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); @@ -35,6 +43,11 @@ public class AircraftMap extends JPanel { 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); @@ -43,6 +56,10 @@ public class AircraftMap extends JPanel { this.drawRange(g); } + /** + * + * @param g + */ public void drawPositionAndScale(Graphics g) { Font currentFont = g.getFont(); Font newFont = currentFont.deriveFont(FONT_SIZE); @@ -50,19 +67,21 @@ public class AircraftMap extends JPanel { 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; + 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() * 10; + diameter += this.getPixelsPerNauticalMile() * RING_SPACING * 2; ringNumber++; } g.drawLine(centerX, 0, centerX, this.getHeight()); @@ -92,8 +111,13 @@ public class AircraftMap extends JPanel { } public double getPixelsPerDegreeLongitude() { - double milesPerDegree = Math.abs(Math.cos(this.centerLatitude) * NAUTICAL_MILES_PER_DEGREE_LATITUDE); - return this.pixelsPerNauticalMile * milesPerDegree; + 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() { @@ -101,42 +125,46 @@ public class AircraftMap extends JPanel { } public void zoomIn() { - this.pixelsPerNauticalMile += ZOOM_INTERVAL; + this.pixelsPerNauticalMile += ZOOM_INTERVAL_MILES; this.invalidate(); this.validate(); this.repaint(); } public void zoomOut() { - this.pixelsPerNauticalMile -= ZOOM_INTERVAL; + this.pixelsPerNauticalMile -= ZOOM_INTERVAL_MILES; this.invalidate(); this.validate(); this.repaint(); } public void moveEast() { - this.centerLongitude = Math.min(this.centerLongitude + PAN_INTERVAL, MAX_LONGITUDE); + 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() { - this.centerLongitude = Math.max(this.centerLongitude - PAN_INTERVAL, MIN_LONGITUDE); + 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() { - this.centerLatitude = Math.min(this.centerLatitude + PAN_INTERVAL, MAX_LATITUDE); + 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() { - this.centerLatitude = Math.max(this.centerLatitude - PAN_INTERVAL, MIN_LATITUDE); + 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(); -- cgit v1.2.3