1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.g2d.nodes;
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;
23 import javax.imageio.ImageIO;
25 import org.simantics.scenegraph.g2d.G2DNode;
27 public class ImageNode extends G2DNode {
29 private static final long serialVersionUID = -4982578347433716440L;
31 protected Boolean visible = Boolean.TRUE;
32 protected BufferedImage img = null;
33 protected float alpha = 1.0f;
36 public void setVisible(Boolean visible) {
37 this.visible = visible;
40 public boolean isVisible() {
45 public void setImage(byte[] buf) {
47 img = ImageIO.read(new ByteArrayInputStream(buf));
48 } catch (IOException e) {
52 public void setImage(BufferedImage src) {
56 public void setAlpha(float alpha) {
57 this.alpha = Math.max(0.0f, Math.min(alpha, 1.0f));
61 public void render(Graphics2D g) {
62 if (!visible || img == null) return;
64 AffineTransform ot = null;
65 if (!transform.isIdentity()) {
66 ot = g.getTransform();
67 g.transform(transform);
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);
78 old = g.getComposite();
79 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
82 g.drawImage(img, 0, 0, null);
91 public Rectangle2D getBoundsInLocal() {
92 return new Rectangle2D.Double(img.getMinX(), img.getMinY(), img.getWidth(), img.getHeight());