diff options
Diffstat (limited to 'src/main/java/com/benburwell/planes/gui/aircraftmap')
-rw-r--r-- | src/main/java/com/benburwell/planes/gui/aircraftmap/AircraftMap.java | 72 | ||||
-rw-r--r-- | src/main/java/com/benburwell/planes/gui/aircraftmap/AircraftMapComponent.java | 31 | ||||
-rw-r--r-- | src/main/java/com/benburwell/planes/gui/aircraftmap/GeoPoint.java | 6 | ||||
-rw-r--r-- | src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/AirportSymbol.java | 48 | ||||
-rw-r--r-- | src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/NDBSymbol.java | 42 | ||||
-rw-r--r-- | src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/PlaneSymbol.java (renamed from src/main/java/com/benburwell/planes/gui/aircraftmap/Plane.java) | 19 | ||||
-rw-r--r-- | src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/VORDMESymbol.java | 29 | ||||
-rw-r--r-- | src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/VORSymbol.java | 49 | ||||
-rw-r--r-- | src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/VORTACSymbol.java | 32 |
9 files changed, 299 insertions, 29 deletions
diff --git a/src/main/java/com/benburwell/planes/gui/aircraftmap/AircraftMap.java b/src/main/java/com/benburwell/planes/gui/aircraftmap/AircraftMap.java index fad1082..64d86e9 100644 --- a/src/main/java/com/benburwell/planes/gui/aircraftmap/AircraftMap.java +++ b/src/main/java/com/benburwell/planes/gui/aircraftmap/AircraftMap.java @@ -1,6 +1,10 @@ package com.benburwell.planes.gui.aircraftmap; +import com.benburwell.planes.data.Airport; +import com.benburwell.planes.data.NavigationAid; +import com.benburwell.planes.data.Position; import com.benburwell.planes.gui.GraphicsTheme; +import com.benburwell.planes.gui.aircraftmap.symbols.*; import javax.swing.*; import java.awt.*; @@ -30,9 +34,13 @@ public class AircraftMap extends JPanel { // instance fields private List<Drawable> planes = new ArrayList<>(); + private List<Drawable> navaids = new ArrayList<>(); + private List<Drawable> airports = new ArrayList<>(); private double centerLatitude; private double centerLongitude; private int pixelsPerNauticalMile = 10; + private boolean showNavAids = true; + private boolean showAirports = true; /** * Construct a map @@ -44,6 +52,45 @@ public class AircraftMap extends JPanel { this.setCenter(0, 0); } + public void addNavAids(List<NavigationAid> aids) { + for (NavigationAid aid : aids) { + if (aid.getType().equals("VOR")) { + Position pos = new Position(); + pos.setLatitude(aid.getLatitude()); + pos.setLongitude(aid.getLongitude()); + pos.setAltitude(aid.getElevation()); + this.navaids.add(new VORSymbol(aid.getIdent(), pos, aid.getFrequency())); + } + if (aid.getType().equals("VOR-DME")) { + Position pos = new Position(); + pos.setLatitude(aid.getLatitude()); + pos.setLongitude(aid.getLongitude()); + pos.setAltitude(aid.getElevation()); + this.navaids.add(new VORDMESymbol(aid.getIdent(), pos, aid.getFrequency())); + } + if (aid.getType().equals("VORTAC")) { + Position pos = new Position(); + pos.setLatitude(aid.getLatitude()); + pos.setLongitude(aid.getLongitude()); + pos.setAltitude(aid.getElevation()); + this.navaids.add(new VORTACSymbol(aid.getIdent(), pos, aid.getFrequency())); + } + if (aid.getType().equals("NDB")) { + Position pos = new Position(); + pos.setLatitude(aid.getLatitude()); + pos.setLongitude(aid.getLongitude()); + pos.setAltitude(aid.getElevation()); + this.navaids.add(new NDBSymbol(aid.getIdent(), pos, aid.getFrequency())); + } + } + } + + public void addAirports(List<Airport> airports) { + for (Airport airport : airports) { + this.airports.add(new AirportSymbol(airport)); + } + } + /** * Paint the ViewComponent on a Graphics instance * @@ -52,9 +99,28 @@ public class AircraftMap extends JPanel { @Override public void paintComponent(Graphics g) { super.paintComponent(g); - this.drawPositionAndScale(g); - this.drawRange(g); - this.planes.forEach(item -> item.drawOn(g, this)); + Graphics2D g2d = (Graphics2D)g.create(); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + this.drawPositionAndScale(g2d); + this.drawRange(g2d); + if (this.showNavAids) { + this.navaids.forEach(aid -> aid.drawOn(g2d, this)); + } + if (this.showAirports) { + this.airports.forEach(airport -> airport.drawOn(g2d, this)); + } + this.planes.forEach(item -> item.drawOn(g2d, this)); + g2d.dispose(); + } + + public void toggleNavAids() { + this.showNavAids = !this.showNavAids; + this.redraw(); + } + + public void toggleAirports() { + this.showAirports = !this.showAirports; + this.redraw(); } public void drawPositionAndScale(Graphics g) { diff --git a/src/main/java/com/benburwell/planes/gui/aircraftmap/AircraftMapComponent.java b/src/main/java/com/benburwell/planes/gui/aircraftmap/AircraftMapComponent.java index f45071b..24dfc94 100644 --- a/src/main/java/com/benburwell/planes/gui/aircraftmap/AircraftMapComponent.java +++ b/src/main/java/com/benburwell/planes/gui/aircraftmap/AircraftMapComponent.java @@ -1,18 +1,14 @@ package com.benburwell.planes.gui.aircraftmap; -import com.benburwell.planes.data.AircraftStore; -import com.benburwell.planes.data.AircraftStoreListener; -import com.benburwell.planes.data.NavigationAidStore; -import com.benburwell.planes.data.Position; +import com.benburwell.planes.data.*; import com.benburwell.planes.gui.ViewComponent; +import com.benburwell.planes.gui.aircraftmap.symbols.PlaneSymbol; import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; -import java.io.IOException; import java.util.List; import java.util.ArrayList; -import java.util.Collections; /** * Created by ben on 11/18/16. @@ -20,25 +16,20 @@ import java.util.Collections; public class AircraftMapComponent implements ViewComponent { private AircraftStore store; private NavigationAidStore navaids; + private AirportStore airportStore; private AircraftMap mapPanel; private String focusedAircraftIdentifier = null; - public AircraftMapComponent(AircraftStore store) { + public AircraftMapComponent(AircraftStore store, NavigationAidStore navaids, AirportStore airportStore) { this.store = store; - this.navaids = new NavigationAidStore(); + this.navaids = navaids; + this.airportStore = airportStore; this.setupMap(); this.bindKeys(); this.subscribeToChanges(); - this.readNavAids(); - } - - public void readNavAids() { - try { - this.navaids.readFromFile("/home/ben/.airdata/navaids.csv"); - } catch (IOException e) { - System.out.println("Could not read navaid file: " + e.getMessage()); - } + this.mapPanel.addNavAids(this.navaids.getNavigationAids()); + this.mapPanel.addAirports(this.airportStore.getAirports()); } public void focusNextAircraft() { @@ -82,6 +73,10 @@ public class AircraftMapComponent implements ViewComponent { } else if (e.getKeyCode() == KeyEvent.VK_TAB && e.getID() == KeyEvent.KEY_PRESSED) { this.focusNextAircraft(); this.centerMapOnPlane(this.focusedAircraftIdentifier); + } else if (e.getKeyCode() == KeyEvent.VK_N && e.getID() == KeyEvent.KEY_PRESSED) { + this.mapPanel.toggleNavAids(); + } else if (e.getKeyCode() == KeyEvent.VK_A && e.getID() == KeyEvent.KEY_PRESSED) { + this.mapPanel.toggleAirports(); } return false; }); @@ -99,7 +94,7 @@ public class AircraftMapComponent implements ViewComponent { @Override public void aircraftStoreChanged() { List<Drawable> planes = new ArrayList<>(); - store.getAircraft().values().forEach(aircraft -> planes.add(new Plane(aircraft))); + store.getAircraft().values().forEach(aircraft -> planes.add(new PlaneSymbol(aircraft))); mapPanel.setPlanes(planes); mapPanel.validate(); mapPanel.repaint(); diff --git a/src/main/java/com/benburwell/planes/gui/aircraftmap/GeoPoint.java b/src/main/java/com/benburwell/planes/gui/aircraftmap/GeoPoint.java index d3eda40..80c6204 100644 --- a/src/main/java/com/benburwell/planes/gui/aircraftmap/GeoPoint.java +++ b/src/main/java/com/benburwell/planes/gui/aircraftmap/GeoPoint.java @@ -30,6 +30,12 @@ public class GeoPoint { return yPosition; } + public boolean shouldDrawOn(AircraftMap map) { + int x = this.getX(map); + int y = this.getY(map); + return (x >= 0 && x <= map.getWidth() && y >= 0 && y <= map.getHeight()); + } + public double getAltitude() { return this.altitude; } diff --git a/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/AirportSymbol.java b/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/AirportSymbol.java new file mode 100644 index 0000000..5286f6f --- /dev/null +++ b/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/AirportSymbol.java @@ -0,0 +1,48 @@ +package com.benburwell.planes.gui.aircraftmap.symbols; + +import com.benburwell.planes.data.Airport; +import com.benburwell.planes.gui.GraphicsTheme; +import com.benburwell.planes.gui.aircraftmap.AircraftMap; +import com.benburwell.planes.gui.aircraftmap.Drawable; +import com.benburwell.planes.gui.aircraftmap.GeoPoint; + +import java.awt.*; + +/** + * Created by ben on 11/19/16. + */ +public class AirportSymbol extends GeoPoint implements Drawable { + private String name; + private String iataCode; + private String localCode; + + public AirportSymbol(Airport airport) { + super(airport.getLatitude(), airport.getLongitude(), airport.getElevation()); + this.name = airport.getName(); + this.iataCode = airport.getIataCode(); + this.localCode = airport.getLocalCode(); + } + + public String getDisplayName() { + if (this.iataCode != null && !this.iataCode.isEmpty()) { + return this.iataCode; + } + if (this.localCode != null && !this.localCode.isEmpty()) { + return this.localCode; + } + return this.name; + } + + @Override + public void drawOn(Graphics g, AircraftMap map) { + if (this.shouldDrawOn(map)) { + int x = this.getX(map); + int y = this.getY(map); + + g.setColor(GraphicsTheme.Colors.MAGENTA); + + g.drawRect(x - 2, y - 2, 4, 4); + g.drawString(this.getDisplayName(), x + 6, y); + } + } +} diff --git a/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/NDBSymbol.java b/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/NDBSymbol.java new file mode 100644 index 0000000..08793ec --- /dev/null +++ b/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/NDBSymbol.java @@ -0,0 +1,42 @@ +package com.benburwell.planes.gui.aircraftmap.symbols; + +import com.benburwell.planes.data.Position; +import com.benburwell.planes.gui.GraphicsTheme; +import com.benburwell.planes.gui.aircraftmap.AircraftMap; +import com.benburwell.planes.gui.aircraftmap.Drawable; +import com.benburwell.planes.gui.aircraftmap.GeoPoint; + +import java.awt.*; + +/** + * Created by ben on 11/19/16. + */ +public class NDBSymbol extends GeoPoint implements Drawable { + public static final int INNER_RADIUS = 3; + public static final int OUTER_RADIUS = 9; + public static final int TEXT_OFFSET = 4; + + private String name; + private int frequency; + + public NDBSymbol(String name, Position pos, int frequency) { + super(pos.getLatitude(), pos.getLongitude(), pos.getAltitude()); + this.name = name; + this.frequency = frequency; + } + + @Override + public void drawOn(Graphics g, AircraftMap map) { + if (this.shouldDrawOn(map)) { + g.setColor(GraphicsTheme.Colors.VIOLET); + + int x = this.getX(map); + int y = this.getY(map); + g.fillOval(x - INNER_RADIUS, y - INNER_RADIUS, INNER_RADIUS * 2, INNER_RADIUS * 2); + g.drawOval(x - OUTER_RADIUS, y - OUTER_RADIUS, OUTER_RADIUS * 2, OUTER_RADIUS * 2); + + g.drawString(this.name, x + OUTER_RADIUS + TEXT_OFFSET, y); + g.drawString("" + this.frequency, x + OUTER_RADIUS + TEXT_OFFSET, y + g.getFontMetrics().getHeight()); + } + } +} diff --git a/src/main/java/com/benburwell/planes/gui/aircraftmap/Plane.java b/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/PlaneSymbol.java index 2f98bab..4e8ebc0 100644 --- a/src/main/java/com/benburwell/planes/gui/aircraftmap/Plane.java +++ b/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/PlaneSymbol.java @@ -1,8 +1,11 @@ -package com.benburwell.planes.gui.aircraftmap; +package com.benburwell.planes.gui.aircraftmap.symbols; import com.benburwell.planes.data.Aircraft; import com.benburwell.planes.data.Position; import com.benburwell.planes.gui.GraphicsTheme; +import com.benburwell.planes.gui.aircraftmap.AircraftMap; +import com.benburwell.planes.gui.aircraftmap.Drawable; +import com.benburwell.planes.gui.aircraftmap.GeoPoint; import java.awt.*; import java.awt.geom.AffineTransform; @@ -10,7 +13,7 @@ import java.awt.geom.AffineTransform; /** * Created by ben on 11/19/16. */ -public class Plane extends GeoPoint implements Drawable { +public class PlaneSymbol extends GeoPoint implements Drawable { public final int TRIANGLE_HEIGHT = 6; public final int TRIANGLE_WIDTH = 4; public final int TEXT_OFFSET_X = 10; @@ -22,15 +25,15 @@ public class Plane extends GeoPoint implements Drawable { private double speed; private double verticalRate; - public Plane(Aircraft ac) { + public PlaneSymbol(Aircraft ac) { this(ac.getCallsign(), ac.getCurrentPosition(), ac.getTrack(), ac.getGroundSpeed(), ac.getVerticalRate()); } - public Plane(String name, Position position, double heading, double speed, double verticalRate) { + public PlaneSymbol(String name, Position position, double heading, double speed, double verticalRate) { this(name, position.getLatitude(), position.getLongitude(), position.getAltitude(), heading, speed, verticalRate); } - public Plane(String name, double latitude, double longitude, double altitude, double heading, double speed, double verticalRate) { + public PlaneSymbol(String name, double latitude, double longitude, double altitude, double heading, double speed, double verticalRate) { super(latitude, longitude, altitude); this.name = name; this.heading = heading; @@ -104,10 +107,10 @@ public class Plane extends GeoPoint implements Drawable { } public void drawOn(Graphics g, AircraftMap map) { - int x = this.getX(map); - int y = this.getY(map); + if (this.shouldDrawOn(map)) { + int x = this.getX(map); + int y = this.getY(map); - if (x >= 0 && x <= map.getSize().getWidth() && y >= 0 && y <= map.getSize().getHeight()) { // draw the plane dot Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(this.getPlaneColor()); diff --git a/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/VORDMESymbol.java b/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/VORDMESymbol.java new file mode 100644 index 0000000..0b67989 --- /dev/null +++ b/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/VORDMESymbol.java @@ -0,0 +1,29 @@ +package com.benburwell.planes.gui.aircraftmap.symbols; + +import com.benburwell.planes.data.Position; +import com.benburwell.planes.gui.GraphicsTheme; +import com.benburwell.planes.gui.aircraftmap.AircraftMap; +import com.benburwell.planes.gui.aircraftmap.Drawable; +import com.benburwell.planes.gui.aircraftmap.symbols.VORSymbol; + +import java.awt.Graphics; + +/** + * Created by ben on 11/19/16. + */ +public class VORDMESymbol extends VORSymbol implements Drawable { + public VORDMESymbol(String name, Position pos, int frequency) { + super(name, pos, frequency); + } + + @Override + public void drawOn(Graphics graphicsContext, AircraftMap map) { + super.drawOn(graphicsContext, map); + if (this.shouldDrawOn(map)) { + int x = this.getX(map); + int y = this.getY(map); + graphicsContext.setColor(GraphicsTheme.Colors.VIOLET); + graphicsContext.drawRect(x - VORSymbol.RADIUS, y - VORSymbol.HEIGHT, VORSymbol.RADIUS * 2, VORSymbol.HEIGHT * 2); + } + } +} diff --git a/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/VORSymbol.java b/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/VORSymbol.java new file mode 100644 index 0000000..e854c2b --- /dev/null +++ b/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/VORSymbol.java @@ -0,0 +1,49 @@ +package com.benburwell.planes.gui.aircraftmap.symbols; + +import com.benburwell.planes.data.Position; +import com.benburwell.planes.gui.GraphicsTheme; +import com.benburwell.planes.gui.aircraftmap.AircraftMap; +import com.benburwell.planes.gui.aircraftmap.Drawable; +import com.benburwell.planes.gui.aircraftmap.GeoPoint; + +import java.awt.*; + +/** + * Created by ben on 11/19/16. + */ +public class VORSymbol extends GeoPoint implements Drawable { + public static final int RADIUS = 11; + public static final int HEIGHT = (int) (Math.sin(Math.toRadians(60)) * RADIUS); + public static final int X_OFFSET = (int) (Math.cos(Math.toRadians(60)) * RADIUS); + public static final int DOT_RADIUS = 3; + public static final int TEXT_OFFSET = 4; + + private String name; + private int frequency; + + public VORSymbol(String name, Position pos, int frequency) { + super(pos.getLatitude(), pos.getLongitude(), pos.getAltitude()); + this.name = name; + this.frequency = frequency; + } + + @Override + public void drawOn(Graphics graphicsContext, AircraftMap map) { + if (this.shouldDrawOn(map)) { + int x = this.getX(map); + int y = this.getY(map); + graphicsContext.setColor(GraphicsTheme.Colors.VIOLET); + + // center dot + graphicsContext.fillOval(x - DOT_RADIUS, y - DOT_RADIUS, DOT_RADIUS * 2, DOT_RADIUS * 2); + + // hexagon + int[] xs = { x - RADIUS, x - X_OFFSET, x + X_OFFSET, x + RADIUS, x + X_OFFSET, x - X_OFFSET, x - RADIUS }; + int[] ys = { y, y - HEIGHT, y - HEIGHT, y, y + HEIGHT, y + HEIGHT, y }; + graphicsContext.drawPolygon(xs, ys, 7); + + graphicsContext.drawString(this.name, x + RADIUS + TEXT_OFFSET, y); + graphicsContext.drawString(String.format("%.3f", this.frequency / 1000.0), x + RADIUS + TEXT_OFFSET, y + graphicsContext.getFontMetrics().getHeight()); + } + } +} diff --git a/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/VORTACSymbol.java b/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/VORTACSymbol.java new file mode 100644 index 0000000..3b0043f --- /dev/null +++ b/src/main/java/com/benburwell/planes/gui/aircraftmap/symbols/VORTACSymbol.java @@ -0,0 +1,32 @@ +package com.benburwell.planes.gui.aircraftmap.symbols; + +import com.benburwell.planes.data.Position; +import com.benburwell.planes.gui.GraphicsTheme; +import com.benburwell.planes.gui.aircraftmap.AircraftMap; +import com.benburwell.planes.gui.aircraftmap.Drawable; +import com.benburwell.planes.gui.aircraftmap.symbols.VORSymbol; + +import java.awt.*; + +/** + * Created by ben on 11/19/16. + */ +public class VORTACSymbol extends VORSymbol implements Drawable { + public VORTACSymbol(String name, Position pos, int frequency) { + super(name, pos, frequency); + } + + @Override + public void drawOn(Graphics graphicsContext, AircraftMap map) { + super.drawOn(graphicsContext, map); + if (this.shouldDrawOn(map)) { + int x = this.getX(map); + int y = this.getY(map); + graphicsContext.setColor(GraphicsTheme.Colors.VIOLET); + + int[] xs = { x - VORSymbol.X_OFFSET, x + VORSymbol.X_OFFSET, x + VORSymbol.X_OFFSET, x - VORSymbol.X_OFFSET, x - VORSymbol.X_OFFSET }; + int[] ys = { y + VORSymbol.RADIUS, y + VORSymbol.RADIUS, y + VORSymbol.RADIUS * 2, y + VORSymbol.RADIUS * 2, y + VORSymbol.RADIUS }; + graphicsContext.fillPolygon(xs, ys, 5); + } + } +} |