diff --git a/swt-examples/src/main/java/org/piccolo2d/extras/swt/examples/SWTBasicExample.java b/swt-examples/src/main/java/org/piccolo2d/extras/swt/examples/SWTBasicExample.java index e228c80..06711eb 100644 --- a/swt-examples/src/main/java/org/piccolo2d/extras/swt/examples/SWTBasicExample.java +++ b/swt-examples/src/main/java/org/piccolo2d/extras/swt/examples/SWTBasicExample.java @@ -67,10 +67,16 @@ circle.setPaint(Color.GREEN); canvas.getLayer().addChild(circle); + circle = PSWTPath.createEllipse(120, 220, 40, 40); + circle.setPaint(Color.GRAY); + circle.setTransparency(0.2f); + canvas.getLayer().addChild(circle); + circle = PSWTPath.createEllipse(400, 400, 75, 150); circle.setPaint(Color.YELLOW); canvas.getLayer().addChild(circle); + PSWTText text = new PSWTText("Hello World\nMultiline"); text.translate(350, 150); text.setPenColor(Color.GRAY); diff --git a/swt/src/main/java/org/piccolo2d/extras/swt/PSWTPath.java b/swt/src/main/java/org/piccolo2d/extras/swt/PSWTPath.java index 1208a1e..06d7b5e 100644 --- a/swt/src/main/java/org/piccolo2d/extras/swt/PSWTPath.java +++ b/swt/src/main/java/org/piccolo2d/extras/swt/PSWTPath.java @@ -73,6 +73,7 @@ private static final Color DEFAULT_STROKE_PAINT = Color.black; private static final BasicStroke BASIC_STROKE = new BasicStroke(); private static final float PEN_WIDTH = 1f; + private static final float DEFAULT_TRANSPARENCY = 1.0f; private Paint strokePaint; @@ -84,6 +85,7 @@ private AffineTransform inverseXForm; private double[] shapePts; + private float transparency = DEFAULT_TRANSPARENCY; /** * Creates a path representing the rectangle provided. @@ -315,6 +317,7 @@ protected void paint(final PPaintContext paintContext) { final Paint p = getPaint(); final SWTGraphics2D g2 = (SWTGraphics2D) paintContext.getGraphics(); + g2.setTransparency(transparency); if (internalXForm != null) { g2.transform(internalXForm); @@ -571,4 +574,25 @@ PBounds bounds = getBoundsReference(); return new Point2D.Double(bounds.x + (bounds.width / 2.0), bounds.y + (bounds.height / 2.0)); } + + /** + * Return the transparency for this SWT path node. + * + * @return the transparency for this SWT path node + */ + public float getTransparency() { + return transparency; + } + + /** + * Set the transparency for this SWT path node to transparency. + * + * @param transparency transparency, must be between 0.0f and 1.0f inclusive + */ + public void setTransparency(final float transparency) { + if ((transparency < 0.0f) || (transparency > 1.0f)) { + throw new IllegalArgumentException("transparency must be between 0.0f and 1.0f inclusive"); + } + this.transparency = transparency; + } } \ No newline at end of file diff --git a/swt/src/main/java/org/piccolo2d/extras/swt/SWTGraphics2D.java b/swt/src/main/java/org/piccolo2d/extras/swt/SWTGraphics2D.java index 9b8e6cd..c9c802f 100644 --- a/swt/src/main/java/org/piccolo2d/extras/swt/SWTGraphics2D.java +++ b/swt/src/main/java/org/piccolo2d/extras/swt/SWTGraphics2D.java @@ -86,6 +86,7 @@ private static final int DEFAULT_FONT_SIZE = 12; private static final boolean DEFAULT_STRING_TRANSPARENCY = true; + private static final float DEFAULT_TRANSPARENCY = 1.0f; /** * The number of Graphics Contexts active as determined by called to @@ -116,7 +117,9 @@ /** The current font to use when drawing text. */ protected org.eclipse.swt.graphics.Font curFont; /** The current stroke width to use when drawing lines. */ - protected double lineWidth = 1.0; + protected double lineWidth = 1.0d; + /** Transparency, 0.0f <= transparency <= 1.0f. */ + private float transparency = DEFAULT_TRANSPARENCY; /** * Constructor for SWTGraphics2D. @@ -1163,6 +1166,28 @@ } /** + * Return the transparency for this graphics context. + * + * @return the transparency for this graphics context + */ + public float getTransparency() { + return transparency; + } + + /** + * Set the transparency for this graphics context to transparency. + * + * @param transparency transparency, must be between 0.0f and 1.0f inclusive + */ + public void setTransparency(final float transparency) { + if ((transparency < 0.0f) || (transparency > 1.0f)) { + throw new IllegalArgumentException("transparency must be between 0.0f and 1.0f inclusive"); + } + this.transparency = transparency; + gc.setAlpha((int) (this.transparency * 255.0f)); + } + + /** * Fills a gradient rectangle of in the direction specified. * * @param x left of resulting rectangle