]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/ImageNode.java
Merge "JsonNode support with Data/Json"
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / ImageNode.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.scenegraph.g2d.nodes;
13
14 import java.awt.AlphaComposite;
15 import java.awt.Composite;
16 import java.awt.Graphics2D;
17 import java.awt.geom.AffineTransform;
18 import java.awt.geom.Rectangle2D;
19 import java.awt.image.BufferedImage;
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22
23 import javax.imageio.ImageIO;
24
25 import org.simantics.scenegraph.g2d.G2DNode;
26
27 public class ImageNode extends G2DNode {
28
29     private static final long serialVersionUID = -4982578347433716440L;
30
31     protected Boolean visible = Boolean.TRUE;
32     protected BufferedImage img = null;
33     protected float alpha = 1.0f;
34
35     @SyncField("visible")
36     public void setVisible(Boolean visible) {
37         this.visible = visible;
38     }
39
40     public boolean isVisible() {
41         return visible;
42     }
43
44     @ClientSide
45     public void setImage(byte[] buf) {
46         try {
47             img = ImageIO.read(new ByteArrayInputStream(buf));
48         } catch (IOException e) {
49         }
50     }
51
52     public void setImage(BufferedImage src) {
53         img = src;
54     }
55
56     public void setAlpha(float alpha) {
57         this.alpha = Math.max(0.0f, Math.min(alpha, 1.0f));
58     }
59
60     @Override
61     public void render(Graphics2D g) {
62         if (!visible || img == null) return;
63
64         AffineTransform ot = null;
65         if (!transform.isIdentity()) {
66             ot = g.getTransform();
67             g.transform(transform);
68         }
69
70 //        G2DParentNode parent = (G2DParentNode) getParent();
71 //        if(parent != null) {
72 //            Rectangle2D b = parent.getBoundsInLocal();
73 //            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);
74 //        }
75
76         Composite old = null;
77         if (alpha < 1.0f) {
78             old = g.getComposite();
79             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
80         }
81
82         g.drawImage(img, 0, 0, null);
83
84         if (old != null)
85             g.setComposite(old);
86         if (ot != null)
87             g.setTransform(ot);
88     }
89
90     @Override
91     public Rectangle2D getBoundsInLocal() {
92         return new Rectangle2D.Double(img.getMinX(), img.getMinY(), img.getWidth(), img.getHeight());
93     }
94
95 }