]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/image/impl/AWTImage.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / image / impl / AWTImage.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.g2d.image.impl;
13
14 import java.awt.Shape;
15 import java.awt.geom.Rectangle2D;
16 import java.awt.image.BufferedImage;
17 import java.util.EnumSet;
18
19 import org.simantics.g2d.image.Image;
20 import org.simantics.g2d.image.ImageUtils;
21 import org.simantics.scenegraph.Node;
22 import org.simantics.scenegraph.g2d.G2DParentNode;
23 import org.simantics.scenegraph.g2d.nodes.ImageNode;
24
25 /**
26  * Adapts {@link BufferedImage} to {@link Image}
27  * 
28  * @See {@link ImageUtils}
29  * @author Toni Kalajainen
30  */
31 public class AWTImage extends AbstractImage implements Image {
32
33     static EnumSet<Feature> feats = EnumSet.noneOf(Feature.class);
34
35     BufferedImage bi;
36     Rectangle2D rect;
37     float alpha;
38
39     public AWTImage(BufferedImage bi, float alpha) {
40         assert(bi!=null);
41         this.bi = bi;
42         this.alpha = alpha;
43         rect = new Rectangle2D.Double(bi.getMinX(),bi.getMinY(),bi.getWidth(), bi.getHeight());
44     }
45
46     public AWTImage(BufferedImage bi) {
47         this(bi, 1.0f);
48     }
49
50     @Override
51     public Rectangle2D getBounds() {
52         return rect;
53     }
54
55     @Override
56     public Shape getOutline() {
57         return rect;
58     }
59
60     @Override
61     public Node init(G2DParentNode parent) {
62         ImageNode node = parent.getOrCreateNode("image", ImageNode.class);
63         node.setImage(bi);
64         node.setAlpha(alpha);
65         node.setZIndex(-100);
66         return node;
67     }
68
69     @Override
70     public EnumSet<Feature> getFeatures() {
71         return feats;
72     }
73
74 }