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 | |
parent | 3993bf0883ec229cac90e436d6e84cd38307c2c3 (diff) |
Implement moving the map
-rw-r--r-- | src/com/benburwell/planes/gui/AircraftMapComponent.java | 99 | ||||
-rw-r--r-- | src/com/benburwell/planes/gui/GraphicsTheme.java | 27 |
2 files changed, 118 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); } } diff --git a/src/com/benburwell/planes/gui/GraphicsTheme.java b/src/com/benburwell/planes/gui/GraphicsTheme.java new file mode 100644 index 0000000..cece138 --- /dev/null +++ b/src/com/benburwell/planes/gui/GraphicsTheme.java @@ -0,0 +1,27 @@ +package com.benburwell.planes.gui; + +import java.awt.*; + +/** + * Created by ben on 11/18/16. + */ +public class GraphicsTheme { + public static class Colors { + public static final Color BASE_0 = new Color(12, 16, 20); + public static final Color BASE_1 = new Color(17, 21, 28); + public static final Color BASE_2 = new Color(9, 31, 46); + public static final Color BASE_3 = new Color(10, 55, 73); + public static final Color BASE_4 = new Color(36, 83, 97); + public static final Color BASE_5 = new Color(89, 156, 171); + public static final Color BASE_6 = new Color(153, 209, 206); + public static final Color BASE_7 = new Color(211, 235, 233); + public static final Color RED = new Color(194, 49, 39); + public static final Color ORANGE = new Color(210, 105, 55); + public static final Color YELLOW = new Color(237, 180, 67); + public static final Color MAGENTA = new Color(136, 140, 166); + public static final Color VIOLET = new Color(78, 81, 102); + public static final Color BLUE = new Color(25, 84, 102); + public static final Color CYAN = new Color(51, 133, 158); + public static final Color GREEN = new Color(42, 168, 137); + } +} |