Merge "ShapeNode with separate stroke and fill paints"
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / image / impl / ShapeImage.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.image.impl;
13
14 import java.awt.Color;
15 import java.awt.Paint;
16 import java.awt.Shape;
17 import java.awt.Stroke;
18 import java.awt.geom.Rectangle2D;
19 import java.util.EnumSet;
20
21 import org.simantics.g2d.image.Image;
22 import org.simantics.scenegraph.Node;
23 import org.simantics.scenegraph.g2d.G2DParentNode;
24 import org.simantics.scenegraph.g2d.nodes.ShapeNode2;
25
26 /**
27  * @author Tuukka Lehtonen
28  */
29 public class ShapeImage extends AbstractImage implements Image {
30
31     private static final EnumSet<Feature> defaultFeats = VECTOR;
32
33     Shape shape;
34     Paint fillPaint;
35     Paint strokePaint;
36     Stroke stroke;
37     EnumSet<Feature> feats;
38     boolean scaleStroke = false;
39
40     public ShapeImage(Shape shape, Paint fill, Stroke stroke) {
41         this(shape, fill, stroke, defaultFeats);
42     }
43
44     public ShapeImage(Shape shape, Paint fill, Stroke stroke, boolean scaleStroke) {
45         this(shape, fill, stroke, fill, scaleStroke, defaultFeats);
46     }
47
48     public ShapeImage(Shape shape, Paint fill, Stroke stroke, EnumSet<Feature> features) {
49         this(shape, fill, stroke, fill, false, features);
50     }
51     public ShapeImage(Shape shape, Paint fill, Stroke stroke, Paint strokeColor, boolean scaleStroke)  {
52          this(shape, fill, stroke, strokeColor, scaleStroke, defaultFeats);
53     }
54
55     public ShapeImage(Shape shape, Paint fill, Stroke stroke, Paint strokeColor, boolean scaleStroke, EnumSet<Feature> features) {
56         this.shape = shape;
57         this.fillPaint = fill;
58         this.strokePaint = strokeColor;
59         this.stroke = stroke;
60         this.scaleStroke = scaleStroke;
61         this.feats = features;
62     }
63
64     @Override
65     public Rectangle2D getBounds() {
66         return shape.getBounds2D();
67     }
68
69     @Override
70     public EnumSet<Feature> getFeatures() {
71         return feats;
72     }
73
74     @Override
75     public Shape getOutline() {
76         return shape;
77     }
78
79     @Override
80     public Node init(G2DParentNode parent) {
81         ShapeNode2 shapeNode = parent.getOrCreateNode("ShapeImage", ShapeNode2.class);
82         shapeNode.setShape(shape);
83         shapeNode.setStroke(stroke);
84         shapeNode.setFillColor(fillPaint);
85         shapeNode.setStrokeColor(strokePaint != null ? strokePaint : Color.BLACK);
86         shapeNode.setScaleStroke(scaleStroke);
87         return shapeNode;
88     }
89
90 };