]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/chassis/ImageChassis.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / chassis / ImageChassis.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/chassis/ImageChassis.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/chassis/ImageChassis.java
new file mode 100644 (file)
index 0000000..e0cc06e
--- /dev/null
@@ -0,0 +1,90 @@
+/*******************************************************************************\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.g2d.chassis;\r
+\r
+import java.awt.Color;\r
+import java.awt.Graphics2D;\r
+import java.awt.Image;\r
+import java.awt.geom.Point2D;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.canvas.IContentContext;\r
+import org.simantics.g2d.canvas.IContentContext.IContentListener;\r
+import org.simantics.scenegraph.g2d.G2DRenderingHints;\r
+\r
+/**\r
+ * Renders canvas to an image.\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class ImageChassis extends AbstractChassis {\r
+\r
+    public static final Color WIPE_COLOR = new Color(.0f, 0.f, 0.f, .0f);\r
+\r
+    private Color backgroundColor;\r
+    \r
+    Image image;\r
+\r
+    boolean dirty = false;\r
+\r
+    public ImageChassis(Image image) {\r
+        this.image = image;\r
+    }\r
+    \r
+    public void setBackgroundColor(Color color) {\r
+       backgroundColor = color;\r
+    }\r
+\r
+    public Point2D getSize() {\r
+        return new Point2D.Double(image.getWidth(null), image.getHeight(null));\r
+    }\r
+\r
+    public Rectangle2D getBounds() {\r
+        return new Rectangle2D.Double(0,0,image.getWidth(null), image.getHeight(null));\r
+    }\r
+\r
+    @Override\r
+    public void setCanvasContext(ICanvasContext canvasContext) {\r
+        super.setCanvasContext(canvasContext);\r
+\r
+        canvasContext.getContentContext().addPaintableContextListener(new IContentListener() {\r
+            @Override\r
+            public void onDirty(IContentContext sender) {\r
+                if (dirty) return;\r
+                dirty = true;\r
+                draw();\r
+            }});\r
+\r
+        draw();\r
+    }\r
+\r
+    public void draw() {\r
+        if (canvasContext.isLocked())\r
+            throw new IllegalStateException("cannot draw image, canvas context is locked: " + canvasContext);\r
+\r
+        dirty = false;\r
+        Graphics2D g2d = (Graphics2D) image.getGraphics();\r
+        try {\r
+            g2d.setRenderingHint(G2DRenderingHints.KEY_CONTROL_BOUNDS, getBounds());\r
+               if(backgroundColor != null) {\r
+                       Rectangle2D bounds = getBounds();\r
+                   g2d.setBackground(backgroundColor);\r
+                   g2d.clearRect(0, 0, (int)bounds.getWidth(), (int)bounds.getHeight() );\r
+               }\r
+            canvasContext.getSceneGraph().render(g2d);\r
+        } finally {\r
+            g2d.dispose();\r
+        }\r
+    }\r
+\r
+}\r