From: lempinen Date: Tue, 21 Dec 2010 10:17:50 +0000 (+0000) Subject: Reduced memory allocations: X-Git-Tag: 2011-04-05-db-merge-trunk~43 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=88bb8e6968ada963359cba5d27a9ecc53825ff85;p=simantics%2Fsysdyn.git Reduced memory allocations: git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@19185 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/FlowNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/FlowNode.java index d50c89ff..70891941 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/FlowNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/FlowNode.java @@ -15,14 +15,13 @@ import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.RenderingHints; -import java.awt.Shape; import java.awt.Stroke; +import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; import org.simantics.scenegraph.ISelectionPainterNode; import org.simantics.scenegraph.g2d.G2DNode; import org.simantics.scenegraph.utils.NodeUtil; -import org.simantics.utils.datastructures.Pair; public class FlowNode extends G2DNode implements ISelectionPainterNode { @@ -35,6 +34,8 @@ public class FlowNode extends G2DNode implements ISelectionPainterNode { private Rectangle2D beginBounds; private Rectangle2D endBounds; private Boolean toValve; + private transient Path2D lines; + private transient Path2D arrow; @PropertySetter("color") @SyncField("color") @@ -96,41 +97,29 @@ public class FlowNode extends G2DNode implements ISelectionPainterNode { * -In the first case there is no arrow and valve is endBounds * -In the second case there is an arrow and valve is beginBounds */ - - Pair lines = null; - Shape arrow = null; // System.out.println("FlowNode.render toValve = " + toValve); if(toValve) { - - // FIXME: optimize memory allocations - lines = Flows.createLines(false, endBounds, beginBounds); - + lines = Flows.createLines(lines, false, endBounds, beginBounds); } else { - - // FIXME: optimize memory allocations - lines = Flows.createLines(true, beginBounds, endBounds); - arrow = Flows.createArrow(beginBounds, endBounds); - + lines = Flows.createLines(lines, true, beginBounds, endBounds); + arrow = Flows.createArrow(arrow, beginBounds, endBounds); } boolean selected = NodeUtil.isSelected(this, 2); if(selected) { g.setColor(Color.PINK); g.setStroke(STROKE); - g.draw(lines.first); - g.draw(lines.second); + g.draw(lines); if(color != null) g.setColor(color); g.setStroke(stroke); - g.draw(lines.first); - g.draw(lines.second); + g.draw(lines); if(arrow != null) g.fill(arrow); } else { if(color != null) g.setColor(color); if(stroke != null) g.setStroke(stroke); - g.draw(lines.first); - g.draw(lines.second); + g.draw(lines); if(arrow != null) g.fill(arrow); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/Flows.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/Flows.java index b10a14fc..64521600 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/Flows.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/Flows.java @@ -11,12 +11,9 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.elements2.connections; -import java.awt.Shape; import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; -import org.simantics.utils.datastructures.Pair; - public class Flows { public static double ARROW_LENGTH = 3.2; @@ -25,83 +22,73 @@ public class Flows { static final double OFFSET = 1.0; static final double ARROW_OFFSET = 3.2; - public static Path2D createArrow(Rectangle2D tail, Rectangle2D head) { + public static Path2D createArrow(Path2D arrow, Rectangle2D tail, Rectangle2D head) { double x = tail.getCenterX(); double y = tail.getCenterY(); -// Rectangle2D rect = new Rectangle2D.Double(); -// head.getBounds(rect); double cx = head.getCenterX(); double minx = head.getMinX(); double maxx = head.getMaxX(); double miny = head.getMinY(); double maxy = head.getMaxY(); - Path2D path = new Path2D.Double(); + arrow = new Path2D.Double(); // approach from top if (y < miny) { - path.moveTo(cx, miny); - path.lineTo(cx + ARROW_WIDTH, miny - ARROW_LENGTH); - path.lineTo(cx - ARROW_WIDTH, miny - ARROW_LENGTH); + arrow.moveTo(cx, miny); + arrow.lineTo(cx + ARROW_WIDTH, miny - ARROW_LENGTH); + arrow.lineTo(cx - ARROW_WIDTH, miny - ARROW_LENGTH); } // approach from beneath else if (y > maxy) { - path.moveTo(cx, maxy); - path.lineTo(cx + ARROW_WIDTH, maxy + ARROW_LENGTH); - path.lineTo(cx - ARROW_WIDTH, maxy + ARROW_LENGTH); + arrow.moveTo(cx, maxy); + arrow.lineTo(cx + ARROW_WIDTH, maxy + ARROW_LENGTH); + arrow.lineTo(cx - ARROW_WIDTH, maxy + ARROW_LENGTH); } // approach from left else if (x < minx) { - path.moveTo(minx, y); - path.lineTo(minx - ARROW_LENGTH, y - ARROW_WIDTH); - path.lineTo(minx - ARROW_LENGTH, y + ARROW_WIDTH); + arrow.moveTo(minx, y); + arrow.lineTo(minx - ARROW_LENGTH, y - ARROW_WIDTH); + arrow.lineTo(minx - ARROW_LENGTH, y + ARROW_WIDTH); } // approach from right else if (x > maxx) { - path.moveTo(maxx, y); - path.lineTo(maxx + ARROW_LENGTH, y - ARROW_WIDTH); - path.lineTo(maxx + ARROW_LENGTH, y + ARROW_WIDTH); + arrow.moveTo(maxx, y); + arrow.lineTo(maxx + ARROW_LENGTH, y - ARROW_WIDTH); + arrow.lineTo(maxx + ARROW_LENGTH, y + ARROW_WIDTH); } else return null; // FIXME (HN) This is just a quick bugfix, didn't understand the logic completely - path.closePath(); + arrow.closePath(); - return path; + return arrow; } - private static Pair createLines(boolean vertical, double ... coordinates) { - -// lineNode1.setShape(createOffsetLine(vertical, OFFSET, coordinates)); -// lineNode2.setShape(createOffsetLine(vertical, -OFFSET, coordinates)); - return new Pair(createOffsetLine(vertical, OFFSET, coordinates), Flows.createOffsetLine(vertical, -OFFSET, coordinates)); - + private static Path2D createLines(Path2D lines, boolean vertical, double ... coordinates) { + lines = new Path2D.Double(); + createOffsetLine(lines, vertical, OFFSET, coordinates); + createOffsetLine(lines, vertical, -OFFSET, coordinates); + return lines; } - public static Pair createLines(boolean hasArrow, Rectangle2D valve, Rectangle2D node) { - + public static Path2D createLines(Path2D lines, boolean hasArrow, Rectangle2D valve, Rectangle2D node) { double x0 = valve.getCenterX(); double y0 = valve.getCenterY(); double x1 = node.getCenterX(); double y1 = node.getCenterY(); -// System.out.println("FlowNode " + x0 + " " + y0 + " x1 " + x1 + " y1 " + y1); - -// Rectangle2D rect = new Rectangle2D.Double(); -// node.getBounds(rect); - double minY = hasArrow ? node.getMinY() - ARROW_OFFSET : node.getMinY(); double maxY = hasArrow ? node.getMaxY() + ARROW_OFFSET : node.getMaxY(); double minX = hasArrow ? node.getMinX() - ARROW_OFFSET : node.getMinX(); double maxX = hasArrow ? node.getMaxX() + ARROW_OFFSET : node.getMaxX(); -// boolean rotated = ((ValveElement)valve).rotated; boolean rotated = false; if( rotated ) { @@ -111,15 +98,15 @@ public class Flows { y0 -= OFFSET; if(node.getMinX() <= x0 && node.getMaxX() >= x0) { if(y1 > y0) - return createLines(true, y0, x0, minY); + return createLines(lines, true, y0, x0, minY); else - return createLines(true, y0, x0, maxY); + return createLines(lines, true, y0, x0, maxY); } else { if(x1 > x0) - return createLines(true, y0, x0, y1, minX); + return createLines(lines, true, y0, x0, y1, minX); else - return createLines(true, y0, x0, y1, maxX); + return createLines(lines, true, y0, x0, y1, maxX); } } else { @@ -129,23 +116,22 @@ public class Flows { x0 -= OFFSET; if(node.getMinY() <= y0 && node.getMaxY() >= y0) { if(x1 > x0) - return createLines(false, x0, y0, minX); + return createLines(lines, false, x0, y0, minX); else - return createLines(false, x0, y0, maxX); + return createLines(lines, false, x0, y0, maxX); } else { if(y1 > y0) - return createLines(false, x0, y0, x1, minY); + return createLines(lines, false, x0, y0, x1, minY); else - return createLines(false, x0, y0, x1, maxY); + return createLines(lines, false, x0, y0, x1, maxY); } } } - public static Path2D createLine(boolean vertical, double ... coordinates) { - Path2D path = new Path2D.Double(); + public static Path2D createLine(Path2D path, boolean vertical, double ... coordinates) { if(vertical) path.moveTo(coordinates[1], coordinates[0]); else @@ -159,7 +145,7 @@ public class Flows { return path; } - public static Path2D createOffsetLine(boolean vertical, double offset, double ... coordinates) { + public static Path2D createOffsetLine(Path2D path, boolean vertical, double offset, double ... coordinates) { double[] newCoordinats = new double[coordinates.length]; newCoordinats[0] = coordinates[0]; newCoordinats[coordinates.length-1] = coordinates[coordinates.length-1]; @@ -169,7 +155,7 @@ public class Flows { else newCoordinats[i] = coordinates[i]-offset; } - return createLine(vertical, newCoordinats); + return createLine(path, vertical, newCoordinats); } }