View Javadoc

1   /*
2   
3   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
4   
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by the
7   Free Software Foundation; either version 2 of the License, or (at your
8   option) any later version.
9   
10  This program is distributed in the hope that it will
11  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  PURPOSE. See the GNU General Public License for more details.
14  
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc., 59
17  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  
19  */
20  
21  package org.devaki.nextobjects.util;
22  
23  import java.awt.Point;
24  import java.awt.Color;
25  
26  /***
27   * This class contains usefull graphics functions
28   * @author eflorent
29   */
30  public abstract class NOGraphicsUtil
31  {
32   /*** 
33     * Calcultate distance
34     * @param p1 the A point
35     * @param p2 the B point
36     * @return the distance
37     */
38    public static int distance(Point p1, Point p2)
39    {
40     return (int) Math.sqrt(
41      Math.pow(p1.getX() - p2.getX(), 2)
42       + Math.pow(p1.getY() - p2.getY(), 2));
43    }
44    
45   /***
46    * Calculate middle
47    * @param pA the A point
48    * @param pB the B point
49    * @return the point at the middle
50    */
51    public static Point getMiddle(Point pA, Point pB)
52    {
53     double x = Math.abs(pA.getX() - pB.getX());
54     x = x / 2;
55     x = x + Math.min(pA.getX(), pB.getX());
56     double y = Math.abs(pA.getY() - pB.getY());
57     y = y / 2;
58     y = y + Math.min(pA.getY(), pB.getY());
59     return new Point((int) x, (int) y);
60    }
61    
62   /***
63    * Give a Color from a hexadecimal representation within a string
64    * @param strColor the string to convert
65    * @return the Color
66    */
67   public static Color hexString2Color(String strColor)
68   {
69    //anyway return black.
70    if (strColor == null)
71    {
72     strColor = "#000000";
73    }
74    strColor = strColor.substring(1, strColor.length());
75    return (new Color(Integer.parseInt(strColor.trim(), 16)));
76   }
77   
78   /***
79   * Give a string hexadecimal representation of a Color
80   * @param couleur the color to convert
81   * @return the String
82   */
83   public static String color2HexString(Color couleur)
84   {
85    String strColor = Integer.toHexString(couleur.getRGB());
86    return "#" + strColor.substring(strColor.length() - 6);
87   }
88     
89  }