1 /*******************************************************************************
2 * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.trend.util;
13 import java.awt.Graphics2D;
14 import java.awt.geom.Line2D;
15 import java.awt.geom.Rectangle2D;
16 import java.util.ArrayList;
17 import java.util.List;
20 * DeviationBuilder builds a vertical string of rectangles as an AWT Shape.
21 * Rectangles are added to the shape, each added to the right hand.
23 * @author toni.kalajainen
25 public class KvikDeviationBuilder {
27 List<Rectangle2D.Double> rects = new ArrayList<Rectangle2D.Double>();
30 public KvikDeviationBuilder() {
34 public void addRectangle(double x1, double x2, double y1, double y2)
42 if (y2 == Double.NaN || y1 == Double.NaN || y2 == y1) {
46 Rectangle2D.Double lastRect = lastRect();
47 if ( lastRect!=null ) {
48 double lx1 = lastRect.x;
49 double lx2 = lastRect.x + lastRect.width;
50 double ly1 = lastRect.y;
51 double ly2 = lastRect.y + lastRect.height;
53 // Extends min-max of prev rectangle
54 if (x1==lx1 && x2==lx2) {
55 if (y2<ly1) lastRect.y = y2;
56 if (y1>ly2) lastRect.height = y1 - lastRect.y;
60 // Change the last values
61 if (x1==lx2 && y1==ly1 && y2==ly2) {
62 lastRect.width = x2 - lastRect.x;
68 Rectangle2D.Double r = newRect();
76 * Extend the previous rectangle to the left
83 public void appendRectangle(double xx, double low, double high)
85 Rectangle2D.Double lastRect = lastRect();
86 if ( lastRect!=null ) {
87 addRectangle(lastRect.x+lastRect.width, xx, low, high);
89 addRectangle(xx, xx, low, high);
93 public void extendRectangle(double toX)
95 Rectangle2D.Double lastRect = lastRect();
96 if ( lastRect == null ) return;
97 lastRect.width = toX - lastRect.x;
100 public boolean isEmpty() {
101 return rectCount == 0;
104 public void reset() {
109 * Draw the shape with lines. This is the fastest for screen but not good for printer drawing
113 public void drawLines(Graphics2D g) {
114 Line2D.Double line = new Line2D.Double();
115 for (int i=0; i<rectCount; i++) {
116 Rectangle2D.Double r = rects.get(i);
117 int x1 = (int) Math.round(r.x);
118 int x2 = (int) Math.round(r.x);
120 line.y2 = r.y + r.height;
121 for (int x = x1; x<=x2; x++) {
122 line.x1 = line.x2 = x;
129 * Draw the shape with rectangles. Correct, not the fastest
131 public void drawRectangles(Graphics2D g) {
132 for (int i=0; i<rectCount; i++) {
133 Rectangle2D.Double r = rects.get(i);
139 * Create new rectangle and add to the list.
140 * @return new rectangle
142 Rectangle2D.Double newRect() {
143 if (rectCount < rects.size()) {
144 return rects.get( rectCount++ );
146 Rectangle2D.Double rect = new Rectangle2D.Double();
153 * Get the last rectangle
156 Rectangle2D.Double lastRect() {
157 if ( rectCount == 0 ) return null;
158 return rects.get( rectCount-1 );