1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.chassis;
\r
14 import java.awt.Color;
\r
15 import java.awt.Graphics2D;
\r
16 import java.awt.Image;
\r
17 import java.awt.geom.Point2D;
\r
18 import java.awt.geom.Rectangle2D;
\r
20 import org.simantics.g2d.canvas.ICanvasContext;
\r
21 import org.simantics.g2d.canvas.IContentContext;
\r
22 import org.simantics.g2d.canvas.IContentContext.IContentListener;
\r
23 import org.simantics.scenegraph.g2d.G2DRenderingHints;
\r
26 * Renders canvas to an image.
\r
28 * @author Toni Kalajainen
\r
30 public class ImageChassis extends AbstractChassis {
\r
32 public static final Color WIPE_COLOR = new Color(.0f, 0.f, 0.f, .0f);
\r
34 private Color backgroundColor;
\r
38 boolean dirty = false;
\r
40 public ImageChassis(Image image) {
\r
44 public void setBackgroundColor(Color color) {
\r
45 backgroundColor = color;
\r
48 public Point2D getSize() {
\r
49 return new Point2D.Double(image.getWidth(null), image.getHeight(null));
\r
52 public Rectangle2D getBounds() {
\r
53 return new Rectangle2D.Double(0,0,image.getWidth(null), image.getHeight(null));
\r
57 public void setCanvasContext(ICanvasContext canvasContext) {
\r
58 super.setCanvasContext(canvasContext);
\r
60 canvasContext.getContentContext().addPaintableContextListener(new IContentListener() {
\r
62 public void onDirty(IContentContext sender) {
\r
71 public void draw() {
\r
72 if (canvasContext.isLocked())
\r
73 throw new IllegalStateException("cannot draw image, canvas context is locked: " + canvasContext);
\r
76 Graphics2D g2d = (Graphics2D) image.getGraphics();
\r
78 g2d.setRenderingHint(G2DRenderingHints.KEY_CONTROL_BOUNDS, getBounds());
\r
79 if(backgroundColor != null) {
\r
80 Rectangle2D bounds = getBounds();
\r
81 g2d.setBackground(backgroundColor);
\r
82 g2d.clearRect(0, 0, (int)bounds.getWidth(), (int)bounds.getHeight() );
\r
84 canvasContext.getSceneGraph().render(g2d);
\r