Use arc instead of only road type to find width and color for drawing.

This commit is contained in:
Mikael Capelle 2018-03-01 13:33:28 +01:00
parent e15d04eda9
commit 0963f4a12f
4 changed files with 139 additions and 128 deletions

View File

@ -126,7 +126,8 @@ public class BasicDrawing extends JPanel implements Drawing {
double scale = DEFAULT_MARKER_WIDTH / (double) img.getHeight(); double scale = DEFAULT_MARKER_WIDTH / (double) img.getHeight();
gr.scale(scale, scale); gr.scale(scale, scale);
graphics.drawImage(img, px - img.getWidth() / 2, py - img.getHeight(), BasicDrawing.this); graphics.drawImage(img, px - img.getWidth() / 2, py - img.getHeight(),
BasicDrawing.this);
} }
}; };
@ -285,7 +286,8 @@ public class BasicDrawing extends JPanel implements Drawing {
* *
*/ */
public BasicDrawing() { public BasicDrawing() {
this.zoomAndPanListener = new ZoomAndPanListener(this, ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20, 1.2); this.zoomAndPanListener = new ZoomAndPanListener(this,
ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20, 1.2);
this.addMouseListener(zoomAndPanListener); this.addMouseListener(zoomAndPanListener);
this.addMouseMotionListener(zoomAndPanListener); this.addMouseMotionListener(zoomAndPanListener);
this.addMouseWheelListener(zoomAndPanListener); this.addMouseWheelListener(zoomAndPanListener);
@ -339,7 +341,8 @@ public class BasicDrawing extends JPanel implements Drawing {
// Get the point using the inverse transform of the Zoom/Pan object, this gives // Get the point using the inverse transform of the Zoom/Pan object, this gives
// us // us
// a point within the drawing box (between [0, 0] and [width, height]). // a point within the drawing box (between [0, 0] and [width, height]).
Point2D ptDst = this.zoomAndPanListener.getCoordTransform().inverseTransform(event.getPoint(), null); Point2D ptDst = this.zoomAndPanListener.getCoordTransform()
.inverseTransform(event.getPoint(), null);
// Inverse the "projection" on x/y to get longitude and latitude. // Inverse the "projection" on x/y to get longitude and latitude.
double lon = ptDst.getX(); double lon = ptDst.getX();
@ -412,14 +415,14 @@ public class BasicDrawing extends JPanel implements Drawing {
* *
* @param arc Arc to draw. * @param arc Arc to draw.
* @param palette Palette to use to retrieve color and width for arc, or null to * @param palette Palette to use to retrieve color and width for arc, or null to
* use current settings. * use current settings.
*/ */
protected void drawArc(Arc arc, GraphPalette palette) { protected void drawArc(Arc arc, GraphPalette palette) {
List<Point> pts = arc.getPoints(); List<Point> pts = arc.getPoints();
if (!pts.isEmpty()) { if (!pts.isEmpty()) {
if (palette != null) { if (palette != null) {
this.graphGraphics.setColor(palette.getColorForType(arc.getInfo().getType())); this.graphGraphics.setColor(palette.getColorForArc(arc));
this.graphGraphics.setStroke(new BasicStroke(palette.getWidthForType(arc.getInfo().getType()))); this.graphGraphics.setStroke(new BasicStroke(palette.getWidthForArc(arc)));
} }
Iterator<Point> it1 = pts.iterator(); Iterator<Point> it1 = pts.iterator();
Point prev = it1.next(); Point prev = it1.next();
@ -449,8 +452,8 @@ public class BasicDrawing extends JPanel implements Drawing {
this.clear(); this.clear();
// Find minimum/maximum longitude and latitude. // Find minimum/maximum longitude and latitude.
double minLon = Double.POSITIVE_INFINITY, minLat = Double.POSITIVE_INFINITY, maxLon = Double.NEGATIVE_INFINITY, double minLon = Double.POSITIVE_INFINITY, minLat = Double.POSITIVE_INFINITY,
maxLat = Double.NEGATIVE_INFINITY; maxLon = Double.NEGATIVE_INFINITY, maxLat = Double.NEGATIVE_INFINITY;
for (Node node: graph.getNodes()) { for (Node node: graph.getNodes()) {
Point pt = node.getPoint(); Point pt = node.getPoint();
if (pt.getLatitude() < minLat) { if (pt.getLatitude() < minLat) {
@ -488,7 +491,8 @@ public class BasicDrawing extends JPanel implements Drawing {
} }
// Create the image // Create the image
BufferedImage img = new BufferedImage(this.width, this.height, BufferedImage.TYPE_3BYTE_BGR); BufferedImage img = new BufferedImage(this.width, this.height,
BufferedImage.TYPE_3BYTE_BGR);
this.graphImage = img; this.graphImage = img;
this.graphGraphics = img.createGraphics(); this.graphGraphics = img.createGraphics();
this.graphGraphics.setBackground(Color.WHITE); this.graphGraphics.setBackground(Color.WHITE);
@ -496,10 +500,12 @@ public class BasicDrawing extends JPanel implements Drawing {
// Set the zoom and pan listener // Set the zoom and pan listener
double scale = 1 / Math.max(this.width / (double) this.getWidth(), this.height / (double) this.getHeight()); double scale = 1 / Math.max(this.width / (double) this.getWidth(),
this.height / (double) this.getHeight());
this.zoomAndPanListener.setCoordTransform(this.graphGraphics.getTransform()); this.zoomAndPanListener.setCoordTransform(this.graphGraphics.getTransform());
this.zoomAndPanListener.getCoordTransform().translate((this.getWidth() - this.width * scale) / 2, this.zoomAndPanListener.getCoordTransform().translate(
(this.getWidth() - this.width * scale) / 2,
(this.getHeight() - this.height * scale) / 2); (this.getHeight() - this.height * scale) / 2);
this.zoomAndPanListener.getCoordTransform().scale(scale, scale); this.zoomAndPanListener.getCoordTransform().scale(scale, scale);
this.zoomAndPanListener.setZoomLevel(0); this.zoomAndPanListener.setZoomLevel(0);
@ -513,7 +519,8 @@ public class BasicDrawing extends JPanel implements Drawing {
this.initialize(graph); this.initialize(graph);
for (Node node: graph.getNodes()) { for (Node node: graph.getNodes()) {
for (Arc arc: node.getSuccessors()) { for (Arc arc: node.getSuccessors()) {
if (arc.getInfo().isOneWay() || arc.getOrigin().compareTo(arc.getDestination()) < 0) { if (arc.getInfo().isOneWay()
|| arc.getOrigin().compareTo(arc.getDestination()) < 0) {
drawArc(arc, palette); drawArc(arc, palette);
} }
} }

View File

@ -2,11 +2,12 @@ package org.insa.graphics.drawing;
import java.awt.Color; import java.awt.Color;
import org.insa.graph.Arc;
import org.insa.graph.RoadInformation.RoadType; import org.insa.graph.RoadInformation.RoadType;
public class BasicGraphPalette implements GraphPalette { public class BasicGraphPalette implements GraphPalette {
// Color types for arc. // Color types for arc.
static final Color motorway = Color.RED; static final Color motorway = Color.RED;
static final Color bigroad = new Color(255, 105, 0); static final Color bigroad = new Color(255, 105, 0);
static final Color smallroad = new Color(255, 234, 0); static final Color smallroad = new Color(255, 234, 0);
@ -18,80 +19,84 @@ public class BasicGraphPalette implements GraphPalette {
/** /**
* *
*/ */
public BasicGraphPalette() { } public BasicGraphPalette() {
}
@Override @Override
public int getDefaultPointWidth() { public int getDefaultPointWidth() {
return 2; return 2;
} }
@Override @Override
public Color getDefaultPointColor() { public Color getDefaultPointColor() {
return Color.GREEN; return Color.GREEN;
} }
@Override @Override
public Color getColorForType(RoadType type) { public Color getColorForArc(Arc arc) {
Color color = Color.BLACK; RoadType type = arc.getInfo().getType();
switch (type) { Color color = Color.BLACK;
case MOTORWAY: switch (type) {
color = motorway; case MOTORWAY:
break; color = motorway;
case TRUNK: break;
case PRIMARY: case TRUNK:
case SECONDARY: case PRIMARY:
case MOTORWAY_LINK: case SECONDARY:
case TRUNK_LINK: case MOTORWAY_LINK:
case PRIMARY_LINK: case TRUNK_LINK:
color = bigroad; case PRIMARY_LINK:
break; color = bigroad;
case SECONDARY_LINK: break;
case TERTIARY: case SECONDARY_LINK:
case RESIDENTIAL: case TERTIARY:
case UNCLASSIFIED: case RESIDENTIAL:
case ROAD: case UNCLASSIFIED:
case LIVING_STREET: case ROAD:
case SERVICE: case LIVING_STREET:
case ROUNDABOUT: case SERVICE:
color = smallroad; case ROUNDABOUT:
break; color = smallroad;
case COASTLINE: break;
color = coastline; case COASTLINE:
break; color = coastline;
} break;
return color; }
}
@Override return color;
public int getWidthForType(RoadType type) { }
int width = 1;
switch (type) { @Override
case MOTORWAY: public int getWidthForArc(Arc arc) {
width = 2; RoadType type = arc.getInfo().getType();
break; int width = 1;
case TRUNK: switch (type) {
case PRIMARY: case MOTORWAY:
case SECONDARY: width = 2;
case MOTORWAY_LINK: break;
case TRUNK_LINK: case TRUNK:
case PRIMARY_LINK: case PRIMARY:
width = 1; case SECONDARY:
break; case MOTORWAY_LINK:
case SECONDARY_LINK: case TRUNK_LINK:
case TERTIARY: case PRIMARY_LINK:
case RESIDENTIAL: width = 1;
case UNCLASSIFIED: break;
case ROAD: case SECONDARY_LINK:
case LIVING_STREET: case TERTIARY:
case SERVICE: case RESIDENTIAL:
case ROUNDABOUT: case UNCLASSIFIED:
width = 1; case ROAD:
break; case LIVING_STREET:
case COASTLINE: case SERVICE:
width = 4; case ROUNDABOUT:
break; width = 1;
} break;
return width; case COASTLINE:
} width = 4;
break;
}
return width;
}
} }

View File

@ -2,24 +2,23 @@ package org.insa.graphics.drawing;
import java.awt.Color; import java.awt.Color;
import org.insa.graph.RoadInformation.RoadType; import org.insa.graph.Arc;
public class BlackAndWhiteGraphPalette extends BasicGraphPalette { public class BlackAndWhiteGraphPalette extends BasicGraphPalette {
// Road colors (index // Road colors (index
private final static Color[] ROAD_COLOR_FROM_WIDTH = { private final static Color[] ROAD_COLOR_FROM_WIDTH = { null, new Color(140, 140, 140),
null, new Color(140, 140, 140), new Color(80, 80, 80), new Color(40, 40, 40), new Color(30, 30, 30) new Color(80, 80, 80), new Color(40, 40, 40), new Color(30, 30, 30) };
};
@Override @Override
public Color getDefaultPointColor() { public Color getDefaultPointColor() {
return Color.BLACK; return Color.BLACK;
} }
@Override @Override
public Color getColorForType(RoadType type) { public Color getColorForArc(Arc arc) {
int width = getWidthForType(type); int width = getWidthForArc(arc);
return ROAD_COLOR_FROM_WIDTH[width]; return ROAD_COLOR_FROM_WIDTH[width];
} }
} }

View File

@ -2,32 +2,32 @@ package org.insa.graphics.drawing;
import java.awt.Color; import java.awt.Color;
import org.insa.graph.RoadInformation.RoadType; import org.insa.graph.Arc;
public interface GraphPalette { public interface GraphPalette {
/** /**
* @return The default point width for this palette. * @return The default point width for this palette.
*/ */
public int getDefaultPointWidth(); public int getDefaultPointWidth();
/** /**
* @return The default point color for this palette. * @return The default point color for this palette.
*/ */
public Color getDefaultPointColor(); public Color getDefaultPointColor();
/** /**
* @param type Type of the road. * @param arc
* *
* @return Color associated to the given type of road. * @return Color associated with the given arc.
*/ */
public Color getColorForType(RoadType type); public Color getColorForArc(Arc arc);
/** /**
* @param type Type of the road. * @param arc
* *
* @return Width associated to the given type of road. * @return Width associated with the given arc.
*/ */
public int getWidthForType(RoadType type); public int getWidthForArc(Arc arc);
} }