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