Revision: 1181
Author:   heuermh
Date:     Fri Aug 31 16:06:53 2012
Log: Issue 189 ; adding transparency support for SWTGraphics2D and PSWTPath
http://code.google.com/p/piccolo2d/source/detail?r=1181

Modified:
/piccolo2d.java/trunk/swt/src/main/java/org/piccolo2d/extras/swt/PSWTPath.java /piccolo2d.java/trunk/swt/src/main/java/org/piccolo2d/extras/swt/SWTGraphics2D.java /piccolo2d.java/trunk/swt-examples/src/main/java/org/piccolo2d/extras/swt/examples/SWTBasicExample.java

=======================================
--- /piccolo2d.java/trunk/swt/src/main/java/org/piccolo2d/extras/swt/PSWTPath.java Tue Mar 15 15:23:53 2011 +++ /piccolo2d.java/trunk/swt/src/main/java/org/piccolo2d/extras/swt/PSWTPath.java Fri Aug 31 16:06:53 2012
@@ -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 <code>transparency</code>.
+     *
+ * @param transparency transparency, must be between <code>0.0f</code> and <code>1.0f</code> 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;
+    }
 }
=======================================
--- /piccolo2d.java/trunk/swt/src/main/java/org/piccolo2d/extras/swt/SWTGraphics2D.java Tue Mar 15 15:23:53 2011 +++ /piccolo2d.java/trunk/swt/src/main/java/org/piccolo2d/extras/swt/SWTGraphics2D.java Fri Aug 31 16:06:53 2012
@@ -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, <code>0.0f &lt;= transparency &lt;= 1.0f</code>. */
+    private float transparency = DEFAULT_TRANSPARENCY;

     /**
      * Constructor for SWTGraphics2D.
@@ -1161,6 +1164,28 @@

         return (int) (Math.max(TEMP_LINE_RECT.getWidth(), 1) + 0.5);
     }
+
+    /**
+     * 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 <code>transparency</code>.
+     *
+ * @param transparency transparency, must be between <code>0.0f</code> and <code>1.0f</code> 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.
=======================================
--- /piccolo2d.java/trunk/swt-examples/src/main/java/org/piccolo2d/extras/swt/examples/SWTBasicExample.java Tue Mar 15 15:23:53 2011 +++ /piccolo2d.java/trunk/swt-examples/src/main/java/org/piccolo2d/extras/swt/examples/SWTBasicExample.java Fri Aug 31 16:06:53 2012
@@ -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);

--
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en

Reply via email to