diff options
author | Ben Burwell <ben.burwell@trifecta.com> | 2016-11-18 23:54:49 -0500 |
---|---|---|
committer | Ben Burwell <ben.burwell@trifecta.com> | 2016-11-18 23:55:08 -0500 |
commit | 118254a9bb006c0001fc467f04521a9e2c11753f (patch) | |
tree | 0bd0781a0e1aaba6ae685dc6f72865750af30e03 /src/com/benburwell/planes/gui/AircraftMapComponent.java | |
parent | 3993bf0883ec229cac90e436d6e84cd38307c2c3 (diff) |
Implement moving the map
Diffstat (limited to 'src/com/benburwell/planes/gui/AircraftMapComponent.java')
-rw-r--r-- | src/com/benburwell/planes/gui/AircraftMapComponent.java | 99 |
1 files changed, 91 insertions, 8 deletions
diff --git a/src/com/benburwell/planes/gui/AircraftMapComponent.java b/src/com/benburwell/planes/gui/AircraftMapComponent.java index 1992c25..d680b60 100644 --- a/src/com/benburwell/planes/gui/AircraftMapComponent.java +++ b/src/com/benburwell/planes/gui/AircraftMapComponent.java @@ -5,6 +5,7 @@ import com.benburwell.planes.data.AircraftStoreListener; import javax.swing.*; import java.awt.*; +import java.awt.event.KeyEvent; import java.util.List; import java.util.ArrayList; @@ -17,8 +18,36 @@ public class AircraftMapComponent implements ViewComponent { public AircraftMapComponent(AircraftStore store) { this.store = store; + this.setupMap(); + this.bindKeys(); + this.subscribeToChanges(); + } + + private void setupMap() { this.mapPanel = new AircraftMap(); this.mapPanel.setCenter(40.6188942, -75.4947205); + } + + private void bindKeys() { + KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(e -> { + if (e.getKeyCode() == KeyEvent.VK_EQUALS && e.isShiftDown() && e.getID() == KeyEvent.KEY_PRESSED) { + this.mapPanel.zoomIn(); + } else if (e.getKeyCode() == KeyEvent.VK_MINUS && e.getID() == KeyEvent.KEY_PRESSED) { + this.mapPanel.zoomOut(); + } else if (e.getKeyCode() == KeyEvent.VK_L && e.getID() == KeyEvent.KEY_PRESSED) { + this.mapPanel.moveEast(); + } else if (e.getKeyCode() == KeyEvent.VK_H && e.getID() == KeyEvent.KEY_PRESSED) { + this.mapPanel.moveWest(); + } else if (e.getKeyCode() == KeyEvent.VK_J && e.getID() == KeyEvent.KEY_PRESSED) { + this.mapPanel.moveSouth(); + } else if (e.getKeyCode() == KeyEvent.VK_K && e.getID() == KeyEvent.KEY_PRESSED) { + this.mapPanel.moveNorth(); + } + return false; + }); + } + + private void subscribeToChanges() { this.store.subscribe(new AircraftStoreListener() { @Override public void aircraftStoreChanged() { @@ -49,13 +78,20 @@ public class AircraftMapComponent implements ViewComponent { private class AircraftMap extends JPanel { private final float FONT_SIZE = 12; private final int TEXT_PADDING = 5; + private final int ZOOM_INTERVAL = 100; + private final double PAN_INTERVAL = 1.0 / 60.0; + 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 List<Drawable> planes = new ArrayList<>(); private double centerLatitude; private double centerLongitude; + private int pixelsPerDegree = 600; public AircraftMap() { super(); - this.setBackground(Color.black); + this.setBackground(GraphicsTheme.Colors.BASE_1); this.setBorder(BorderFactory.createEmptyBorder()); this.setCenter(0, 0); } @@ -71,7 +107,7 @@ public class AircraftMapComponent implements ViewComponent { Font currentFont = g.getFont(); Font newFont = currentFont.deriveFont(FONT_SIZE); g.setFont(newFont); - g.setColor(Color.gray); + 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); } @@ -95,7 +131,49 @@ public class AircraftMapComponent implements ViewComponent { } public double getPixelsPerDegree() { - return 600; + return this.pixelsPerDegree; + } + + public void zoomIn() { + this.pixelsPerDegree += ZOOM_INTERVAL; + this.invalidate(); + this.validate(); + this.repaint(); + } + + public void zoomOut() { + this.pixelsPerDegree -= ZOOM_INTERVAL; + this.invalidate(); + this.validate(); + this.repaint(); + } + + public void moveEast() { + this.centerLongitude = Math.min(this.centerLongitude + PAN_INTERVAL, MAX_LONGITUDE); + this.invalidate(); + this.validate(); + this.repaint(); + } + + public void moveWest() { + this.centerLongitude = Math.max(this.centerLongitude - PAN_INTERVAL, MIN_LONGITUDE); + this.invalidate(); + this.validate(); + this.repaint(); + } + + public void moveNorth() { + this.centerLatitude = Math.min(this.centerLatitude + PAN_INTERVAL, MAX_LATITUDE); + this.invalidate(); + this.validate(); + this.repaint(); + } + + public void moveSouth() { + this.centerLatitude = Math.max(this.centerLatitude - PAN_INTERVAL, MIN_LATITUDE); + this.invalidate(); + this.validate(); + this.repaint(); } } @@ -117,8 +195,6 @@ public class AircraftMapComponent implements ViewComponent { 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; } @@ -133,7 +209,7 @@ public class AircraftMapComponent implements ViewComponent { private class Plane extends GeoPoint implements Drawable { private String name; - private final int DOT_DIMENSION = 4; + private final int DOT_DIMENSION = 10; private final int TEXT_OFFSET_X = 10; private final int TEXT_OFFSET_Y = 15; @@ -143,8 +219,15 @@ public class AircraftMapComponent implements ViewComponent { } 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); + int x = this.getX(map); + int y = this.getY(map); + + // draw the plane dot + g.setColor(GraphicsTheme.Colors.ORANGE); + g.fillOval(x - (DOT_DIMENSION / 2), y - (DOT_DIMENSION / 2), DOT_DIMENSION, DOT_DIMENSION); + + // draw the name of the plane + g.setColor(GraphicsTheme.Colors.BLUE); g.drawString(this.name, this.getX(map) + TEXT_OFFSET_X, this.getY(map) + TEXT_OFFSET_Y); } } |