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.graphviz.drawable;
14 import java.awt.Canvas;
15 import java.awt.Color;
16 import java.awt.Graphics;
17 import java.awt.Graphics2D;
18 import java.awt.Point;
19 import java.awt.RenderingHints;
20 import java.awt.event.ComponentAdapter;
21 import java.awt.event.ComponentEvent;
22 import java.awt.event.KeyAdapter;
23 import java.awt.event.KeyEvent;
24 import java.awt.event.MouseAdapter;
25 import java.awt.event.MouseEvent;
26 import java.awt.event.MouseWheelEvent;
27 import java.awt.event.MouseWheelListener;
28 import java.awt.geom.AffineTransform;
29 import java.awt.geom.Rectangle2D;
30 import java.awt.image.VolatileImage;
33 * A canvas showing a given Drawable.
35 * @author Hannu Niemist�
37 public class ViewerCanvas extends Canvas {
39 private static final long serialVersionUID = -1094658958968314511L;
41 AffineTransform transform;
47 public ViewerCanvas(Drawable drawable) {
48 this.drawable = drawable;
49 setBackground(Color.WHITE);
50 addKeyListener(new KeyAdapter() {
53 public void keyPressed(KeyEvent e) {
54 if(e.getKeyChar()=='1') {
61 addMouseListener(new MouseAdapter() {
64 public void mousePressed(MouseEvent e) {
68 addMouseMotionListener(new MouseAdapter() {
70 public void mouseDragged(MouseEvent e) {
71 Point cur = e.getPoint();
72 double deltaX = cur.getX()-prev.getX();
73 double deltaY = cur.getY()-prev.getY();
74 transform.preConcatenate(AffineTransform.getTranslateInstance(deltaX, deltaY));
79 addMouseWheelListener(new MouseWheelListener() {
82 public void mouseWheelMoved(MouseWheelEvent e) {
83 if(transform != null) {
84 double scale = Math.exp(-0.1*e.getUnitsToScroll());
85 Point p = e.getPoint();
86 AffineTransform mod = new AffineTransform();
87 mod.translate(p.getX(), p.getY());
88 mod.scale(scale, scale);
89 mod.translate(-p.getX(), -p.getY());
90 transform.preConcatenate(mod);
96 addComponentListener(new ComponentAdapter() {
98 public void componentResized(ComponentEvent e) {
99 if(getWidth() > 0 && getHeight() > 0) {
100 im = createVolatileImage(getWidth(), getHeight());
108 Rectangle2D bounds = drawable.getBounds();
110 Rectangle2D container = getBounds();
111 double scale = Math.min(
112 container.getWidth() / bounds.getWidth(),
113 container.getHeight() / bounds.getHeight());
115 transform = new AffineTransform(
118 container.getX() + container.getWidth() * 0.5 - bounds.getCenterX() * scale,
119 container.getY() + container.getHeight() * 0.5 - bounds.getCenterY() * scale
124 public void paint(Graphics _g) {
126 if (im == null || im.validate(getGraphicsConfiguration()) ==
127 VolatileImage.IMAGE_INCOMPATIBLE) {
128 im = createVolatileImage(getWidth(), getHeight());
131 Graphics2D g = (Graphics2D)im.getGraphics();
133 g.clearRect(0, 0, im.getWidth(null), im.getHeight(null));
135 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
136 g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
137 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
139 if(transform == null)
141 g.setTransform(transform);
143 drawable.draw(g, null);
145 } while(im.contentsLost());
147 _g.drawImage(im, 0, 0, this);
151 public void update(Graphics g) {
155 public AffineTransform getTransform() {