From 43835e48371d735c4e48bb307758ac30783b8313 Mon Sep 17 00:00:00 2001 From: Holt59 Date: Tue, 27 Feb 2018 22:09:57 +0100 Subject: [PATCH] Remove drawPoint, switch to PointSetOverlay. --- ...aklyConnectedComponentGraphicObserver.java | 13 +- .../insa/graphics/drawing/BasicDrawing.java | 131 ++++++++++++------ .../org/insa/graphics/drawing/Drawing.java | 28 ++-- .../insa/graphics/drawing/MapViewDrawing.java | 69 +++++++-- .../drawing/overlays/PointSetOverlay.java | 64 +++++++++ 5 files changed, 237 insertions(+), 68 deletions(-) create mode 100644 src/main/org/insa/graphics/drawing/overlays/PointSetOverlay.java diff --git a/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentGraphicObserver.java b/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentGraphicObserver.java index c7d272f..46e96b3 100644 --- a/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentGraphicObserver.java +++ b/src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentGraphicObserver.java @@ -5,33 +5,36 @@ import java.util.ArrayList; import org.insa.graph.Node; import org.insa.graphics.drawing.Drawing; +import org.insa.graphics.drawing.overlays.PointSetOverlay; public class WeaklyConnectedComponentGraphicObserver implements WeaklyConnectedComponentObserver { private static final Color[] COLORS = { Color.BLUE, Color.ORANGE, Color.GREEN, Color.YELLOW, Color.RED }; // Drawing + Graph drawing - private Drawing drawing; + private PointSetOverlay grPoints; // Current index color - private int cindex = -1; + private int cindex = 0; public WeaklyConnectedComponentGraphicObserver(Drawing drawing) { - this.drawing = drawing; + this.grPoints = drawing.createPointSetOverlay(); + this.grPoints.setWidth(1); } @Override public void notifyStartComponent(Node curNode) { - cindex = (cindex + 1) % COLORS.length; + this.grPoints.setColor(COLORS[cindex]); } @Override public void notifyNewNodeInComponent(Node node) { - this.drawing.drawPoint(node.getPoint(), 1, COLORS[cindex]); + this.grPoints.addPoint(node.getPoint()); } @Override public void notifyEndComponent(ArrayList nodes) { + cindex = (cindex + 1) % COLORS.length; } } diff --git a/src/main/org/insa/graphics/drawing/BasicDrawing.java b/src/main/org/insa/graphics/drawing/BasicDrawing.java index d11fa1a..5678703 100644 --- a/src/main/org/insa/graphics/drawing/BasicDrawing.java +++ b/src/main/org/insa/graphics/drawing/BasicDrawing.java @@ -28,6 +28,7 @@ import org.insa.graphics.drawing.overlays.MarkerOverlay; import org.insa.graphics.drawing.overlays.MarkerUtils; import org.insa.graphics.drawing.overlays.Overlay; import org.insa.graphics.drawing.overlays.PathOverlay; +import org.insa.graphics.drawing.overlays.PointSetOverlay; /** * Cette implementation de la classe Dessin produit vraiment un affichage (au @@ -82,11 +83,14 @@ public class BasicDrawing extends JPanel implements Drawing { private class BasicMarkerOverlay extends BasicOverlay implements MarkerOverlay { + // Default marker width + private static final int DEFAULT_MARKER_WIDTH = 20; + // Point of the marker. private Point point; // Color of the marker. - private Color color; + private final Color color; public BasicMarkerOverlay(Point point, Color color) { super(); @@ -129,13 +133,13 @@ public class BasicDrawing extends JPanel implements Drawing { private class BasicPathOverlay extends BasicOverlay implements PathOverlay { // List of points - List points; + private final List points; // Color for the path - Color color; + private Color color; // Origin / Destination markers. - BasicMarkerOverlay origin, destination; + private BasicMarkerOverlay origin, destination; public BasicPathOverlay(List points, Color color, BasicMarkerOverlay origin, BasicMarkerOverlay destination) { @@ -180,16 +184,83 @@ public class BasicDrawing extends JPanel implements Drawing { }; + private class BasicPointSetOverlay extends BasicOverlay implements PointSetOverlay { + + // Default point width + private static final int DEFAULT_POINT_WIDTH = 5; + + // Image for path / points + private final BufferedImage image; + private final Graphics2D graphics; + + private int width = DEFAULT_POINT_WIDTH; + + public BasicPointSetOverlay() { + this.image = new BufferedImage(BasicDrawing.this.width, BasicDrawing.this.height, + BufferedImage.TYPE_4BYTE_ABGR); + this.graphics = image.createGraphics(); + this.graphics.setBackground(new Color(0, 0, 0, 0)); + } + + @Override + public void setColor(Color color) { + this.graphics.setColor(color); + } + + @Override + public void setWidth(int width) { + this.width = width; + } + + @Override + public void setWidthAndColor(int width, Color color) { + setWidth(width); + setColor(color); + } + + @Override + public void addPoint(Point point) { + this.width = 5; + System.out.println("Add point!"); + int x = BasicDrawing.this.projx(point.getLongitude()) - this.width / 2; + int y = BasicDrawing.this.projy(point.getLatitude()) - this.width / 2; + this.graphics.fillOval(x, y, this.width, this.width); + BasicDrawing.this.repaint(); + } + + @Override + public void addPoint(Point point, int width) { + setWidth(width); + addPoint(point); + } + + @Override + public void addPoint(Point point, Color color) { + setColor(color); + addPoint(point); + } + + @Override + public void addPoint(Point point, int width, Color color) { + setWidth(width); + setColor(color); + addPoint(point); + } + + @Override + public void drawImpl(Graphics2D g) { + System.out.println("Draw?"); + g.drawImage(this.image, 0, 0, BasicDrawing.this); + } + + } + // Default path color. public static final Color DEFAULT_PATH_COLOR = new Color(255, 0, 255); // Default palette. public static final GraphPalette DEFAULT_PALETTE = new BasicGraphPalette(); - // Default marker and point width - private static final int DEFAULT_MARKER_WIDTH = 20; - private static final int DEFAULT_POINT_WIDTH = 5; - private double long1, long2, lat1, lat2; // Width and height of the image @@ -201,10 +272,6 @@ public class BasicDrawing extends JPanel implements Drawing { private Image graphImage; private final Graphics2D graphGraphics; - // Image for path / points - private Image overlayImage; - private Graphics2D overlayGraphics; - // List of image for markers private List overlays = new ArrayList<>(); @@ -230,11 +297,6 @@ public class BasicDrawing extends JPanel implements Drawing { this.graphGraphics = img.createGraphics(); this.graphGraphics.setBackground(Color.WHITE); - img = new BufferedImage(this.width, this.height, BufferedImage.TYPE_4BYTE_ABGR); - this.overlayImage = img; - this.overlayGraphics = img.createGraphics(); - this.overlayGraphics.setBackground(new Color(0, 0, 0, 0)); - this.zoomAndPanListener.setCoordTransform(this.graphGraphics.getTransform()); this.long1 = -180; @@ -256,9 +318,6 @@ public class BasicDrawing extends JPanel implements Drawing { // Draw graph g.drawImage(graphImage, 0, 0, this); - // Draw overlays (path, etc.) - g.drawImage(overlayImage, 0, 0, this); - // Draw markers for (BasicOverlay overlay: overlays) { overlay.draw(g); @@ -341,18 +400,9 @@ public class BasicDrawing extends JPanel implements Drawing { this.removeMouseListener(this.listenerMapping.get(listener)); } - protected void setWidth(int width) { - this.overlayGraphics.setStroke(new BasicStroke(width)); - } - - protected void setColor(Color col) { - this.overlayGraphics.setColor(col); - } - @Override public void clear() { this.graphGraphics.clearRect(0, 0, this.width, this.height); - this.overlayGraphics.clearRect(0, 0, this.width, this.height); this.overlays.clear(); } @@ -360,11 +410,6 @@ public class BasicDrawing extends JPanel implements Drawing { return new BasicMarkerOverlay(point, color); } - @Override - public MarkerOverlay drawMarker(Point point) { - return drawMarker(point, this.overlayGraphics.getColor()); - } - @Override public MarkerOverlay drawMarker(Point point, Color color) { BasicMarkerOverlay marker = createMarker(point, color); @@ -374,13 +419,17 @@ public class BasicDrawing extends JPanel implements Drawing { } @Override - public void drawPoint(Point point, int width, Color color) { - setWidth(width); - setColor(color); - int x = this.projx(point.getLongitude()) - DEFAULT_POINT_WIDTH / 2; - int y = this.projy(point.getLatitude()) - DEFAULT_POINT_WIDTH / 2; - overlayGraphics.fillOval(x, y, DEFAULT_POINT_WIDTH, DEFAULT_POINT_WIDTH); - this.repaint(); + public PointSetOverlay createPointSetOverlay() { + BasicPointSetOverlay ps = new BasicPointSetOverlay(); + this.overlays.add(ps); + return ps; + } + + @Override + public PointSetOverlay createPointSetOverlay(int width, Color color) { + PointSetOverlay ps = createPointSetOverlay(); + ps.setWidthAndColor(width, color); + return ps; } /** diff --git a/src/main/org/insa/graphics/drawing/Drawing.java b/src/main/org/insa/graphics/drawing/Drawing.java index 7fc4867..237a0d2 100644 --- a/src/main/org/insa/graphics/drawing/Drawing.java +++ b/src/main/org/insa/graphics/drawing/Drawing.java @@ -7,6 +7,7 @@ import org.insa.graph.Path; import org.insa.graph.Point; import org.insa.graphics.drawing.overlays.MarkerOverlay; import org.insa.graphics.drawing.overlays.PathOverlay; +import org.insa.graphics.drawing.overlays.PointSetOverlay; public interface Drawing { @@ -29,13 +30,6 @@ public interface Drawing { */ public void clear(); - /** - * Draw a marker at the given point with the default color. - * - * @param point - */ - public MarkerOverlay drawMarker(Point point); - /** * Draw the given point with the given color. * @@ -44,14 +38,22 @@ public interface Drawing { public MarkerOverlay drawMarker(Point point, Color color); /** - * Draw a point width the given width and color. Do not use this to mark - * location, use drawMarker. + * Create a new PointSetOverlay that can be used to add overlay points to this + * drawing. + * + * PointSetOverlay are heavy memory resources, do not use one for each point! * - * @param point - * @param width - * @param color */ - public void drawPoint(Point point, int width, Color color); + public PointSetOverlay createPointSetOverlay(); + + /** + * Create a new PointSetOverlay with the original width and color that can be + * used to add overlay points to this drawing. + * + * PointSetOverlay are heavy memory resources, do not use one for each point! + * + */ + public PointSetOverlay createPointSetOverlay(int width, Color color); /** * Draw the given graph using the given palette. diff --git a/src/main/org/insa/graphics/drawing/MapViewDrawing.java b/src/main/org/insa/graphics/drawing/MapViewDrawing.java index 7c13a3a..ad23779 100644 --- a/src/main/org/insa/graphics/drawing/MapViewDrawing.java +++ b/src/main/org/insa/graphics/drawing/MapViewDrawing.java @@ -13,6 +13,7 @@ import org.insa.graphics.drawing.overlays.MarkerOverlay; import org.insa.graphics.drawing.overlays.MarkerUtils; import org.insa.graphics.drawing.overlays.Overlay; import org.insa.graphics.drawing.overlays.PathOverlay; +import org.insa.graphics.drawing.overlays.PointSetOverlay; import org.insa.graphics.drawing.overlays.PolylineAutoScaling; import org.mapsforge.core.graphics.Bitmap; import org.mapsforge.core.graphics.GraphicFactory; @@ -65,6 +66,9 @@ public class MapViewDrawing extends MapView implements Drawing { @Override public boolean isVisible() { + if (this.layers.length == 0) { + return true; + } return this.layers[0].isVisible(); } @@ -123,6 +127,51 @@ public class MapViewDrawing extends MapView implements Drawing { } + private class MapViewPointSetOverlay extends MapViewOverlay implements PointSetOverlay { + + public MapViewPointSetOverlay() { + super(new Layer[0]); + } + + @Override + public void setColor(Color color) { + } + + @Override + public void setWidth(int width) { + } + + @Override + public void setWidthAndColor(int width, Color color) { + setWidth(width); + setColor(color); + } + + @Override + public void addPoint(Point point) { + } + + @Override + public void addPoint(Point point, int width) { + setWidth(width); + addPoint(point); + } + + @Override + public void addPoint(Point point, Color color) { + setColor(color); + addPoint(point); + } + + @Override + public void addPoint(Point point, int width, Color color) { + setWidth(width); + setColor(color); + addPoint(point); + } + + }; + // Default path color. public static final Color DEFAULT_PATH_COLOR = new Color(66, 134, 244); @@ -189,19 +238,21 @@ public class MapViewDrawing extends MapView implements Drawing { return new Marker(convertPoint(point), bitmap, 0, -bitmap.getHeight() / 2); } - @Override - public MarkerOverlay drawMarker(Point point) { - return drawMarker(point, Color.GREEN); - } - @Override public MarkerOverlay drawMarker(Point point, Color color) { return new MapViewMarkerOverlay(createMarker(point, color), color); } @Override - public void drawPoint(Point point, int width, Color color) { - // TODO: + public PointSetOverlay createPointSetOverlay() { + return new MapViewPointSetOverlay(); + } + + @Override + public PointSetOverlay createPointSetOverlay(int width, Color color) { + PointSetOverlay ps = new MapViewPointSetOverlay(); + ps.setWidthAndColor(width, color); + return ps; } public void drawGraph(File file) { @@ -229,12 +280,12 @@ public class MapViewDrawing extends MapView implements Drawing { @Override public void drawGraph(Graph graph, GraphPalette palette) { - // TODO: Unimplemented for now... + // drawGraph(graph, null); } @Override public void drawGraph(Graph graph) { - drawGraph(graph, null); + // drawGraph(graph, null); } @Override diff --git a/src/main/org/insa/graphics/drawing/overlays/PointSetOverlay.java b/src/main/org/insa/graphics/drawing/overlays/PointSetOverlay.java new file mode 100644 index 0000000..d34c71c --- /dev/null +++ b/src/main/org/insa/graphics/drawing/overlays/PointSetOverlay.java @@ -0,0 +1,64 @@ +package org.insa.graphics.drawing.overlays; + +import java.awt.Color; + +import org.insa.graph.Point; + +public interface PointSetOverlay extends Overlay { + + /** + * Set the color of this overlay for future addPoint(). + * + * @param color + */ + public void setColor(Color color); + + /** + * Set the width of this overlay for future addPoint(). + * + * @param width + */ + public void setWidth(int width); + + /** + * Set color and width for this overlay for future addPoint(). + * + * @param width + * @param color + */ + public void setWidthAndColor(int width, Color color); + + /** + * Add a new point using the current width and color. + * + * @param point + */ + public void addPoint(Point point); + + /** + * Set the current width and then add a new point. + * + * @param point + * @param width + */ + public void addPoint(Point point, int width); + + /** + * Set the current color and then add a new point. + * + * @param point + * @param color + */ + public void addPoint(Point point, Color color); + + /** + * Add a new point to this set at the given location, with the given color and + * width, and update the current color. + * + * @param point + * @param width + * @param color + */ + public void addPoint(Point point, int width, Color color); + +}