]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/style/StyledRectangle.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / style / StyledRectangle.java
1 /*****************************************************/\r
2 /*          This java file is a part of the          */\r
3 /*                                                   */\r
4 /*           -  Plouf's Java IRC Client  -           */\r
5 /*                                                   */\r
6 /*   Copyright (C)  2002 - 2004 Philippe Detournay   */\r
7 /*                                                   */\r
8 /*         All contacts : theplouf@yahoo.com         */\r
9 /*                                                   */\r
10 /*  PJIRC is free software; you can redistribute     */\r
11 /*  it and/or modify it under the terms of the GNU   */\r
12 /*  General Public License as published by the       */\r
13 /*  Free Software Foundation; version 2 or later of  */\r
14 /*  the License.                                     */\r
15 /*                                                   */\r
16 /*  PJIRC is distributed in the hope that it will    */\r
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */\r
18 /*  even the implied warranty of MERCHANTABILITY or  */\r
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */\r
20 /*  General Public License for more details.         */\r
21 /*                                                   */\r
22 /*  You should have received a copy of the GNU       */\r
23 /*  General Public License along with PJIRC; if      */\r
24 /*  not, write to the Free Software Foundation,      */\r
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */\r
26 /*  MA  02111-1307  USA                              */\r
27 /*                                                   */\r
28 /*****************************************************/\r
29 \r
30 package irc.style;\r
31 \r
32 /**\r
33  * A rectangle.\r
34  */\r
35 public class StyledRectangle {\r
36         /**\r
37          * X position.\r
38          */\r
39         public int x;\r
40 \r
41         /**\r
42          * Y position.\r
43          */\r
44         public int y;\r
45 \r
46         /**\r
47          * Width.\r
48          */\r
49         public int width;\r
50 \r
51         /**\r
52          * Height.\r
53          */\r
54         public int height;\r
55 \r
56         /**\r
57          * Create a new StyledRectangle.\r
58          * \r
59          * @param ax\r
60          *          x position.\r
61          * @param ay\r
62          *          y position.\r
63          * @param w\r
64          *          width.\r
65          * @param h\r
66          *          height.\r
67          */\r
68         public StyledRectangle(int ax, int ay, int w, int h) {\r
69                 x = ax;\r
70                 y = ay;\r
71                 width = w;\r
72                 height = h;\r
73         }\r
74 \r
75         @Override\r
76         public boolean equals(Object o) {\r
77                 if (!(o instanceof StyledRectangle))\r
78                         return false;\r
79                 StyledRectangle r = (StyledRectangle) o;\r
80                 return (r.x == x) && (r.y == y) && (r.width == width) && (r.height == height);\r
81         }\r
82 \r
83         @Override\r
84         public int hashCode() {\r
85                 return x + y + width + height;\r
86         }\r
87 \r
88         /**\r
89          * Test wether the given point is inside the rectangle or not.\r
90          * \r
91          * @param px\r
92          *          x coordinate of point to test.\r
93          * @param py\r
94          *          y coordinate of point to test.\r
95          * @return true if the point is inside the rectangle, false otherwise.\r
96          */\r
97         public boolean contains(int px, int py) {\r
98                 return (px >= x) && (py >= y) && (px < x + width) && (py < y + height);\r
99         }\r
100 \r
101         private boolean noEmpty(int v1, int lv1, int v2, int lv2) {\r
102                 if (v1 < v2)\r
103                         return v1 + lv1 > v2;\r
104                 return v2 + lv2 > v1;\r
105         }\r
106 \r
107         /**\r
108          * Test wether the given rectangle is partially or fully inside the rectangle\r
109          * or not.\r
110          * \r
111          * @param r\r
112          *          rectangle to test.\r
113          * @return true if r is partially or fully inside the rectangle, false\r
114          *         otherwise.\r
115          */\r
116         public boolean hit(StyledRectangle r) {\r
117                 if (!noEmpty(r.x, r.width, x, width))\r
118                         return false;\r
119                 return noEmpty(r.y, r.height, y, height);\r
120         }\r
121 \r
122         /**\r
123          * Add the given rectangle to this rectangle. This rectangle will be the union\r
124          * of the two rectangles.\r
125          * \r
126          * @param r\r
127          *          the rectangle to add.\r
128          */\r
129         public void add(StyledRectangle r) {\r
130                 if (r.x < x) {\r
131                         width += (x - r.x);\r
132                         x = r.x;\r
133                 }\r
134 \r
135                 if (r.y < y) {\r
136                         height += (y - r.y);\r
137                         y = r.y;\r
138                 }\r
139 \r
140                 if (r.width > width)\r
141                         width = r.width;\r
142                 if (r.height > height)\r
143                         height = r.height;\r
144         }\r
145 \r
146         @Override\r
147         public String toString() {\r
148                 return "StyledRectangle : " + x + "," + y + "," + width + "," + height;\r
149         }\r
150 }\r