-/*******************************************************************************\r
- * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.trend.util;\r
-\r
-import java.awt.Graphics2D;\r
-import java.awt.geom.Line2D;\r
-import java.awt.geom.Rectangle2D;\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-/**\r
- * DeviationBuilder builds a vertical string of rectangles as an AWT Shape.\r
- * Rectangles are added to the shape, each added to the right hand.\r
- * \r
- * @author toni.kalajainen\r
- */\r
-public class KvikDeviationBuilder {\r
-\r
- List<Rectangle2D.Double> rects = new ArrayList<Rectangle2D.Double>();\r
- int rectCount = 0;\r
- \r
- public KvikDeviationBuilder() {\r
- reset();\r
- }\r
- \r
- public void addRectangle(double x1, double x2, double y1, double y2)\r
- {\r
- if (y1>y2) {\r
- double yy = y1;\r
- y1 = y2;\r
- y2 = yy;\r
- }\r
- \r
- if (y2 == Double.NaN || y1 == Double.NaN || y2 == y1) {\r
- return;\r
- }\r
- \r
- Rectangle2D.Double lastRect = lastRect();\r
- if ( lastRect!=null ) {\r
- double lx1 = lastRect.x;\r
- double lx2 = lastRect.x + lastRect.width;\r
- double ly1 = lastRect.y;\r
- double ly2 = lastRect.y + lastRect.height;\r
- \r
- // Extends min-max of prev rectangle\r
- if (x1==lx1 && x2==lx2) {\r
- if (y2<ly1) lastRect.y = y2;\r
- if (y1>ly2) lastRect.height = y1 - lastRect.y;\r
- return;\r
- }\r
- \r
- // Change the last values \r
- if (x1==lx2 && y1==ly1 && y2==ly2) {\r
- lastRect.width = x2 - lastRect.x;\r
- return;\r
- }\r
- }\r
-\r
- // Add new region\r
- Rectangle2D.Double r = newRect();\r
- r.x=x1;\r
- r.y=y1;\r
- r.width=x2-x1;\r
- r.height=y2-y1;\r
- }\r
-\r
- /**\r
- * Extend the previous rectangle to the left\r
- * \r
- * @param x1\r
- * @param x2\r
- * @param low\r
- * @param high\r
- */\r
- public void appendRectangle(double xx, double low, double high)\r
- {\r
- Rectangle2D.Double lastRect = lastRect();\r
- if ( lastRect!=null ) {\r
- addRectangle(lastRect.x+lastRect.width, xx, low, high);\r
- } else {\r
- addRectangle(xx, xx, low, high);\r
- }\r
- }\r
-\r
- public void extendRectangle(double toX)\r
- {\r
- Rectangle2D.Double lastRect = lastRect();\r
- if ( lastRect == null ) return;\r
- lastRect.width = toX - lastRect.x;\r
- }\r
- \r
- public boolean isEmpty() {\r
- return rectCount == 0;\r
- }\r
- \r
- public void reset() {\r
- rectCount = 0;\r
- }\r
- \r
- /**\r
- * Draw the shape with lines. This is the fastest for screen but not good for printer drawing\r
- * \r
- * @param g\r
- */\r
- public void drawLines(Graphics2D g) {\r
- Line2D.Double line = new Line2D.Double();\r
- for (int i=0; i<rectCount; i++) {\r
- Rectangle2D.Double r = rects.get(i);\r
- int x1 = (int) Math.round(r.x);\r
- int x2 = (int) Math.round(r.x);\r
- line.y1 = r.y;\r
- line.y2 = r.y + r.height;\r
- for (int x = x1; x<=x2; x++) {\r
- line.x1 = line.x2 = x;\r
- g.draw(line);\r
- }\r
- }\r
- }\r
- \r
- /**\r
- * Draw the shape with rectangles. Correct, not the fastest\r
- */\r
- public void drawRectangles(Graphics2D g) {\r
- for (int i=0; i<rectCount; i++) {\r
- Rectangle2D.Double r = rects.get(i);\r
- g.fill(r);\r
- }\r
- }\r
- \r
- /**\r
- * Create new rectangle and add to the list. \r
- * @return new rectangle\r
- */\r
- Rectangle2D.Double newRect() {\r
- if (rectCount < rects.size()) {\r
- return rects.get( rectCount++ );\r
- }\r
- Rectangle2D.Double rect = new Rectangle2D.Double();\r
- rectCount++;\r
- rects.add(rect);\r
- return rect;\r
- }\r
- \r
- /**\r
- * Get the last rectangle\r
- * @return\r
- */\r
- Rectangle2D.Double lastRect() {\r
- if ( rectCount == 0 ) return null;\r
- return rects.get( rectCount-1 );\r
- }\r
- \r
-\r
-}\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.trend.util;
+
+import java.awt.Graphics2D;
+import java.awt.geom.Line2D;
+import java.awt.geom.Rectangle2D;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * DeviationBuilder builds a vertical string of rectangles as an AWT Shape.
+ * Rectangles are added to the shape, each added to the right hand.
+ *
+ * @author toni.kalajainen
+ */
+public class KvikDeviationBuilder {
+
+ List<Rectangle2D.Double> rects = new ArrayList<Rectangle2D.Double>();
+ int rectCount = 0;
+
+ public KvikDeviationBuilder() {
+ reset();
+ }
+
+ public void addRectangle(double x1, double x2, double y1, double y2)
+ {
+ if (y1>y2) {
+ double yy = y1;
+ y1 = y2;
+ y2 = yy;
+ }
+
+ if (y2 == Double.NaN || y1 == Double.NaN || y2 == y1) {
+ return;
+ }
+
+ Rectangle2D.Double lastRect = lastRect();
+ if ( lastRect!=null ) {
+ double lx1 = lastRect.x;
+ double lx2 = lastRect.x + lastRect.width;
+ double ly1 = lastRect.y;
+ double ly2 = lastRect.y + lastRect.height;
+
+ // Extends min-max of prev rectangle
+ if (x1==lx1 && x2==lx2) {
+ if (y2<ly1) lastRect.y = y2;
+ if (y1>ly2) lastRect.height = y1 - lastRect.y;
+ return;
+ }
+
+ // Change the last values
+ if (x1==lx2 && y1==ly1 && y2==ly2) {
+ lastRect.width = x2 - lastRect.x;
+ return;
+ }
+ }
+
+ // Add new region
+ Rectangle2D.Double r = newRect();
+ r.x=x1;
+ r.y=y1;
+ r.width=x2-x1;
+ r.height=y2-y1;
+ }
+
+ /**
+ * Extend the previous rectangle to the left
+ *
+ * @param x1
+ * @param x2
+ * @param low
+ * @param high
+ */
+ public void appendRectangle(double xx, double low, double high)
+ {
+ Rectangle2D.Double lastRect = lastRect();
+ if ( lastRect!=null ) {
+ addRectangle(lastRect.x+lastRect.width, xx, low, high);
+ } else {
+ addRectangle(xx, xx, low, high);
+ }
+ }
+
+ public void extendRectangle(double toX)
+ {
+ Rectangle2D.Double lastRect = lastRect();
+ if ( lastRect == null ) return;
+ lastRect.width = toX - lastRect.x;
+ }
+
+ public boolean isEmpty() {
+ return rectCount == 0;
+ }
+
+ public void reset() {
+ rectCount = 0;
+ }
+
+ /**
+ * Draw the shape with lines. This is the fastest for screen but not good for printer drawing
+ *
+ * @param g
+ */
+ public void drawLines(Graphics2D g) {
+ Line2D.Double line = new Line2D.Double();
+ for (int i=0; i<rectCount; i++) {
+ Rectangle2D.Double r = rects.get(i);
+ int x1 = (int) Math.round(r.x);
+ int x2 = (int) Math.round(r.x);
+ line.y1 = r.y;
+ line.y2 = r.y + r.height;
+ for (int x = x1; x<=x2; x++) {
+ line.x1 = line.x2 = x;
+ g.draw(line);
+ }
+ }
+ }
+
+ /**
+ * Draw the shape with rectangles. Correct, not the fastest
+ */
+ public void drawRectangles(Graphics2D g) {
+ for (int i=0; i<rectCount; i++) {
+ Rectangle2D.Double r = rects.get(i);
+ g.fill(r);
+ }
+ }
+
+ /**
+ * Create new rectangle and add to the list.
+ * @return new rectangle
+ */
+ Rectangle2D.Double newRect() {
+ if (rectCount < rects.size()) {
+ return rects.get( rectCount++ );
+ }
+ Rectangle2D.Double rect = new Rectangle2D.Double();
+ rectCount++;
+ rects.add(rect);
+ return rect;
+ }
+
+ /**
+ * Get the last rectangle
+ * @return
+ */
+ Rectangle2D.Double lastRect() {
+ if ( rectCount == 0 ) return null;
+ return rects.get( rectCount-1 );
+ }
+
+
+}
+