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.routing.test;
14 import java.awt.BasicStroke;
15 import java.awt.Color;
16 import java.awt.Frame;
17 import java.awt.Graphics;
18 import java.awt.Graphics2D;
19 import java.awt.Image;
20 import java.awt.RenderingHints;
21 import java.awt.Stroke;
22 import java.awt.event.KeyEvent;
23 import java.awt.event.KeyListener;
24 import java.awt.event.MouseEvent;
25 import java.awt.event.MouseListener;
26 import java.awt.event.MouseMotionListener;
27 import java.awt.event.WindowEvent;
28 import java.awt.event.WindowListener;
29 import java.awt.geom.Ellipse2D;
30 import java.awt.geom.Path2D;
31 import java.awt.geom.Rectangle2D;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.List;
38 import org.simantics.g2d.routing.Connection;
39 import org.simantics.g2d.routing.Obstacle;
40 import org.simantics.g2d.routing.algorithm1.RouterOld;
42 public class TestRouter extends Frame {
44 private static final long serialVersionUID = 2181877722124429003L;
46 List<Obstacle> obstacles = new ArrayList<Obstacle>();
47 List<Connection> connections = new ArrayList<Connection>();
48 Connection connection;
50 RouterOld router = new RouterOld();
52 double startX, startY;
57 addWindowListener(new WindowListener() {
60 public void windowActivated(WindowEvent e) {
64 public void windowClosed(WindowEvent e) {
68 public void windowClosing(WindowEvent e) {
73 public void windowDeactivated(WindowEvent e) {
77 public void windowDeiconified(WindowEvent e) {
81 public void windowIconified(WindowEvent e) {
85 public void windowOpened(WindowEvent e) {
90 addMouseListener(new MouseListener() {
93 public void mouseClicked(MouseEvent e) {
94 // TODO Auto-generated method stub
99 public void mouseEntered(MouseEvent e) {
100 // TODO Auto-generated method stub
105 public void mouseExited(MouseEvent e) {
106 // TODO Auto-generated method stub
111 public void mousePressed(MouseEvent e) {
112 mouseButtons = e.getButton();
113 curX = startX = e.getX();
114 curY = startY = e.getY();
115 if(e.getButton() == MouseEvent.BUTTON3) {
116 if(connection == null)
117 connection = new Connection(new double[0], 0xf, 0xf);
118 connection.routePath =
119 Arrays.copyOf(connection.routePath, connection.routePath.length+2);
120 connection.routePath[connection.routePath.length-2] = curX;
121 connection.routePath[connection.routePath.length-1] = curY;
127 public void mouseReleased(MouseEvent e) {
129 if(e.getButton() == MouseEvent.BUTTON1) {
132 double x1 = e.getX();
133 double y1 = e.getY();
144 Obstacle obs = new Obstacle(new Rectangle2D.Double(x0, y0, x1-x0, y1-y0));
146 router.addObstacle(obs);
154 addMouseMotionListener(new MouseMotionListener() {
157 public void mouseDragged(MouseEvent e) {
164 public void mouseMoved(MouseEvent e) {
167 if(connection != null)
173 addKeyListener(new KeyListener() {
176 public void keyPressed(KeyEvent e) {
177 if(e.getKeyChar() == 'c') {
178 if(connection != null) {
179 connection.routePath =
180 Arrays.copyOf(connection.routePath, connection.routePath.length+2);
181 connection.routePath[connection.routePath.length-2] = curX;
182 connection.routePath[connection.routePath.length-1] = curY;
184 connections.add(connection);
185 router.addConnection(connection);
193 public void keyReleased(KeyEvent e) {
194 // TODO Auto-generated method stub
199 public void keyTyped(KeyEvent e) {
200 // TODO Auto-generated method stub
211 public void paint(Graphics _g) {
212 final Graphics2D g = (Graphics2D)_g;
213 Map<Object, Object> hints = new HashMap<Object, Object>();
214 hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
215 hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
216 g.addRenderingHints(hints);
218 /*Path2D path = new Path2D.Double();
219 path.moveTo(0.0, 0.0);
220 path.lineTo(100.0, 100.0);
221 path.lineTo(400.0, 100.0);
226 g.setColor(Color.GRAY);
227 g.setStroke(new BasicStroke(1.0f));
228 for(Obstacle obs : obstacles)
231 Obstacle curObs = null;
232 if(mouseButtons == 1) {
247 curObs = new Obstacle(new Rectangle2D.Double(x0, y0, x1-x0, y1-y0));
248 g.fill(curObs.shape);
249 router.addObstacle(curObs);
252 Connection curConnection = null;
253 if(connection != null) {
254 double[] points = Arrays.copyOf(connection.routePath, connection.routePath.length+2);
255 points[points.length-2] = curX;
256 points[points.length-1] = curY;
257 curConnection = new Connection(points, connection.startDirections, connection.endDirections);
258 router.addConnection(curConnection);
261 g.setColor(Color.BLACK);
262 g.setStroke(new BasicStroke(3.0f));
263 for(Connection c : connections)
265 if(curConnection != null) {
266 draw(g, curConnection);
267 router.removeConnection(curConnection);
270 router.removeObstacle(curObs);
273 public void draw(Graphics2D g, Connection c) {
274 Path2D path = router.getPath(c);
277 g.setColor(Color.WHITE);
278 g.setStroke(stroke5);
280 g.setColor(Color.BLACK);
281 g.setStroke(stroke1);
283 double[] points = c.routePath;
284 for(int i=0;i<points.length;i+=2) {
285 double x = points[i];
286 double y = points[i+1];
287 g.setStroke(stroke1);
288 g.draw(new Ellipse2D.Double(x-5.0, y-5.0, 10.0, 10.0));
292 static final Stroke stroke1 = new BasicStroke(1.0f);
293 static final Stroke stroke3 = new BasicStroke(3.0f);
294 static final Stroke stroke5 = new BasicStroke(5.0f);
296 static void drawRectangle(Graphics2D g, double x0, double y0, double x1, double y1) {
311 Rectangle2D rect = new Rectangle2D.Double(x0, y0, x1-x0, y1-y0);
315 Image offScreenImage;
316 int offScreenImageWidth;
317 int offScreenImageHeight;
319 public void update( Graphics g ) {
320 if ( offScreenImage == null || getWidth() != offScreenImageWidth || getHeight() != offScreenImageHeight) {
321 offScreenImageWidth = getWidth();
322 offScreenImageHeight = getHeight();
323 offScreenImage = createImage(offScreenImageWidth, offScreenImageHeight);
326 Graphics gOffScreenImage = offScreenImage.getGraphics();
328 gOffScreenImage.clearRect(0, 0, getWidth(), getHeight());
329 paint( gOffScreenImage );
331 g.drawImage(offScreenImage, 0, 0, this);
333 gOffScreenImage.dispose();
336 public static void main(String[] args) {