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.g2d.chassis;
14 import java.awt.Color;
15 import java.awt.Graphics2D;
16 import java.awt.Image;
17 import java.awt.geom.Point2D;
18 import java.awt.geom.Rectangle2D;
20 import org.simantics.g2d.canvas.ICanvasContext;
21 import org.simantics.g2d.canvas.IContentContext;
22 import org.simantics.g2d.canvas.IContentContext.IContentListener;
23 import org.simantics.scenegraph.g2d.G2DRenderingHints;
26 * Renders canvas to an image.
28 * @author Toni Kalajainen
30 public class ImageChassis extends AbstractChassis {
32 public static final Color WIPE_COLOR = new Color(.0f, 0.f, 0.f, .0f);
34 private Color backgroundColor;
38 boolean dirty = false;
40 public ImageChassis(Image image) {
44 public void setBackgroundColor(Color color) {
45 backgroundColor = color;
48 public Point2D getSize() {
49 return new Point2D.Double(image.getWidth(null), image.getHeight(null));
52 public Rectangle2D getBounds() {
53 return new Rectangle2D.Double(0,0,image.getWidth(null), image.getHeight(null));
57 public void setCanvasContext(ICanvasContext canvasContext) {
58 super.setCanvasContext(canvasContext);
60 canvasContext.getContentContext().addPaintableContextListener(new IContentListener() {
62 public void onDirty(IContentContext sender) {
72 if (canvasContext.isLocked())
73 throw new IllegalStateException("cannot draw image, canvas context is locked: " + canvasContext);
76 Graphics2D g2d = (Graphics2D) image.getGraphics();
78 g2d.setRenderingHint(G2DRenderingHints.KEY_CONTROL_BOUNDS, getBounds());
79 if(backgroundColor != null) {
80 Rectangle2D bounds = getBounds();
81 g2d.setBackground(backgroundColor);
82 g2d.clearRect(0, 0, (int)bounds.getWidth(), (int)bounds.getHeight() );
84 canvasContext.getSceneGraph().render(g2d);