Improve arc memory management.

This commit is contained in:
Mikael Capelle
2018-03-05 17:58:59 +01:00
parent be1176c1e3
commit 98aba8dd06
9 changed files with 210 additions and 81 deletions

View File

@@ -21,6 +21,7 @@ import java.util.List;
import javax.swing.JPanel;
import org.insa.graph.ArcForward;
import org.insa.graph.Arc;
import org.insa.graph.Graph;
import org.insa.graph.Node;
@@ -132,8 +133,8 @@ public class BasicDrawing extends JPanel implements Drawing {
int px = BasicDrawing.this.projx(getPoint().getLongitude());
int py = BasicDrawing.this.projy(getPoint().getLatitude());
graphics.drawImage(this.image, px - MARKER_WIDTH / 2, py - MARKER_HEIGHT, MARKER_WIDTH, MARKER_HEIGHT,
BasicDrawing.this);
graphics.drawImage(this.image, px - MARKER_WIDTH / 2, py - MARKER_HEIGHT, MARKER_WIDTH,
MARKER_HEIGHT, BasicDrawing.this);
}
};
@@ -282,7 +283,8 @@ public class BasicDrawing extends JPanel implements Drawing {
private Graphics2D graphGraphics = null;
// List of image for markers
private List<BasicOverlay> overlays = Collections.synchronizedList(new ArrayList<BasicOverlay>());
private List<BasicOverlay> overlays = Collections
.synchronizedList(new ArrayList<BasicOverlay>());
// Mapping DrawingClickListener -> MouseEventListener
private List<DrawingClickListener> drawingClickListeners = new ArrayList<>();
@@ -295,11 +297,13 @@ public class BasicDrawing extends JPanel implements Drawing {
*
*/
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);
// Try...
try {
this.zoomControls = new MapZoomControls(this, 0, ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20);
this.zoomControls = new MapZoomControls(this, 0,
ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20);
this.zoomControls.addZoomInListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@@ -394,7 +398,8 @@ public class BasicDrawing extends JPanel implements Drawing {
// Get the point using the inverse transform of the Zoom/Pan object, this gives
// us
// 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.
double lon = ptDst.getX();
@@ -403,7 +408,7 @@ public class BasicDrawing extends JPanel implements Drawing {
lat = (1 - lat / this.height) * (this.lat2 - this.lat1) + this.lat1;
// Return a new point.
return new Point(lon, lat);
return new Point((float) lon, (float) lat);
}
@Override
@@ -461,9 +466,9 @@ public class BasicDrawing extends JPanel implements Drawing {
*
* @param arc Arc to draw.
* @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, boolean repaint) {
protected void drawArc(ArcForward arc, GraphPalette palette, boolean repaint) {
List<Point> pts = arc.getPoints();
if (!pts.isEmpty()) {
if (palette != null) {
@@ -500,8 +505,8 @@ public class BasicDrawing extends JPanel implements Drawing {
this.clear();
// Find minimum/maximum longitude and latitude.
double minLon = Double.POSITIVE_INFINITY, minLat = Double.POSITIVE_INFINITY, maxLon = Double.NEGATIVE_INFINITY,
maxLat = Double.NEGATIVE_INFINITY;
double minLon = Double.POSITIVE_INFINITY, minLat = Double.POSITIVE_INFINITY,
maxLon = Double.NEGATIVE_INFINITY, maxLat = Double.NEGATIVE_INFINITY;
for (Node node: graph.getNodes()) {
Point pt = node.getPoint();
if (pt.getLatitude() < minLat) {
@@ -539,7 +544,8 @@ public class BasicDrawing extends JPanel implements Drawing {
}
// 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.graphGraphics = img.createGraphics();
this.graphGraphics.setBackground(Color.WHITE);
@@ -547,10 +553,12 @@ public class BasicDrawing extends JPanel implements Drawing {
// 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.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.zoomAndPanListener.getCoordTransform().scale(scale, scale);
this.zoomAndPanListener.setZoomLevel(0);
@@ -575,8 +583,8 @@ public class BasicDrawing extends JPanel implements Drawing {
for (Node node: graph.getNodes()) {
for (Arc arc: node.getSuccessors()) {
if (arc.getRoadInformation().isOneWay() || arc.getOrigin().compareTo(arc.getDestination()) < 0) {
drawArc(arc, palette, false);
if (arc instanceof ArcForward) { // draw only "true" arcs
drawArc((ArcForward) arc, palette, false);
}
}
if (node.getId() % repaintModulo == 0) {