| 1 | /* Copyright (C) 2000, 2002 Free Software Foundation
|
|---|
| 2 |
|
|---|
| 3 | This file is part of GNU Classpath.
|
|---|
| 4 |
|
|---|
| 5 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 6 | it under the terms of the GNU General Public License as published by
|
|---|
| 7 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 8 | any later version.
|
|---|
| 9 |
|
|---|
| 10 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 11 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 13 | General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU General Public License
|
|---|
| 16 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 17 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 18 | 02111-1307 USA.
|
|---|
| 19 |
|
|---|
| 20 | Linking this library statically or dynamically with other modules is
|
|---|
| 21 | making a combined work based on this library. Thus, the terms and
|
|---|
| 22 | conditions of the GNU General Public License cover the whole
|
|---|
| 23 | combination.
|
|---|
| 24 |
|
|---|
| 25 | As a special exception, the copyright holders of this library give you
|
|---|
| 26 | permission to link this library with independent modules to produce an
|
|---|
| 27 | executable, regardless of the license terms of these independent
|
|---|
| 28 | modules, and to copy and distribute the resulting executable under
|
|---|
| 29 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 30 | independent module, the terms and conditions of the license of that
|
|---|
| 31 | module. An independent module is a module which is not derived from
|
|---|
| 32 | or based on this library. If you modify this library, you may extend
|
|---|
| 33 | this exception to your version of the library, but you are not
|
|---|
| 34 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 35 | exception statement from your version. */
|
|---|
| 36 |
|
|---|
| 37 | package java.awt.geom;
|
|---|
| 38 |
|
|---|
| 39 | /** This class implements a rectangle with rounded corners.
|
|---|
| 40 | * @author Tom Tromey <[email protected]>
|
|---|
| 41 | * @date December 3, 2000
|
|---|
| 42 | */
|
|---|
| 43 | public abstract class RoundRectangle2D extends RectangularShape
|
|---|
| 44 | {
|
|---|
| 45 | /** Return the arc height of this round rectangle. */
|
|---|
| 46 | public abstract double getArcHeight ();
|
|---|
| 47 |
|
|---|
| 48 | /** Return the arc width of this round rectangle. */
|
|---|
| 49 | public abstract double getArcWidth ();
|
|---|
| 50 |
|
|---|
| 51 | /** Set the values of this round rectangle
|
|---|
| 52 | * @param x The x coordinate
|
|---|
| 53 | * @param y The y coordinate
|
|---|
| 54 | * @param w The width
|
|---|
| 55 | * @param h The height
|
|---|
| 56 | * @param arcWidth The arc width
|
|---|
| 57 | * @param arcHeight The arc height
|
|---|
| 58 | */
|
|---|
| 59 | public abstract void setRoundRect (double x, double y, double w, double h,
|
|---|
| 60 | double arcWidth, double arcHeight);
|
|---|
| 61 |
|
|---|
| 62 | /** Create a RoundRectangle2D. This is protected because this class
|
|---|
| 63 | * is abstract and cannot be instantiated.
|
|---|
| 64 | */
|
|---|
| 65 | protected RoundRectangle2D ()
|
|---|
| 66 | {
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /** Return true if this object contains the specified point.
|
|---|
| 70 | * @param x The x coordinate
|
|---|
| 71 | * @param y The y coordinate
|
|---|
| 72 | */
|
|---|
| 73 | public boolean contains (double x, double y)
|
|---|
| 74 | {
|
|---|
| 75 | double mx = getX ();
|
|---|
| 76 | double mw = getWidth ();
|
|---|
| 77 | if (x < mx || x >= mx + mw)
|
|---|
| 78 | return false;
|
|---|
| 79 | double my = getY ();
|
|---|
| 80 | double mh = getHeight ();
|
|---|
| 81 | if (y < my || y >= my + mh)
|
|---|
| 82 | return false;
|
|---|
| 83 |
|
|---|
| 84 | // Now check to see if the point is in range of an arc.
|
|---|
| 85 | double dy = Math.min (Math.abs (my - y), Math.abs (my + mh - y));
|
|---|
| 86 | double dx = Math.min (Math.abs (mx - x), Math.abs (mx + mw - x));
|
|---|
| 87 | double aw = getArcWidth ();
|
|---|
| 88 | double ah = getArcHeight ();
|
|---|
| 89 | if (dx > aw || dy > ah)
|
|---|
| 90 | return true;
|
|---|
| 91 |
|
|---|
| 92 | // At this point DX represents the distance from the nearest edge
|
|---|
| 93 | // of the rectangle. But we want to transform it to represent the
|
|---|
| 94 | // scaled distance from the center of the ellipse that forms the
|
|---|
| 95 | // arc. Hence this code:
|
|---|
| 96 | dy = (ah - dy) / ah;
|
|---|
| 97 | dx = (aw - dx) / aw;
|
|---|
| 98 |
|
|---|
| 99 | return dx * dx + dy * dy <= 1.0;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /** Return true if this object contains the specified rectangle
|
|---|
| 103 | * @param x The x coordinate
|
|---|
| 104 | * @param y The y coordinate
|
|---|
| 105 | * @param w The width
|
|---|
| 106 | * @param h The height
|
|---|
| 107 | */
|
|---|
| 108 | public boolean contains (double x, double y, double w, double h)
|
|---|
| 109 | {
|
|---|
| 110 | // We have to check all four points here (for ordinary rectangles
|
|---|
| 111 | // we can just check opposing corners).
|
|---|
| 112 | return (contains (x, y) && contains (x + w, h)
|
|---|
| 113 | && contains (x, y + h) && contains (x + w, y + h));
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /** Return a new path iterator which iterates over this rectangle.
|
|---|
| 117 | * @param at An affine transform to apply to the object
|
|---|
| 118 | */
|
|---|
| 119 | public PathIterator getPathIterator (AffineTransform at)
|
|---|
| 120 | {
|
|---|
| 121 | // FIXME.
|
|---|
| 122 | return null;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /** Return true if the given rectangle intersects this shape.
|
|---|
| 126 | * @param x The x coordinate
|
|---|
| 127 | * @param y The y coordinate
|
|---|
| 128 | * @param w The width
|
|---|
| 129 | * @param h The height
|
|---|
| 130 | */
|
|---|
| 131 | public boolean intersects (double x, double y, double w, double h)
|
|---|
| 132 | {
|
|---|
| 133 | // Here we can use the same code we use for an ordinary rectangle.
|
|---|
| 134 | double mx = getX ();
|
|---|
| 135 | double mw = getWidth ();
|
|---|
| 136 | if (x < mx || x >= mx + mw || x + w < mx || x + w >= mx + mw)
|
|---|
| 137 | return false;
|
|---|
| 138 | double my = getY ();
|
|---|
| 139 | double mh = getHeight ();
|
|---|
| 140 | return y >= my && y < my + mh && y + h >= my && y + h < my + mh;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /** Set the boundary of this round rectangle.
|
|---|
| 144 | * @param x The x coordinate
|
|---|
| 145 | * @param y The y coordinate
|
|---|
| 146 | * @param w The width
|
|---|
| 147 | * @param h The height
|
|---|
| 148 | */
|
|---|
| 149 | public void setFrame (double x, double y, double w, double h)
|
|---|
| 150 | {
|
|---|
| 151 | // This is a bit lame.
|
|---|
| 152 | setRoundRect (x, y, w, h, getArcWidth (), getArcHeight ());
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /** Set the values of this round rectangle to be the same as those
|
|---|
| 156 | * of the argument.
|
|---|
| 157 | * @param rr The round rectangle to copy
|
|---|
| 158 | */
|
|---|
| 159 | public void setRoundRect (RoundRectangle2D rr)
|
|---|
| 160 | {
|
|---|
| 161 | setRoundRect (rr.getX (), rr.getY (), rr.getWidth (), rr.getHeight (),
|
|---|
| 162 | rr.getArcWidth (), rr.getArcHeight ());
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /** A subclass of RoundRectangle which keeps its parameters as
|
|---|
| 166 | * floats. */
|
|---|
| 167 | public static class Float extends RoundRectangle2D
|
|---|
| 168 | {
|
|---|
| 169 | /** The height of the corner arc. */
|
|---|
| 170 | public float archeight;
|
|---|
| 171 |
|
|---|
| 172 | /** The width of the corner arc. */
|
|---|
| 173 | public float arcwidth;
|
|---|
| 174 |
|
|---|
| 175 | /** The x coordinate of this object. */
|
|---|
| 176 | public float x;
|
|---|
| 177 |
|
|---|
| 178 | /** The y coordinate of this object. */
|
|---|
| 179 | public float y;
|
|---|
| 180 |
|
|---|
| 181 | /** The width of this object. */
|
|---|
| 182 | public float width;
|
|---|
| 183 |
|
|---|
| 184 | /** The height of this object. */
|
|---|
| 185 | public float height;
|
|---|
| 186 |
|
|---|
| 187 | /** Construct a new instance, with all parameters set to 0. */
|
|---|
| 188 | public Float ()
|
|---|
| 189 | {
|
|---|
| 190 | this (0, 0, 0, 0, 0, 0);
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | /** Construct a new instance with the given arguments.
|
|---|
| 194 | * @param x The x coordinate
|
|---|
| 195 | * @param y The y coordinate
|
|---|
| 196 | * @param w The width
|
|---|
| 197 | * @param h The height
|
|---|
| 198 | * @param arcWidth The arc width
|
|---|
| 199 | * @param arcHeight The arc height
|
|---|
| 200 | */
|
|---|
| 201 | public Float (float x, float y, float w, float h,
|
|---|
| 202 | float arcWidth, float arcHeight)
|
|---|
| 203 | {
|
|---|
| 204 | this.x = x;
|
|---|
| 205 | this.y = y;
|
|---|
| 206 | this.width = w;
|
|---|
| 207 | this.height = h;
|
|---|
| 208 | this.arcwidth = arcWidth;
|
|---|
| 209 | this.archeight = arcHeight;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | public double getArcHeight ()
|
|---|
| 213 | {
|
|---|
| 214 | return archeight;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | public double getArcWidth ()
|
|---|
| 218 | {
|
|---|
| 219 | return arcwidth;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | public Rectangle2D getBounds2D ()
|
|---|
| 223 | {
|
|---|
| 224 | return new Rectangle2D.Float (x, y, width, height);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | public double getX ()
|
|---|
| 228 | {
|
|---|
| 229 | return x;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | public double getY ()
|
|---|
| 233 | {
|
|---|
| 234 | return y;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | public double getWidth ()
|
|---|
| 238 | {
|
|---|
| 239 | return width;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | public double getHeight ()
|
|---|
| 243 | {
|
|---|
| 244 | return height;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | public boolean isEmpty ()
|
|---|
| 248 | {
|
|---|
| 249 | return width <= 0 || height <= 0;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | public void setRoundRect (float x, float y, float w, float h,
|
|---|
| 253 | float arcWidth, float arcHeight)
|
|---|
| 254 | {
|
|---|
| 255 | this.x = x;
|
|---|
| 256 | this.y = y;
|
|---|
| 257 | this.width = w;
|
|---|
| 258 | this.height = h;
|
|---|
| 259 | this.arcwidth = arcWidth;
|
|---|
| 260 | this.archeight = arcHeight;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | public void setRoundRect (double x, double y, double w, double h,
|
|---|
| 264 | double arcWidth, double arcHeight)
|
|---|
| 265 | {
|
|---|
| 266 | this.x = (float) x;
|
|---|
| 267 | this.y = (float) y;
|
|---|
| 268 | this.width = (float) w;
|
|---|
| 269 | this.height = (float) h;
|
|---|
| 270 | this.arcwidth = (float) arcWidth;
|
|---|
| 271 | this.archeight = (float) arcHeight;
|
|---|
| 272 | }
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | /** A subclass of RoundRectangle which keeps its parameters as
|
|---|
| 276 | * doubles. */
|
|---|
| 277 | public static class Double extends RoundRectangle2D
|
|---|
| 278 | {
|
|---|
| 279 | /** The height of the corner arc. */
|
|---|
| 280 | public double archeight;
|
|---|
| 281 |
|
|---|
| 282 | /** The width of the corner arc. */
|
|---|
| 283 | public double arcwidth;
|
|---|
| 284 |
|
|---|
| 285 | /** The x coordinate of this object. */
|
|---|
| 286 | public double x;
|
|---|
| 287 |
|
|---|
| 288 | /** The y coordinate of this object. */
|
|---|
| 289 | public double y;
|
|---|
| 290 |
|
|---|
| 291 | /** The width of this object. */
|
|---|
| 292 | public double width;
|
|---|
| 293 |
|
|---|
| 294 | /** The height of this object. */
|
|---|
| 295 | public double height;
|
|---|
| 296 |
|
|---|
| 297 | /** Construct a new instance, with all parameters set to 0. */
|
|---|
| 298 | public Double ()
|
|---|
| 299 | {
|
|---|
| 300 | this (0, 0, 0, 0, 0, 0);
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | /** Construct a new instance with the given arguments.
|
|---|
| 304 | * @param x The x coordinate
|
|---|
| 305 | * @param y The y coordinate
|
|---|
| 306 | * @param w The width
|
|---|
| 307 | * @param h The height
|
|---|
| 308 | * @param arcWidth The arc width
|
|---|
| 309 | * @param arcHeight The arc height
|
|---|
| 310 | */
|
|---|
| 311 | public Double (double x, double y, double w, double h,
|
|---|
| 312 | double arcWidth, double arcHeight)
|
|---|
| 313 | {
|
|---|
| 314 | this.x = x;
|
|---|
| 315 | this.y = y;
|
|---|
| 316 | this.width = w;
|
|---|
| 317 | this.height = h;
|
|---|
| 318 | this.arcwidth = arcWidth;
|
|---|
| 319 | this.archeight = arcHeight;
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | public double getArcHeight ()
|
|---|
| 323 | {
|
|---|
| 324 | return archeight;
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | public double getArcWidth ()
|
|---|
| 328 | {
|
|---|
| 329 | return arcwidth;
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | public Rectangle2D getBounds2D ()
|
|---|
| 333 | {
|
|---|
| 334 | return new Rectangle2D.Double (x, y, width, height);
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | public double getX ()
|
|---|
| 338 | {
|
|---|
| 339 | return x;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | public double getY ()
|
|---|
| 343 | {
|
|---|
| 344 | return y;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | public double getWidth ()
|
|---|
| 348 | {
|
|---|
| 349 | return width;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | public double getHeight ()
|
|---|
| 353 | {
|
|---|
| 354 | return height;
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | public boolean isEmpty ()
|
|---|
| 358 | {
|
|---|
| 359 | return width <= 0 || height <= 0;
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | public void setRoundRect (double x, double y, double w, double h,
|
|---|
| 363 | double arcWidth, double arcHeight)
|
|---|
| 364 | {
|
|---|
| 365 | this.x = x;
|
|---|
| 366 | this.y = y;
|
|---|
| 367 | this.width = w;
|
|---|
| 368 | this.height = h;
|
|---|
| 369 | this.arcwidth = arcWidth;
|
|---|
| 370 | this.archeight = arcHeight;
|
|---|
| 371 | }
|
|---|
| 372 | }
|
|---|
| 373 | }
|
|---|