-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.scenegraph.g2d.nodes;\r
-\r
-import java.awt.Graphics2D;\r
-import java.awt.geom.AffineTransform;\r
-import java.awt.geom.Rectangle2D;\r
-import java.awt.image.BufferedImage;\r
-import java.io.ByteArrayInputStream;\r
-import java.io.IOException;\r
-\r
-import javax.imageio.ImageIO;\r
-\r
-import org.simantics.scenegraph.g2d.G2DNode;\r
-\r
-public class ImageNode extends G2DNode {\r
-\r
- private static final long serialVersionUID = -4982578347433716440L;\r
-\r
- protected Boolean visible = Boolean.TRUE;\r
- protected BufferedImage img = null;\r
-\r
- @SyncField("visible")\r
- public void setVisible(Boolean visible) {\r
- this.visible = visible;\r
- }\r
-\r
- public boolean isVisible() {\r
- return visible;\r
- }\r
-\r
- @ClientSide\r
- public void setImage(byte[] buf) {\r
- try {\r
- img = ImageIO.read(new ByteArrayInputStream(buf));\r
- } catch (IOException e) {\r
- }\r
- }\r
-\r
- public void setImage(BufferedImage src) {\r
- img = src;\r
- }\r
-\r
- @Override\r
- public void render(Graphics2D g) {\r
- if (!visible || img == null) return;\r
-\r
- AffineTransform ot = null;\r
- if (!transform.isIdentity()) {\r
- ot = g.getTransform();\r
- g.transform(transform);\r
- }\r
-\r
-// G2DParentNode parent = (G2DParentNode) getParent();\r
-// if(parent != null) {\r
-// Rectangle2D b = parent.getBoundsInLocal();\r
-// g.drawImage(img, (int)b.getMinX(), (int)b.getMinY(), (int)b.getWidth()+(int)b.getMinX(), (int)b.getHeight()+(int)b.getMinY(), 0, 0, img.getWidth(), img.getHeight(), null);\r
-// }\r
- g.drawImage(img, 0, 0, null);\r
-\r
- if (ot != null)\r
- g.setTransform(ot);\r
- }\r
-\r
- @Override\r
- public Rectangle2D getBoundsInLocal() {\r
- return new Rectangle2D.Double(img.getMinX(), img.getMinY(), img.getWidth(), img.getHeight());\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.scenegraph.g2d.nodes;
+
+import java.awt.AlphaComposite;
+import java.awt.Composite;
+import java.awt.Graphics2D;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+import javax.imageio.ImageIO;
+
+import org.simantics.scenegraph.g2d.G2DNode;
+
+public class ImageNode extends G2DNode {
+
+ private static final long serialVersionUID = -4982578347433716440L;
+
+ protected Boolean visible = Boolean.TRUE;
+ protected BufferedImage img = null;
+ protected float alpha = 1.0f;
+
+ @SyncField("visible")
+ public void setVisible(Boolean visible) {
+ this.visible = visible;
+ }
+
+ public boolean isVisible() {
+ return visible;
+ }
+
+ @ClientSide
+ public void setImage(byte[] buf) {
+ try {
+ img = ImageIO.read(new ByteArrayInputStream(buf));
+ } catch (IOException e) {
+ }
+ }
+
+ public void setImage(BufferedImage src) {
+ img = src;
+ }
+
+ public void setAlpha(float alpha) {
+ this.alpha = Math.max(0.0f, Math.min(alpha, 1.0f));
+ }
+
+ @Override
+ public void render(Graphics2D g) {
+ if (!visible || img == null) return;
+
+ AffineTransform ot = null;
+ if (!transform.isIdentity()) {
+ ot = g.getTransform();
+ g.transform(transform);
+ }
+
+// G2DParentNode parent = (G2DParentNode) getParent();
+// if(parent != null) {
+// Rectangle2D b = parent.getBoundsInLocal();
+// g.drawImage(img, (int)b.getMinX(), (int)b.getMinY(), (int)b.getWidth()+(int)b.getMinX(), (int)b.getHeight()+(int)b.getMinY(), 0, 0, img.getWidth(), img.getHeight(), null);
+// }
+
+ Composite old = null;
+ if (alpha < 1.0f) {
+ old = g.getComposite();
+ g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
+ }
+
+ g.drawImage(img, 0, 0, null);
+
+ if (old != null)
+ g.setComposite(old);
+ if (ot != null)
+ g.setTransform(ot);
+ }
+
+ @Override
+ public Rectangle2D getBoundsInLocal() {
+ return new Rectangle2D.Double(img.getMinX(), img.getMinY(), img.getWidth(), img.getHeight());
+ }
+
+}