Clean code.

This commit is contained in:
Mikael Capelle 2018-03-05 17:58:20 +01:00
parent 3252a6faba
commit be1176c1e3
2 changed files with 46 additions and 30 deletions

View File

@ -105,14 +105,15 @@ public class MapViewDrawing extends MapView implements Drawing {
Color color; Color color;
public MapViewMarkerOverlay(Marker marker, Color color) { public MapViewMarkerOverlay(Marker marker, Color color) {
super(new Layer[] { marker }); super(new Layer[]{ marker });
this.color = color; this.color = color;
} }
@Override @Override
public Point getPoint() { public Point getPoint() {
Marker marker = (Marker) super.layers[0]; Marker marker = (Marker) super.layers[0];
return new Point(marker.getLatLong().getLongitude(), marker.getLatLong().getLatitude()); return new Point((float) marker.getLatLong().getLongitude(),
(float) marker.getLatLong().getLatitude());
} }
@Override @Override
@ -138,11 +139,11 @@ public class MapViewDrawing extends MapView implements Drawing {
private class MapViewPathOverlay extends MapViewOverlay implements PathOverlay { private class MapViewPathOverlay extends MapViewOverlay implements PathOverlay {
public MapViewPathOverlay(PolylineAutoScaling path, Marker origin, Marker destination) { public MapViewPathOverlay(PolylineAutoScaling path, Marker origin, Marker destination) {
super(new Layer[] { path, origin, destination }); super(new Layer[]{ path, origin, destination });
} }
public MapViewPathOverlay(PolylineAutoScaling path) { public MapViewPathOverlay(PolylineAutoScaling path) {
super(new Layer[] { path }); super(new Layer[]{ path });
} }
} }
@ -253,7 +254,8 @@ public class MapViewDrawing extends MapView implements Drawing {
super.paint(graphics); super.paint(graphics);
if (this.zoomControls != null) { if (this.zoomControls != null) {
this.zoomControls.setZoomLevel(this.getModel().mapViewPosition.getZoomLevel()); this.zoomControls.setZoomLevel(this.getModel().mapViewPosition.getZoomLevel());
this.zoomControls.draw((Graphics2D) graphics, getWidth() - this.zoomControls.getWidth() - 20, this.zoomControls.draw((Graphics2D) graphics,
getWidth() - this.zoomControls.getWidth() - 20,
this.getHeight() - this.zoomControls.getHeight() - 10, this); this.getHeight() - this.zoomControls.getHeight() - 10, this);
} }
@ -263,17 +265,19 @@ public class MapViewDrawing extends MapView implements Drawing {
return new LatLong(point.getLatitude(), point.getLongitude()); return new LatLong(point.getLatitude(), point.getLongitude());
} }
private TileRendererLayer createTileRendererLayer(TileCache tileCache, MapDataStore mapDataStore, private TileRendererLayer createTileRendererLayer(TileCache tileCache,
MapViewPosition mapViewPosition, HillsRenderConfig hillsRenderConfig) { MapDataStore mapDataStore, MapViewPosition mapViewPosition,
TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore, mapViewPosition, false, HillsRenderConfig hillsRenderConfig) {
true, false, GRAPHIC_FACTORY, hillsRenderConfig) { TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore,
mapViewPosition, false, true, false, GRAPHIC_FACTORY, hillsRenderConfig) {
@Override @Override
public boolean onTap(LatLong tapLatLong, org.mapsforge.core.model.Point layerXY, public boolean onTap(LatLong tapLatLong, org.mapsforge.core.model.Point layerXY,
org.mapsforge.core.model.Point tapXY) { org.mapsforge.core.model.Point tapXY) {
if (zoomControls.contains(new java.awt.Point((int) tapXY.x, (int) tapXY.y))) { if (zoomControls.contains(new java.awt.Point((int) tapXY.x, (int) tapXY.y))) {
return false; return false;
} }
Point pt = new Point(tapLatLong.getLongitude(), tapLatLong.getLatitude()); Point pt = new Point((float) tapLatLong.getLongitude(),
(float) tapLatLong.getLatitude());
for (DrawingClickListener listener: MapViewDrawing.this.drawingClickListeners) { for (DrawingClickListener listener: MapViewDrawing.this.drawingClickListeners) {
listener.mouseClicked(pt); listener.mouseClicked(pt);
} }
@ -326,7 +330,8 @@ public class MapViewDrawing extends MapView implements Drawing {
public void drawGraph(File file) { public void drawGraph(File file) {
// Tile cache // Tile cache
TileCache tileCache = AwtUtil.createTileCache(tileSize, getModel().frameBufferModel.getOverdrawFactor(), 1024, TileCache tileCache = AwtUtil.createTileCache(tileSize,
getModel().frameBufferModel.getOverdrawFactor(), 1024,
new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString())); new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()));
// Layers // Layers
@ -339,12 +344,15 @@ public class MapViewDrawing extends MapView implements Drawing {
BoundingBox boundingBox = mapDataStore.boundingBox(); BoundingBox boundingBox = mapDataStore.boundingBox();
final Model model = getModel(); final Model model = getModel();
if (model.mapViewPosition.getZoomLevel() == 0 || !boundingBox.contains(model.mapViewPosition.getCenter())) { if (model.mapViewPosition.getZoomLevel() == 0
byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(), boundingBox, || !boundingBox.contains(model.mapViewPosition.getCenter())) {
model.displayModel.getTileSize()); byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(),
model.mapViewPosition.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel)); boundingBox, model.displayModel.getTileSize());
model.mapViewPosition
.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
zoomControls.setZoomLevel(zoomLevel); zoomControls.setZoomLevel(zoomLevel);
} }
} }
@Override @Override
@ -366,7 +374,8 @@ public class MapViewDrawing extends MapView implements Drawing {
PathOverlay overlay = null; PathOverlay overlay = null;
if (markers) { if (markers) {
Marker origin = createMarker(path.getOrigin().getPoint(), DEFAULT_PATH_COLOR), Marker origin = createMarker(path.getOrigin().getPoint(), DEFAULT_PATH_COLOR),
destination = createMarker(path.getDestination().getPoint(), DEFAULT_PATH_COLOR); destination = createMarker(path.getDestination().getPoint(),
DEFAULT_PATH_COLOR);
overlay = new MapViewPathOverlay(line, origin, destination); overlay = new MapViewPathOverlay(line, origin, destination);
} }
else { else {

View File

@ -55,19 +55,20 @@ public class MapZoomControls {
// Zoom in/out image and their rectangles. // Zoom in/out image and their rectangles.
private final Image zoomIn, zoomOut; private final Image zoomIn, zoomOut;
private final Rectangle zoomInRect = new Rectangle(0, 0, 0, 0), zoomOutRect = new Rectangle(0, 0, 0, 0); private final Rectangle zoomInRect = new Rectangle(0, 0, 0, 0),
zoomOutRect = new Rectangle(0, 0, 0, 0);
// List of listeners // List of listeners
private final List<ActionListener> zoomInListeners = new ArrayList<>(); private final List<ActionListener> zoomInListeners = new ArrayList<>();
private final List<ActionListener> zoomOutListeners = new ArrayList<>(); private final List<ActionListener> zoomOutListeners = new ArrayList<>();
public MapZoomControls(Component component, final int defaultZoom, final int minZoom, final int maxZoom) public MapZoomControls(Component component, final int defaultZoom, final int minZoom,
throws IOException { final int maxZoom) throws IOException {
zoomIn = ImageIO.read(new File("res/zoomIn.png")).getScaledInstance(DEFAULT_HEIGHT, DEFAULT_HEIGHT, zoomIn = ImageIO.read(new File("res/zoomIn.png")).getScaledInstance(DEFAULT_HEIGHT,
Image.SCALE_SMOOTH); DEFAULT_HEIGHT, Image.SCALE_SMOOTH);
zoomOut = ImageIO.read(new File("res/zoomOut.png")).getScaledInstance(DEFAULT_HEIGHT, DEFAULT_HEIGHT, zoomOut = ImageIO.read(new File("res/zoomOut.png")).getScaledInstance(DEFAULT_HEIGHT,
Image.SCALE_SMOOTH); DEFAULT_HEIGHT, Image.SCALE_SMOOTH);
this.currentLevel = defaultZoom; this.currentLevel = defaultZoom;
this.minLevel = minZoom; this.minLevel = minZoom;
@ -91,13 +92,15 @@ public class MapZoomControls {
if (zoomInRect.contains(e.getPoint()) && currentLevel < maxLevel) { if (zoomInRect.contains(e.getPoint()) && currentLevel < maxLevel) {
currentLevel += 1; currentLevel += 1;
for (ActionListener al: zoomInListeners) { for (ActionListener al: zoomInListeners) {
al.actionPerformed(new ActionEvent(this, ZOOM_IN_ACTION_ID, ZOOM_IN_ACTION_NAME)); al.actionPerformed(
new ActionEvent(this, ZOOM_IN_ACTION_ID, ZOOM_IN_ACTION_NAME));
} }
} }
else if (zoomOutRect.contains(e.getPoint()) && currentLevel > minLevel) { else if (zoomOutRect.contains(e.getPoint()) && currentLevel > minLevel) {
currentLevel -= 1; currentLevel -= 1;
for (ActionListener al: zoomOutListeners) { for (ActionListener al: zoomOutListeners) {
al.actionPerformed(new ActionEvent(this, ZOOM_OUT_ACTION_ID, ZOOM_OUT_ACTION_NAME)); al.actionPerformed(
new ActionEvent(this, ZOOM_OUT_ACTION_ID, ZOOM_OUT_ACTION_NAME));
} }
} }
component.repaint(); component.repaint();
@ -150,7 +153,8 @@ public class MapZoomControls {
* @return Width of this "component" when drawn. * @return Width of this "component" when drawn.
*/ */
public int getWidth() { public int getWidth() {
return DEFAULT_HEIGHT + 2 + (this.maxLevel - this.minLevel) * DEFAULT_SPACING + 1 + 2 + DEFAULT_HEIGHT; return DEFAULT_HEIGHT + 2 + (this.maxLevel - this.minLevel) * DEFAULT_SPACING + 1 + 2
+ DEFAULT_HEIGHT;
} }
/** /**
@ -160,7 +164,7 @@ public class MapZoomControls {
* @param point * @param point
* *
* @return true if the given point correspond to an element of this zoom * @return true if the given point correspond to an element of this zoom
* controls. * controls.
*/ */
public boolean contains(Point point) { public boolean contains(Point point) {
return zoomInRect.contains(point) || zoomOutRect.contains(point); return zoomInRect.contains(point) || zoomOutRect.contains(point);
@ -179,11 +183,14 @@ public class MapZoomControls {
// Draw ticks // Draw ticks
xoffset += DEFAULT_HEIGHT + 2; xoffset += DEFAULT_HEIGHT + 2;
g.setColor(ZOOM_TICK_COLOR); g.setColor(ZOOM_TICK_COLOR);
g.drawLine(xoffset, yoffset + height / 2, xoffset + (this.maxLevel - this.minLevel) * DEFAULT_SPACING + 1, g.drawLine(xoffset, yoffset + height / 2,
xoffset + (this.maxLevel - this.minLevel) * DEFAULT_SPACING + 1,
yoffset + height / 2); yoffset + height / 2);
for (int i = 0; i <= (this.maxLevel - this.minLevel); i += halfMark ? 2 : 1) { for (int i = 0; i <= (this.maxLevel - this.minLevel); i += halfMark ? 2 : 1) {
g.drawLine(xoffset + i * DEFAULT_SPACING, yoffset + (int) (height * (1 - ZOOM_TICK_HEIGHT_RATIO) / 2), g.drawLine(xoffset + i * DEFAULT_SPACING,
xoffset + i * DEFAULT_SPACING, yoffset + (int) (height * (1 + ZOOM_TICK_HEIGHT_RATIO) / 2)); yoffset + (int) (height * (1 - ZOOM_TICK_HEIGHT_RATIO) / 2),
xoffset + i * DEFAULT_SPACING,
yoffset + (int) (height * (1 + ZOOM_TICK_HEIGHT_RATIO) / 2));
} }
// Draw current ticks // Draw current ticks