Create OverlayTracker as a prent of MarkerTracker to allow tracking of other objects.

This commit is contained in:
Holt59 2018-02-25 18:12:15 +01:00
parent 6bc633e5bb
commit 29051be12b
5 changed files with 76 additions and 64 deletions

View File

@ -8,9 +8,7 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import javax.swing.Box; import javax.swing.Box;
import javax.swing.BoxLayout; import javax.swing.BoxLayout;
@ -25,10 +23,8 @@ import javax.swing.JRadioButton;
import javax.swing.JSplitPane; import javax.swing.JSplitPane;
import javax.swing.border.EmptyBorder; 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.ShortestPathAlgorithm;
import org.insa.algo.shortestpath.ShortestPathAlgorithmFactory;
import org.insa.algo.shortestpath.ShortestPathData.Mode; import org.insa.algo.shortestpath.ShortestPathData.Mode;
import org.insa.graph.Graph; import org.insa.graph.Graph;
import org.insa.graph.Node; 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<String, Class<? extends ShortestPathAlgorithm>> SHORTEST_PATH_ALGORITHMS = new HashMap<>();
// Input panels for node. // Input panels for node.
private NodesInputPanel nodesInputPanel; private NodesInputPanel nodesInputPanel;
@ -132,7 +124,8 @@ public class ShortestPathPanel extends JPanel {
add(Box.createVerticalStrut(8)); add(Box.createVerticalStrut(8));
// Add algorithm selection // Add algorithm selection
JComboBox<String> algoSelect = new JComboBox<>(SHORTEST_PATH_ALGORITHMS.keySet().toArray(new String[0])); JComboBox<String> algoSelect = new JComboBox<>(
ShortestPathAlgorithmFactory.getAlgorithmNames().toArray(new String[0]));
algoSelect.setBackground(Color.WHITE); algoSelect.setBackground(Color.WHITE);
algoSelect.setAlignmentX(Component.LEFT_ALIGNMENT); algoSelect.setAlignmentX(Component.LEFT_ALIGNMENT);
add(algoSelect); add(algoSelect);
@ -180,7 +173,8 @@ public class ShortestPathPanel extends JPanel {
Mode mode = lengthModeButton.isSelected() ? Mode.LENGTH : Mode.TIME; Mode mode = lengthModeButton.isSelected() ? Mode.LENGTH : Mode.TIME;
for (ActionListener lis: startActionListeners) { 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)); origin, destination, mode));
} }
} }
@ -240,12 +234,6 @@ public class ShortestPathPanel extends JPanel {
this.startActionListeners.add(listener); 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 { public static void main(String[] args) throws IOException {
String nomcarte = "../BE_Graphe_Maps/morbihan3.mapgr"; String nomcarte = "../BE_Graphe_Maps/morbihan3.mapgr";

View File

@ -38,10 +38,7 @@ public class BasicDrawing extends JPanel implements Drawing {
*/ */
private static final long serialVersionUID = 96779785877771827L; private static final long serialVersionUID = 96779785877771827L;
public class BasicMarkerTracker implements MarkerTracker { public class BasicOverlayTracker implements OverlayTracker {
// Point of the marker.
private Point point;
// Image of the marker // Image of the marker
protected BufferedImage image; protected BufferedImage image;
@ -49,23 +46,11 @@ public class BasicDrawing extends JPanel implements Drawing {
// Visible? // Visible?
protected boolean visible; protected boolean visible;
public BasicMarkerTracker(Point point, BufferedImage image) { public BasicOverlayTracker(BufferedImage image) {
this.point = point;
this.image = image; this.image = image;
this.visible = true; this.visible = true;
} }
@Override
public Point getPoint() {
return point;
}
@Override
public void moveTo(Point point) {
this.point = point;
BasicDrawing.this.repaint();
}
@Override @Override
public void setVisible(boolean visible) { public void setVisible(boolean visible) {
this.visible = 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. // Default path color.
public static final Color DEFAULT_PATH_COLOR = new Color(255, 0, 255); public static final Color DEFAULT_PATH_COLOR = new Color(255, 0, 255);

View File

@ -27,6 +27,7 @@ import org.mapsforge.map.awt.graphics.AwtGraphicFactory;
import org.mapsforge.map.awt.util.AwtUtil; import org.mapsforge.map.awt.util.AwtUtil;
import org.mapsforge.map.awt.view.MapView; import org.mapsforge.map.awt.view.MapView;
import org.mapsforge.map.datastore.MapDataStore; import org.mapsforge.map.datastore.MapDataStore;
import org.mapsforge.map.layer.Layer;
import org.mapsforge.map.layer.Layers; import org.mapsforge.map.layer.Layers;
import org.mapsforge.map.layer.cache.TileCache; import org.mapsforge.map.layer.cache.TileCache;
import org.mapsforge.map.layer.hills.HillsRenderConfig; import org.mapsforge.map.layer.hills.HillsRenderConfig;
@ -46,37 +47,47 @@ public class MapViewDrawing extends MapView implements Drawing {
*/ */
private static final long serialVersionUID = 8606967833704938092L; private static final long serialVersionUID = 8606967833704938092L;
public class MapViewMarkerTracker implements MarkerTracker { public class MapViewOverlayTracker implements OverlayTracker {
// Marker associated. // 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) { public MapViewMarkerTracker(Marker marker) {
this.marker = marker; super(marker);
} }
@Override @Override
public Point getPoint() { public Point getPoint() {
Marker marker = (Marker) super.layer;
return new Point(marker.getLatLong().getLongitude(), marker.getLatLong().getLatitude()); return new Point(marker.getLatLong().getLongitude(), marker.getLatLong().getLatitude());
} }
@Override @Override
public void moveTo(Point point) { public void moveTo(Point point) {
Marker marker = (Marker) this.layer;
this.delete(); this.delete();
Marker marker = new Marker(convertPoint(point), this.marker.getBitmap(), this.marker.getHorizontalOffset(), marker = new Marker(convertPoint(point), marker.getBitmap(), marker.getHorizontalOffset(),
this.marker.getVerticalOffset()); marker.getVerticalOffset());
this.marker = marker; this.layer = marker;
MapViewDrawing.this.getLayerManager().getLayers().add(this.marker); MapViewDrawing.this.getLayerManager().getLayers().add(this.layer);
}
@Override
public void setVisible(boolean visible) {
this.marker.setVisible(visible);
}
@Override
public void delete() {
MapViewDrawing.this.getLayerManager().getLayers().remove(marker);
} }
}; };

View File

@ -2,7 +2,7 @@ package org.insa.graphics.drawing;
import org.insa.graph.Point; import org.insa.graph.Point;
public interface MarkerTracker { public interface MarkerTracker extends OverlayTracker {
/** /**
* @return The point associated with this marker. * @return The point associated with this marker.
@ -14,16 +14,4 @@ public interface MarkerTracker {
*/ */
public void moveTo(Point point); 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();
} }

View File

@ -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();
}