1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.g2d.nodes;
14 import java.awt.Graphics2D;
15 import java.awt.Paint;
16 import java.awt.Shape;
17 import java.awt.geom.AffineTransform;
18 import java.awt.geom.Rectangle2D;
21 * A scene graph node that renders multiple separately transformed instances of
22 * a {@link Shape} by optionally filling or stroking it.
24 * If filling is enabled, it will be performed before stroking the shape.
26 * @author Tuukka Lehtonen
28 public class InstancingShapeNode extends ShapeNode {
30 private static final long serialVersionUID = -681327976554446434L;
32 AffineTransform[] transforms = {};
35 @SyncField({"instances", "paints"})
36 public void setInstances(AffineTransform[] transforms, Paint[] paints) {
37 if (transforms.length != paints.length)
38 throw new IllegalArgumentException("transforms.length (" + transforms.length + ") != paints.length (" + paints.length + ")");
40 this.transforms = transforms;
45 public void render(Graphics2D g2d) {
48 if (transforms == null || transforms.length == 0)
53 AffineTransform startTr = g2d.getTransform();
54 Paint startPaint = g2d.getPaint();
56 for (int i = 0; i < transforms.length; ++i) {
57 if (transforms[i] != null)
58 g2d.transform(transforms[i]);
60 if (paints[i] != null)
61 g2d.setPaint(paints[i]);
63 g2d.setPaint(startPaint);
65 renderShape(g2d, shape);
67 g2d.setTransform(startTr);
72 public Rectangle2D getBoundsInLocal() {
75 if (transforms == null || transforms.length == 0)
78 Rectangle2D result = null;
79 Rectangle2D sb = shape.getBounds2D();
80 for (AffineTransform at : transforms) {
81 Rectangle2D instanceBounds = at.createTransformedShape(sb).getBounds2D();
83 result = instanceBounds;
85 Rectangle2D.union(result, instanceBounds, result);