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.event.MouseEvent;
22 import java.awt.event.MouseListener;
23 import java.awt.event.MouseMotionListener;
24 import java.awt.event.WindowEvent;
25 import java.awt.event.WindowListener;
26 import java.awt.geom.Rectangle2D;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.HashMap;
32 import org.simantics.g2d.routing.algorithm1.Penalty;
33 import org.simantics.g2d.routing.algorithm1.Rectangle;
34 import org.simantics.g2d.routing.algorithm1.StopSet;
35 import org.simantics.g2d.routing.algorithm1.StopSet.IStopProcedure;
36 import org.simantics.g2d.routing.algorithm1.StopSet.Line;
37 import org.simantics.g2d.routing.algorithm1.StopSet.Stop;
39 public class TestStopSet extends Frame {
41 private static final long serialVersionUID = 2181877722124429003L;
43 Collection<Rectangle> rectangles = new ArrayList<Rectangle>();
47 double startX, startY;
51 public TestStopSet() {
52 addWindowListener(new WindowListener() {
55 public void windowActivated(WindowEvent e) {
59 public void windowClosed(WindowEvent e) {
63 public void windowClosing(WindowEvent e) {
68 public void windowDeactivated(WindowEvent e) {
72 public void windowDeiconified(WindowEvent e) {
76 public void windowIconified(WindowEvent e) {
80 public void windowOpened(WindowEvent e) {
85 addMouseListener(new MouseListener() {
88 public void mouseClicked(MouseEvent e) {
89 // TODO Auto-generated method stub
94 public void mouseEntered(MouseEvent e) {
95 // TODO Auto-generated method stub
100 public void mouseExited(MouseEvent e) {
101 // TODO Auto-generated method stub
106 public void mousePressed(MouseEvent e) {
107 mouseButtons = e.getButton();
108 curX = startX = e.getX();
109 curY = startY = e.getY();
110 if(e.getButton() == MouseEvent.BUTTON2)
112 else if(e.getButton() == MouseEvent.BUTTON3)
118 public void mouseReleased(MouseEvent e) {
122 double x1 = e.getX();
123 double y1 = e.getY();
134 Rectangle rect = new Rectangle(x0, y0, x1, y1);
136 if(e.getButton() == MouseEvent.BUTTON1)
137 rectangles.add(rect);
138 else if(e.getButton() == MouseEvent.BUTTON2)
140 else if(e.getButton() == MouseEvent.BUTTON3)
148 addMouseMotionListener(new MouseMotionListener() {
151 public void mouseDragged(MouseEvent e) {
158 public void mouseMoved(MouseEvent e) {
168 public void paint(Graphics _g) {
169 final Graphics2D g = (Graphics2D)_g;
170 Map<Object, Object> hints = new HashMap<Object, Object>();
171 hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
172 hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
173 g.addRenderingHints(hints);
175 /*Path2D path = new Path2D.Double();
176 path.moveTo(0.0, 0.0);
177 path.lineTo(100.0, 100.0);
178 path.lineTo(400.0, 100.0);
183 g.setColor(Color.GRAY);
184 g.setStroke(new BasicStroke(1.0f));
185 for(Rectangle rect : rectangles)
186 drawRectangle(g, rect.x0, rect.y0, rect.x1, rect.y1);
189 g.setColor(Color.BLUE);
190 drawRectangle(g, source.x0, source.y0, source.x1, source.y1);
193 g.setColor(Color.GREEN);
194 drawRectangle(g, target.x0, target.y0, target.x1, target.y1);
196 if(mouseButtons != 0) {
197 if(mouseButtons == 1)
198 g.setColor(Color.BLACK);
199 else if(mouseButtons == 2)
200 g.setColor(Color.BLUE);
201 else if(mouseButtons == 3)
202 g.setColor(Color.GREEN);
203 drawRectangle(g, startX, startY, curX, curY);
206 Collection<Stop> stops = new ArrayList<Stop>(rectangles.size());
207 Penalty penalty = new Penalty(100.0, 0.0);
208 for(Rectangle rect : rectangles)
209 stops.add(new Stop(penalty, rect.x0, rect.x1, rect.y0));
210 StopSet ss = new StopSet(stops);
213 g.setColor(Color.GREEN);
214 g.setStroke(new BasicStroke(1.0f));
215 drawFront(g, ss, target.x0, target.x1, target.y1);
219 public final double PADDING = 2.0;
221 void drawFront(final Graphics2D g, final StopSet ss, final double x0, final double x1, final double y) {
222 ss.findStops(x0, x1, y, new IStopProcedure() {
225 public void blockEnd(double y1) {
226 if(y1 == Double.POSITIVE_INFINITY)
228 Rectangle2D rect = new Rectangle2D.Double(x0 + PADDING, y + PADDING, x1-x0 - PADDING*2, y1-y - PADDING*2);
233 public void continuation(double min, double max, int pos, Line line) {
234 drawFront(g, ss, pos, line, min, max);
240 void drawFront(final Graphics2D g, final StopSet ss, int pos, final Line line, final double x0, final double x1) {
241 StopSet.continueStop(pos, line, x0, x1, new IStopProcedure() {
244 public void blockEnd(double y1) {
245 if(y1 == Double.POSITIVE_INFINITY)
247 Rectangle2D rect = new Rectangle2D.Double(x0 + PADDING, line.y + PADDING, x1-x0 - PADDING*2, y1-line.y - PADDING*2);
252 public void continuation(double min, double max, int pos, Line line) {
253 drawFront(g, ss, pos, line, min, max);
259 static void drawRectangle(Graphics2D g, double x0, double y0, double x1, double y1) {
270 Rectangle2D rect = new Rectangle2D.Double(x0, y0, x1-x0, y1-y0);
274 Image offScreenImage;
275 int offScreenImageWidth;
276 int offScreenImageHeight;
278 public void update( Graphics g ) {
279 if ( offScreenImage == null || getWidth() != offScreenImageWidth || getHeight() != offScreenImageHeight) {
280 offScreenImageWidth = getWidth();
281 offScreenImageHeight = getHeight();
282 offScreenImage = createImage(offScreenImageWidth, offScreenImageHeight);
285 Graphics gOffScreenImage = offScreenImage.getGraphics();
287 gOffScreenImage.clearRect(0, 0, getWidth(), getHeight());
288 paint( gOffScreenImage );
290 g.drawImage(offScreenImage, 0, 0, this);
292 gOffScreenImage.dispose();
295 public static void main(String[] args) {