1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }