Improve overlay management for BasicDrawing.

This commit is contained in:
Holt59 2018-02-25 12:44:15 +01:00
parent 481e30e80c
commit 9acdc998b3
3 changed files with 72 additions and 31 deletions

View file

@ -11,6 +11,7 @@ import java.awt.event.MouseListener;
import java.awt.geom.NoninvertibleTransformException; import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D; import java.awt.geom.Point2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.IdentityHashMap; import java.util.IdentityHashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -23,6 +24,7 @@ import org.insa.graph.Graph;
import org.insa.graph.Node; import org.insa.graph.Node;
import org.insa.graph.Path; import org.insa.graph.Path;
import org.insa.graph.Point; import org.insa.graph.Point;
import org.insa.graphics.drawing.utils.MarkerUtils;
/** /**
* Cette implementation de la classe Dessin produit vraiment un affichage (au * Cette implementation de la classe Dessin produit vraiment un affichage (au
@ -41,12 +43,12 @@ public class BasicDrawing extends JPanel implements Drawing {
// Point of the marker. // Point of the marker.
private Point point; private Point point;
// Color of the "marker". // Image of the marker
private Color color; protected BufferedImage image;
public BasicMarkerTracker(Point point, Color color) { public BasicMarkerTracker(Point point, BufferedImage image) {
this.point = point; this.point = point;
this.color = color; this.image = image;
} }
@Override @Override
@ -56,11 +58,14 @@ public class BasicDrawing extends JPanel implements Drawing {
@Override @Override
public void moveTo(Point point) { public void moveTo(Point point) {
BasicDrawing.this.drawMarker(point, color); this.point = point;
BasicDrawing.this.repaint();
} }
@Override @Override
public void delete() { public void delete() {
BasicDrawing.this.markers.remove(this);
BasicDrawing.this.repaint();
} }
}; };
@ -74,18 +79,24 @@ public class BasicDrawing extends JPanel implements Drawing {
// Default marker width // Default marker width
private static final int DEFAULT_MARKER_WIDTH = 10; private static final int DEFAULT_MARKER_WIDTH = 10;
//
private final Graphics2D gr;
private double long1, long2, lat1, lat2; private double long1, long2, lat1, lat2;
// Width and height of the image // Width and height of the image
private final int width, height; private final int width, height;
//
private Image image;
private ZoomAndPanListener zoomAndPanListener; private ZoomAndPanListener zoomAndPanListener;
//
private Image graphImage;
private final Graphics2D graphGraphics;
// Image for path / points
private Image overlayImage;
private Graphics2D overlayGraphics;
// List of image for markers
private List<BasicMarkerTracker> markers = new ArrayList<>();
// Mapping DrawingClickListener -> MouseEventListener // Mapping DrawingClickListener -> MouseEventListener
private Map<DrawingClickListener, MouseListener> listenerMapping = new IdentityHashMap<>(); private Map<DrawingClickListener, MouseListener> listenerMapping = new IdentityHashMap<>();
@ -104,11 +115,16 @@ public class BasicDrawing extends JPanel implements Drawing {
this.height = 1600; this.height = 1600;
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.graphGraphics = img.createGraphics();
this.graphGraphics.setBackground(Color.WHITE);
this.image = img; img = new BufferedImage(this.width, this.height, BufferedImage.TYPE_4BYTE_ABGR);
this.gr = img.createGraphics(); this.overlayImage = img;
this.overlayGraphics = img.createGraphics();
this.overlayGraphics.setBackground(new Color(0, 0, 0, 0));
this.zoomAndPanListener.setCoordTransform(this.gr.getTransform()); this.zoomAndPanListener.setCoordTransform(this.graphGraphics.getTransform());
this.long1 = -180; this.long1 = -180;
this.long2 = 180; this.long2 = 180;
@ -125,7 +141,20 @@ public class BasicDrawing extends JPanel implements Drawing {
Graphics2D g = (Graphics2D) g1; Graphics2D g = (Graphics2D) g1;
g.clearRect(0, 0, getWidth(), getHeight()); g.clearRect(0, 0, getWidth(), getHeight());
g.setTransform(zoomAndPanListener.getCoordTransform()); g.setTransform(zoomAndPanListener.getCoordTransform());
g.drawImage(image, 0, 0, this);
// Draw graph
g.drawImage(graphImage, 0, 0, this);
// Draw overlays (path, etc.)
g.drawImage(overlayImage, 0, 0, this);
// Draw markers
for (BasicMarkerTracker mtracker: markers) {
BufferedImage img = mtracker.image;
int px = this.projx(mtracker.getPoint().getLongitude());
int py = this.projy(mtracker.getPoint().getLatitude());
g.drawImage(img, px - img.getWidth() / 2, py - img.getHeight(), this);
}
} }
protected void setBB(double long1, double long2, double lat1, double lat2) { protected void setBB(double long1, double long2, double lat1, double lat2) {
@ -206,17 +235,18 @@ public class BasicDrawing extends JPanel implements Drawing {
} }
protected void setWidth(int width) { protected void setWidth(int width) {
this.gr.setStroke(new BasicStroke(width)); this.overlayGraphics.setStroke(new BasicStroke(width));
} }
protected void setColor(Color col) { protected void setColor(Color col) {
this.gr.setColor(col); this.overlayGraphics.setColor(col);
} }
@Override @Override
public void clear() { public void clear() {
this.gr.setColor(Color.WHITE); this.graphGraphics.clearRect(0, 0, this.width, this.height);
this.gr.fillRect(0, 0, this.width, this.height); this.overlayGraphics.clearRect(0, 0, this.width, this.height);
this.markers.clear();
} }
@Override @Override
@ -226,7 +256,7 @@ public class BasicDrawing extends JPanel implements Drawing {
int y1 = this.projy(from.getLatitude()); int y1 = this.projy(from.getLatitude());
int y2 = this.projy(to.getLatitude()); int y2 = this.projy(to.getLatitude());
gr.drawLine(x1, y1, x2, y2); overlayGraphics.drawLine(x1, y1, x2, y2);
this.repaint(); this.repaint();
} }
@ -245,14 +275,24 @@ public class BasicDrawing extends JPanel implements Drawing {
@Override @Override
public MarkerTracker drawMarker(Point point) { public MarkerTracker drawMarker(Point point) {
drawPoint(point, DEFAULT_MARKER_WIDTH, this.gr.getColor()); return drawMarker(point, this.overlayGraphics.getColor());
return new BasicMarkerTracker(point, this.gr.getColor());
} }
@Override @Override
public MarkerTracker drawMarker(Point point, Color color) { public MarkerTracker drawMarker(Point point, Color color) {
setColor(color); /*
return drawMarker(point); * BufferedImage img = new BufferedImage(DEFAULT_MARKER_WIDTH,
* DEFAULT_MARKER_WIDTH, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D gr =
* img.createGraphics(); gr.setColor(color); gr.fillOval(0, 0,
* DEFAULT_MARKER_WIDTH, DEFAULT_MARKER_WIDTH);
*/
BufferedImage img = MarkerUtils.getMarkerForColor(color);
Graphics2D gr = img.createGraphics();
gr.scale(0.3, 0.3);
BasicMarkerTracker marker = new BasicMarkerTracker(point, img);
this.markers.add(marker);
this.repaint();
return marker;
} }
@Override @Override
@ -261,7 +301,7 @@ public class BasicDrawing extends JPanel implements Drawing {
setColor(color); setColor(color);
int x = this.projx(point.getLongitude()) - DEFAULT_MARKER_WIDTH / 2; int x = this.projx(point.getLongitude()) - DEFAULT_MARKER_WIDTH / 2;
int y = this.projy(point.getLatitude()) - DEFAULT_MARKER_WIDTH / 2; int y = this.projy(point.getLatitude()) - DEFAULT_MARKER_WIDTH / 2;
gr.fillOval(x, y, DEFAULT_MARKER_WIDTH, DEFAULT_MARKER_WIDTH); overlayGraphics.fillOval(x, y, DEFAULT_MARKER_WIDTH, DEFAULT_MARKER_WIDTH);
this.repaint(); this.repaint();
} }
@ -321,12 +361,15 @@ public class BasicDrawing extends JPanel implements Drawing {
@Override @Override
public void drawGraph(Graph graph, GraphPalette palette) { public void drawGraph(Graph graph, GraphPalette palette) {
clear(); clear();
Graphics2D oldOverlayGraphics = this.overlayGraphics;
this.overlayGraphics = this.graphGraphics;
initialize(graph); initialize(graph);
for (Node node: graph.getNodes()) { for (Node node: graph.getNodes()) {
for (Arc arc: node.getSuccessors()) { for (Arc arc: node.getSuccessors()) {
drawArc(arc, palette); drawArc(arc, palette);
} }
} }
this.overlayGraphics = oldOverlayGraphics;
} }
@Override @Override
@ -366,7 +409,7 @@ public class BasicDrawing extends JPanel implements Drawing {
private void putText(Point point, String txt) { private void putText(Point point, String txt) {
int x = this.projx(point.getLongitude()); int x = this.projx(point.getLongitude());
int y = this.projy(point.getLatitude()); int y = this.projy(point.getLatitude());
gr.drawString(txt, x, y); graphGraphics.drawString(txt, x, y);
this.repaint(); this.repaint();
} }

View file

@ -22,6 +22,7 @@ import org.mapsforge.core.model.BoundingBox;
import org.mapsforge.core.model.LatLong; import org.mapsforge.core.model.LatLong;
import org.mapsforge.core.model.MapPosition; import org.mapsforge.core.model.MapPosition;
import org.mapsforge.core.util.LatLongUtils; import org.mapsforge.core.util.LatLongUtils;
import org.mapsforge.map.awt.graphics.AwtBitmap;
import org.mapsforge.map.awt.graphics.AwtGraphicFactory; 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;
@ -196,7 +197,7 @@ public class MapViewDrawing extends MapView implements Drawing {
@Override @Override
public MarkerTracker drawMarker(Point point, Color color) { public MarkerTracker drawMarker(Point point, Color color) {
Bitmap bitmap = MarkerUtils.getMarkerForColor(color); Bitmap bitmap = new AwtBitmap(MarkerUtils.getMarkerForColor(color));
Marker marker = new Marker(convertPoint(point), bitmap, 0, -bitmap.getHeight() / 2); Marker marker = new Marker(convertPoint(point), bitmap, 0, -bitmap.getHeight() / 2);
getLayerManager().getLayers().add(marker); getLayerManager().getLayers().add(marker);
return new MapViewMarkerTracker(marker); return new MapViewMarkerTracker(marker);

View file

@ -3,9 +3,6 @@ package org.insa.graphics.drawing.utils;
import java.awt.Color; import java.awt.Color;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import org.mapsforge.core.graphics.Bitmap;
import org.mapsforge.map.awt.graphics.AwtBitmap;
public class MarkerUtils { public class MarkerUtils {
/** /**
@ -24,7 +21,7 @@ public class MarkerUtils {
* @param color * @param color
* @return * @return
*/ */
public static Bitmap getMarkerForColor(Color color) { public static BufferedImage getMarkerForColor(Color color) {
// create image // create image
BufferedImage image = new BufferedImage(MARKER_MASK[0].length, MARKER_MASK.length, BufferedImage.TYPE_INT_ARGB); BufferedImage image = new BufferedImage(MARKER_MASK[0].length, MARKER_MASK.length, BufferedImage.TYPE_INT_ARGB);
@ -36,7 +33,7 @@ public class MarkerUtils {
} }
// Create Bitmap and return it. // Create Bitmap and return it.
return new AwtBitmap(image); return image;
} }
// Mask // Mask