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();
@ -418,8 +421,8 @@ public class BasicDrawing extends JPanel implements Drawing {
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,6 +2,7 @@ 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 {
@ -18,7 +19,8 @@ public class BasicGraphPalette implements GraphPalette {
/** /**
* *
*/ */
public BasicGraphPalette() { } public BasicGraphPalette() {
}
@Override @Override
public int getDefaultPointWidth() { public int getDefaultPointWidth() {
@ -31,7 +33,8 @@ public class BasicGraphPalette implements GraphPalette {
} }
@Override @Override
public Color getColorForType(RoadType type) { public Color getColorForArc(Arc arc) {
RoadType type = arc.getInfo().getType();
Color color = Color.BLACK; Color color = Color.BLACK;
switch (type) { switch (type) {
case MOTORWAY: case MOTORWAY:
@ -59,11 +62,13 @@ public class BasicGraphPalette implements GraphPalette {
color = coastline; color = coastline;
break; break;
} }
return color; return color;
} }
@Override @Override
public int getWidthForType(RoadType type) { public int getWidthForArc(Arc arc) {
RoadType type = arc.getInfo().getType();
int width = 1; int width = 1;
switch (type) { switch (type) {
case MOTORWAY: case MOTORWAY:

View File

@ -2,14 +2,13 @@ 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() {
@ -17,8 +16,8 @@ public class BlackAndWhiteGraphPalette extends BasicGraphPalette {
} }
@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,7 +2,7 @@ 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 {
@ -17,17 +17,17 @@ public interface GraphPalette {
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);
} }