1 package org.simantics.district.network.ui.nodes;
3 import java.awt.BasicStroke;
5 import java.awt.Graphics2D;
6 import java.awt.geom.Line2D;
7 import java.awt.geom.Point2D;
8 import java.awt.geom.Rectangle2D;
11 import org.simantics.g2d.utils.GeometryUtils;
12 import org.simantics.scenegraph.g2d.G2DNode;
14 public class ConnectionLineNode extends G2DNode {
16 public static final String NODE_NAME = "districtNetworkConnection";
18 private static final BasicStroke STROKE = new BasicStroke(1.f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.f, new float[] {4.f, 2.f}, 0.f);
19 private static final Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.ORANGE, Color.CYAN, Color.PINK };
21 private float strokeWidth;
22 private Line2D[] lines;
24 public ConnectionLineNode() {
28 private static final long serialVersionUID = 1L;
31 public Rectangle2D getBoundsInLocal() {
36 public Rectangle2D getBoundsInLocal(boolean b) {
41 public Rectangle2D getBounds() {
45 public void setStrokeWidth(float w) {
49 public void setPoints(List<Point2D> result) {
50 Point2D p0 = DistrictNetworkNodeUtils.calculatePoint2D(result.get(0), null);
51 lines = new Line2D[result.size() - 1];
52 for (int i = 1; i < result.size(); i++)
54 Point2D p = result.get(i);
55 lines[i-1] = p != null ? new Line2D.Double(p0, DistrictNetworkNodeUtils.calculatePoint2D(p, null)) : null;
60 public void render(Graphics2D g2d) {
61 if (lines == null || lines.length == 0)
64 // Keep fixed line width on screen
65 float scaleRecip = (float) GeometryUtils.getScale(g2d.getTransform());
66 g2d.setStroke(GeometryUtils.scaleStroke(STROKE, strokeWidth / scaleRecip));
68 for (int i = 0; i < lines.length; i++) {
69 if (lines[i] != null) {
70 g2d.setColor(colors[i % colors.length]);