From 29051be12b3e2fce8aac6c9c7a8a365a52147c4f Mon Sep 17 00:00:00 2001 From: Holt59 Date: Sun, 25 Feb 2018 18:12:15 +0100 Subject: [PATCH] Create OverlayTracker as a prent of MarkerTracker to allow tracking of other objects. --- .../org/insa/graphics/ShortestPathPanel.java | 22 +++------ .../insa/graphics/drawing/BasicDrawing.java | 42 ++++++++++------- .../insa/graphics/drawing/MapViewDrawing.java | 45 ++++++++++++------- .../insa/graphics/drawing/MarkerTracker.java | 14 +----- .../insa/graphics/drawing/OverlayTracker.java | 17 +++++++ 5 files changed, 76 insertions(+), 64 deletions(-) create mode 100644 src/main/org/insa/graphics/drawing/OverlayTracker.java diff --git a/src/main/org/insa/graphics/ShortestPathPanel.java b/src/main/org/insa/graphics/ShortestPathPanel.java index 4ab4fec..699dcd3 100644 --- a/src/main/org/insa/graphics/ShortestPathPanel.java +++ b/src/main/org/insa/graphics/ShortestPathPanel.java @@ -8,9 +8,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import javax.swing.Box; import javax.swing.BoxLayout; @@ -25,10 +23,8 @@ import javax.swing.JRadioButton; import javax.swing.JSplitPane; import javax.swing.border.EmptyBorder; -import org.insa.algo.shortestpath.AStarAlgorithm; -import org.insa.algo.shortestpath.BellmanFordAlgorithm; -import org.insa.algo.shortestpath.DijkstraAlgorithm; import org.insa.algo.shortestpath.ShortestPathAlgorithm; +import org.insa.algo.shortestpath.ShortestPathAlgorithmFactory; import org.insa.algo.shortestpath.ShortestPathData.Mode; import org.insa.graph.Graph; import org.insa.graph.Node; @@ -100,10 +96,6 @@ public class ShortestPathPanel extends JPanel { }; - // Map between algorithm names and class, see end of this class for - // initialization. - private static Map> SHORTEST_PATH_ALGORITHMS = new HashMap<>(); - // Input panels for node. private NodesInputPanel nodesInputPanel; @@ -132,7 +124,8 @@ public class ShortestPathPanel extends JPanel { add(Box.createVerticalStrut(8)); // Add algorithm selection - JComboBox algoSelect = new JComboBox<>(SHORTEST_PATH_ALGORITHMS.keySet().toArray(new String[0])); + JComboBox algoSelect = new JComboBox<>( + ShortestPathAlgorithmFactory.getAlgorithmNames().toArray(new String[0])); algoSelect.setBackground(Color.WHITE); algoSelect.setAlignmentX(Component.LEFT_ALIGNMENT); add(algoSelect); @@ -180,7 +173,8 @@ public class ShortestPathPanel extends JPanel { Mode mode = lengthModeButton.isSelected() ? Mode.LENGTH : Mode.TIME; for (ActionListener lis: startActionListeners) { - lis.actionPerformed(new StartActionEvent(SHORTEST_PATH_ALGORITHMS.get(algoSelect.getSelectedItem()), + lis.actionPerformed(new StartActionEvent( + ShortestPathAlgorithmFactory.getAlgorithmClass((String) algoSelect.getSelectedItem()), origin, destination, mode)); } } @@ -240,12 +234,6 @@ public class ShortestPathPanel extends JPanel { this.startActionListeners.add(listener); } - static { - SHORTEST_PATH_ALGORITHMS.put("Bellman-Ford", BellmanFordAlgorithm.class); - SHORTEST_PATH_ALGORITHMS.put("Dijkstra", DijkstraAlgorithm.class); - SHORTEST_PATH_ALGORITHMS.put("A*", AStarAlgorithm.class); - } - public static void main(String[] args) throws IOException { String nomcarte = "../BE_Graphe_Maps/morbihan3.mapgr"; diff --git a/src/main/org/insa/graphics/drawing/BasicDrawing.java b/src/main/org/insa/graphics/drawing/BasicDrawing.java index 7488789..4d52b62 100644 --- a/src/main/org/insa/graphics/drawing/BasicDrawing.java +++ b/src/main/org/insa/graphics/drawing/BasicDrawing.java @@ -38,10 +38,7 @@ public class BasicDrawing extends JPanel implements Drawing { */ private static final long serialVersionUID = 96779785877771827L; - public class BasicMarkerTracker implements MarkerTracker { - - // Point of the marker. - private Point point; + public class BasicOverlayTracker implements OverlayTracker { // Image of the marker protected BufferedImage image; @@ -49,23 +46,11 @@ public class BasicDrawing extends JPanel implements Drawing { // Visible? protected boolean visible; - public BasicMarkerTracker(Point point, BufferedImage image) { - this.point = point; + public BasicOverlayTracker(BufferedImage image) { this.image = image; this.visible = true; } - @Override - public Point getPoint() { - return point; - } - - @Override - public void moveTo(Point point) { - this.point = point; - BasicDrawing.this.repaint(); - } - @Override public void setVisible(boolean visible) { this.visible = visible; @@ -80,6 +65,29 @@ public class BasicDrawing extends JPanel implements Drawing { }; + public class BasicMarkerTracker extends BasicOverlayTracker implements MarkerTracker { + + // Point of the marker. + private Point point; + + public BasicMarkerTracker(Point point, BufferedImage image) { + super(image); + this.point = point; + } + + @Override + public Point getPoint() { + return point; + } + + @Override + public void moveTo(Point point) { + this.point = point; + BasicDrawing.this.repaint(); + } + + }; + // Default path color. public static final Color DEFAULT_PATH_COLOR = new Color(255, 0, 255); diff --git a/src/main/org/insa/graphics/drawing/MapViewDrawing.java b/src/main/org/insa/graphics/drawing/MapViewDrawing.java index 8bc92e4..ac894e8 100644 --- a/src/main/org/insa/graphics/drawing/MapViewDrawing.java +++ b/src/main/org/insa/graphics/drawing/MapViewDrawing.java @@ -27,6 +27,7 @@ import org.mapsforge.map.awt.graphics.AwtGraphicFactory; import org.mapsforge.map.awt.util.AwtUtil; import org.mapsforge.map.awt.view.MapView; import org.mapsforge.map.datastore.MapDataStore; +import org.mapsforge.map.layer.Layer; import org.mapsforge.map.layer.Layers; import org.mapsforge.map.layer.cache.TileCache; import org.mapsforge.map.layer.hills.HillsRenderConfig; @@ -46,37 +47,47 @@ public class MapViewDrawing extends MapView implements Drawing { */ private static final long serialVersionUID = 8606967833704938092L; - public class MapViewMarkerTracker implements MarkerTracker { + public class MapViewOverlayTracker implements OverlayTracker { // Marker associated. - private Marker marker; + protected Layer layer; + + public MapViewOverlayTracker(Layer marker) { + this.layer = marker; + } + + @Override + public void setVisible(boolean visible) { + this.layer.setVisible(visible); + } + + @Override + public void delete() { + MapViewDrawing.this.getLayerManager().getLayers().remove(layer); + } + + }; + + public class MapViewMarkerTracker extends MapViewOverlayTracker implements MarkerTracker { public MapViewMarkerTracker(Marker marker) { - this.marker = marker; + super(marker); } @Override public Point getPoint() { + Marker marker = (Marker) super.layer; return new Point(marker.getLatLong().getLongitude(), marker.getLatLong().getLatitude()); } @Override public void moveTo(Point point) { + Marker marker = (Marker) this.layer; this.delete(); - Marker marker = new Marker(convertPoint(point), this.marker.getBitmap(), this.marker.getHorizontalOffset(), - this.marker.getVerticalOffset()); - this.marker = marker; - MapViewDrawing.this.getLayerManager().getLayers().add(this.marker); - } - - @Override - public void setVisible(boolean visible) { - this.marker.setVisible(visible); - } - - @Override - public void delete() { - MapViewDrawing.this.getLayerManager().getLayers().remove(marker); + marker = new Marker(convertPoint(point), marker.getBitmap(), marker.getHorizontalOffset(), + marker.getVerticalOffset()); + this.layer = marker; + MapViewDrawing.this.getLayerManager().getLayers().add(this.layer); } }; diff --git a/src/main/org/insa/graphics/drawing/MarkerTracker.java b/src/main/org/insa/graphics/drawing/MarkerTracker.java index b43e9b9..c1f3daf 100644 --- a/src/main/org/insa/graphics/drawing/MarkerTracker.java +++ b/src/main/org/insa/graphics/drawing/MarkerTracker.java @@ -2,7 +2,7 @@ package org.insa.graphics.drawing; import org.insa.graph.Point; -public interface MarkerTracker { +public interface MarkerTracker extends OverlayTracker { /** * @return The point associated with this marker. @@ -14,16 +14,4 @@ public interface MarkerTracker { */ public void moveTo(Point point); - /** - * Show or hide this marker - A marker should be visible when created. - * - * @param visible true to show the marker, false to hide. - */ - public void setVisible(boolean visible); - - /** - * Delete this marker. - */ - public void delete(); - } diff --git a/src/main/org/insa/graphics/drawing/OverlayTracker.java b/src/main/org/insa/graphics/drawing/OverlayTracker.java new file mode 100644 index 0000000..f0af951 --- /dev/null +++ b/src/main/org/insa/graphics/drawing/OverlayTracker.java @@ -0,0 +1,17 @@ +package org.insa.graphics.drawing; + +public interface OverlayTracker { + + /** + * Show or hide this marker - A marker should be visible when created. + * + * @param visible true to show the marker, false to hide. + */ + public void setVisible(boolean visible); + + /** + * Delete this marker. + */ + public void delete(); + +}