Clover coverage report - Maven Clover report
Coverage timestamp: Thu Oct 11 2007 08:41:48 CEST
file stats: LOC: 313   Methods: 9
NCLOC: 205   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
GraphicHelper.java 76.7% 57.5% 66.7% 61.8%
coverage coverage
 1    /*
 2    * Copyright (C) 2007 Eric MIGNOT - mignots.eric@free.fr
 3    *
 4    * This library is free software; you can redistribute it and/or
 5    * modify it under the terms of the GNU Lesser General Public
 6    * License as published by the Free Software Foundation; either
 7    * version 2.1 of the License, or (at your option) any later version.
 8    *
 9    * This library is distributed in the hope that it will be useful,
 10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 12    * Lesser General Public License for more details.
 13    *
 14    * You should have received a copy of the GNU Lesser General Public
 15    * License along with this library; if not, write to the Free Software
 16    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 17    */
 18   
 19    package org.ericmignot.modeler.util.graphic;
 20   
 21    import java.awt.*;
 22    import java.awt.geom.*;
 23   
 24    import javax.swing.*;
 25   
 26    /**
 27    * Conveniences geometrical methods
 28    */
 29    public class GraphicHelper
 30    {
 31   
 32    /**
 33    * Returns the panel geometrical center
 34    * @param panel
 35    * @return
 36    */
 37  1499 public static Point getPanelCenter(JPanel panel)
 38    {
 39  1499 Rectangle bounds = panel.getBounds();
 40  1499 Point toReturn = new Point(
 41    bounds.x + bounds.width/2,
 42    bounds.y + bounds.height/2);
 43  1499 return toReturn;
 44    }
 45   
 46    // public Point getScreenLocation(Component c)
 47    // {
 48    // Point toReturn = c.getL
 49    // }
 50   
 51    /**
 52    * Returns the minimum panel containing all the given points
 53    * @param points
 54    * @return
 55    */
 56  1 public static Rectangle getMinPanelFromPoints(Point[] points)
 57    {
 58  1 if (points.length == 0)
 59    {
 60  1 return new Rectangle(0, 0, 0, 0);
 61    }
 62  0 int minX = 60000;
 63  0 int maxX = 0;
 64  0 int minY = 60000;
 65  0 int maxY = 0;
 66   
 67  0 for (int i = 0; i < points.length; i ++)
 68    {
 69  0 minX = (int) Math.min(minX, points[i].getX());
 70  0 minY = (int) Math.min(minY, points[i].getY());
 71  0 maxX = (int) Math.max(maxX, points[i].getX());
 72  0 maxY = (int) Math.max(maxY, points[i].getY());
 73    }
 74  0 Rectangle toReturn = new Rectangle(minX, minY, (maxX-minX+1), (maxY-minY+1));
 75  0 return toReturn;
 76    }
 77   
 78    /**
 79    * point1 is assumed to be out of the rectangle and point2 in the rectangle
 80    * @param point1
 81    * @param point2
 82    * @param rectangle
 83    * @return
 84    */
 85  625 public static Point getLineRectangleIntersection(Point point1, Point point2, Rectangle rectangle)
 86    {
 87  625 int outCode = rectangle.outcode(point1.getX(), point1.getY());
 88   
 89  625 Point arrowPoint = null;
 90  625 if (outCode == Rectangle2D.OUT_LEFT)
 91    {
 92  184 int x = (int) rectangle.getX();
 93    //y1 (x2-x)/(x2-x1) + y2 (x1-x)/(x1-x2)
 94  184 int y = (int) (point1.getY()*(point2.getX()-x)/(point2.getX()-point1.getX()) + point2.getY()*(point1.getX()-x)/(point1.getX()-point2.getX()));
 95  184 arrowPoint = new Point(x, y);
 96    }
 97  441 else if (outCode == Rectangle2D.OUT_RIGHT)
 98    {
 99  178 int x = (int) (rectangle.getX() + rectangle.getWidth() - 1);
 100    //y1 (x2-x)/(x2-x1) + y2 (x1-x)/(x1-x2)
 101  178 int y = (int) (point1.getY()*(point2.getX()-x)/(point2.getX()-point1.getX()) + point2.getY()*(point1.getX()-x)/(point1.getX()-point2.getX()));
 102  178 arrowPoint = new Point(x, y);
 103    }
 104  263 else if (outCode == Rectangle2D.OUT_TOP)
 105    {
 106  4 int y = (int) (rectangle.getY());
 107    //x1 (y2-y)/(y2-y1) + x2 (y1-x)/(y1-y2)
 108  4 int x = (int) (point1.getX()*(point2.getY()-y)/(point2.getY()-point1.getY()) + point2.getX()*(point1.getY()-y)/(point1.getY()-point2.getY()));
 109  4 arrowPoint = new Point(x, y);
 110    }
 111  259 else if (outCode == Rectangle2D.OUT_BOTTOM)
 112    {
 113  4 int y = (int) (rectangle.getY() + rectangle.getHeight() - 1);
 114    //x1 (y2-y)/(y2-y1) + x2 (y1-x)/(y1-y2)
 115  4 int x = (int) (point1.getX()*(point2.getY()-y)/(point2.getY()-point1.getY()) + point2.getX()*(point1.getY()-y)/(point1.getY()-point2.getY()));
 116  4 arrowPoint = new Point(x, y);
 117    }
 118  255 else if (outCode == Rectangle2D.OUT_LEFT + Rectangle.OUT_TOP)
 119    {
 120  24 int ccw = Line2D.relativeCCW(point1.getX(), point1.getY(), point2.getX(), point2.getY(),
 121    rectangle.getX(), rectangle.getY());
 122  24 if (ccw == -1)
 123    {
 124  0 int y = (int) (rectangle.getY());
 125    //x1 (y2-y)/(y2-y1) + x2 (y1-x)/(y1-y2)
 126  0 int x = (int) (point1.getX()*(point2.getY()-y)/(point2.getY()-point1.getY()) + point2.getX()*(point1.getY()-y)/(point1.getY()-point2.getY()));
 127  0 arrowPoint = new Point(x, y);
 128    }
 129    else
 130    {
 131  24 int x = (int) rectangle.getX();
 132    //y1 (x2-x)/(x2-x1) + y2 (x1-x)/(x1-x2)
 133  24 int y = (int) (point1.getY()*(point2.getX()-x)/(point2.getX()-point1.getX()) + point2.getY()*(point1.getX()-x)/(point1.getX()-point2.getX()));
 134  24 arrowPoint = new Point(x, y);
 135    }
 136    }
 137  231 else if (outCode == Rectangle2D.OUT_LEFT + Rectangle.OUT_BOTTOM)
 138    {
 139  4 int ccw = Line2D.relativeCCW(point1.getX(), point1.getY(), point2.getX(), point2.getY(),
 140    rectangle.getX(), rectangle.getY() + rectangle.getHeight() - 1);
 141  4 if (ccw == -1)
 142    {
 143  2 int x = (int) rectangle.getX();
 144    //y1 (x2-x)/(x2-x1) + y2 (x1-x)/(x1-x2)
 145  2 int y = (int) (point1.getY()*(point2.getX()-x)/(point2.getX()-point1.getX()) + point2.getY()*(point1.getX()-x)/(point1.getX()-point2.getX()));
 146  2 arrowPoint = new Point(x, y);
 147    }
 148    else
 149    {
 150  2 int y = (int) (rectangle.getY() + rectangle.getHeight() - 1);
 151    //x1 (y2-y)/(y2-y1) + x2 (y1-x)/(y1-y2)
 152  2 int x = (int) (point1.getX()*(point2.getY()-y)/(point2.getY()-point1.getY()) + point2.getX()*(point1.getY()-y)/(point1.getY()-point2.getY()));
 153  2 arrowPoint = new Point(x, y);
 154    }
 155    }
 156  227 else if (outCode == Rectangle2D.OUT_RIGHT + Rectangle.OUT_TOP)
 157    {
 158  4 int ccw = Line2D.relativeCCW(point1.getX(), point1.getY(), point2.getX(), point2.getY(),
 159    rectangle.getX() + rectangle.getWidth() - 1, rectangle.getY());
 160  4 if (ccw == -1)
 161    {
 162  1 int x = (int) (rectangle.getX() + rectangle.getWidth() - 1);
 163    //y1 (x2-x)/(x2-x1) + y2 (x1-x)/(x1-x2)
 164  1 int y = (int) (point1.getY()*(point2.getX()-x)/(point2.getX()-point1.getX()) + point2.getY()*(point1.getX()-x)/(point1.getX()-point2.getX()));
 165  1 arrowPoint = new Point(x, y);
 166    }
 167    else
 168    {
 169  3 int y = (int) (rectangle.getY());
 170    //x1 (y2-y)/(y2-y1) + x2 (y1-x)/(y1-y2)
 171  3 int x = (int) (point1.getX()*(point2.getY()-y)/(point2.getY()-point1.getY()) + point2.getX()*(point1.getY()-y)/(point1.getY()-point2.getY()));
 172  3 arrowPoint = new Point(x, y);
 173    }
 174    }
 175  223 else if (outCode == Rectangle2D.OUT_RIGHT + Rectangle.OUT_BOTTOM)
 176    {
 177  72 int ccw = Line2D.relativeCCW(point1.getX(), point1.getY(), point2.getX(), point2.getY(),
 178    rectangle.getX() + rectangle.getWidth() - 1, rectangle.getY() + rectangle.getHeight() - 1);
 179  72 if (ccw == -1)
 180    {
 181  0 int y = (int) (rectangle.getY() + rectangle.getHeight() - 1);
 182    //x1 (y2-y)/(y2-y1) + x2 (y1-x)/(y1-y2)
 183  0 int x = (int) (point1.getX()*(point2.getY()-y)/(point2.getY()-point1.getY()) + point2.getX()*(point1.getY()-y)/(point1.getY()-point2.getY()));
 184  0 arrowPoint = new Point(x, y);
 185    }
 186    else
 187    {
 188  72 int x = (int) (rectangle.getX() + rectangle.getWidth() - 1);
 189    //y1 (x2-x)/(x2-x1) + y2 (x1-x)/(x1-x2)
 190  72 int y = (int) (point1.getY()*(point2.getX()-x)/(point2.getX()-point1.getX()) + point2.getY()*(point1.getX()-x)/(point1.getX()-point2.getX()));
 191  72 arrowPoint = new Point(x, y);
 192    }
 193    }
 194  625 return arrowPoint;
 195    }
 196    /**
 197    * Draws an line finished by an arrow from point1 to point2
 198    *
 199    * @param g
 200    * @param point1
 201    * @param point2
 202    * @param length
 203    * @param angle in radians
 204    */
 205  0 public static void drawArrow(Graphics g, Point point1, Point point2, double zoomFactor)
 206    {
 207  0 double angle = 30 * Math.PI / 180;
 208  0 double length = 10 * zoomFactor;
 209   
 210  0 double norme = Math.sqrt((point2.getY() - point1.getY())*(point2.getY() - point1.getY()) + (point2.getX() - point1.getX())*(point2.getX() - point1.getX()));
 211  0 Point2D unitVector = new Point2D.Double(
 212    (point1.getX()-point2.getX())/norme,
 213    (point1.getY()-point2.getY())/norme
 214    );
 215  0 double cos = Math.cos(angle);
 216  0 double sin = Math.sin(angle);
 217   
 218  0 double x;
 219  0 double y;
 220   
 221  0 x = cos * unitVector.getX() - sin * unitVector.getY();
 222  0 y = sin * unitVector.getX() + cos * unitVector.getY();
 223  0 x *= length;
 224  0 y *= length;
 225  0 x += point2.getX();
 226  0 y += point2.getY();
 227  0 Point base1 = new Point((int) x, (int) y);
 228    //g.drawLine((int) point2.getX(), (int) point2.getY(), (int) base1.getX(), (int) base1.getY());
 229   
 230  0 x = cos * unitVector.getX() + sin * unitVector.getY();
 231  0 y = -sin * unitVector.getX() + cos * unitVector.getY();
 232  0 x *= length;
 233  0 y *= length;
 234  0 x += point2.getX();
 235  0 y += point2.getY();
 236  0 Point base2 = new Point((int) x, (int) y);
 237    //g.drawLine((int) point2.getX(), (int) point2.getY(), (int) base2.getX(), (int) base2.getY());
 238   
 239  0 Polygon poly = new Polygon(new int[] {base1.x, point2.x, base2.x},
 240    new int[] {base1.y, point2.y, base2.y}, 3);
 241  0 g.fillPolygon(poly);
 242   
 243    }
 244   
 245    /**
 246    * Returns the same rectangle instance enlarged from the given parameters
 247    * @param rectangle
 248    * @param semiWidth
 249    * @param semiHeight
 250    * @return
 251    */
 252  0 public static Rectangle enlargeRectangle(Rectangle rectangle, int semiWidth, int semiHeight)
 253    {
 254  0 rectangle.setBounds(
 255    (int)(rectangle.getX() - semiWidth),
 256    (int)(rectangle.getY() - semiHeight),
 257    (int)(rectangle.getWidth() + 2*semiWidth),
 258    (int)(rectangle.getHeight() + 2*semiHeight)
 259    );
 260  0 return rectangle;
 261    }
 262   
 263  3 public static Rectangle buildContainerRectangle(Rectangle r1, Rectangle r2)
 264    {
 265  3 int minx = Math.min(r1.x, r2.x);
 266  3 int miny = Math.min(r1.y, r2.y);
 267  3 int maxx = Math.max(r1.x+r1.width, r2.x+r2.width);
 268  3 int maxy = Math.max(r1.y+r1.height, r2.y+r2.height);
 269   
 270  3 return new Rectangle(minx, miny, (maxx-minx), (maxy-miny));
 271    }
 272   
 273    /**
 274    *
 275    * @param rvb the rvb code assumed to be formated like for example "204, 0, 15"
 276    * @return
 277    */
 278  491 public static Color buildColor(String rvb)
 279    {
 280  491 String[] values = rvb.split(",");
 281   
 282  491 int red = Integer.parseInt(values[0].trim());
 283  491 int green = Integer.parseInt(values[1].trim());
 284  491 int blue = Integer.parseInt(values[2].trim());
 285   
 286  491 return new Color(red, green, blue);
 287    }
 288   
 289    /**
 290    *
 291    * @param p the point
 292    * @param s1 the beginning of the segment
 293    * @param s2 the end of the segment
 294    * @return -1 if the point p is out of the rectangle made by s1 and s2
 295    */
 296  0 public static double getSegmentDistance(Point s1, Point s2, Point p)
 297    {
 298  0 double toReturn = -1;
 299   
 300  0 Rectangle r = new Rectangle(Math.min(s1.x, s2.x), Math.min(s1.y, s2.y),
 301    Math.abs(s2.x-s1.x +1), Math.abs(s2.y-s1.y +1));
 302  0 if (r.contains(p))
 303    {
 304  0 toReturn = Line2D.ptLineDist(s1.x, s1.y, s2.x, s2.y, p.x, p.y);
 305    }
 306   
 307  0 return toReturn;
 308    }
 309  77 public static double getLineDistance(Point s1, Point s2, Point p)
 310    {
 311  77 return Line2D.ptLineDist(s1.x, s1.y, s2.x, s2.y, p.x, p.y);
 312    }
 313    }