summaryrefslogtreecommitdiff
path: root/libjava/classpath/java/awt/geom/RoundRectangle2D.java
blob: 19a7b423700937e95b2c697e4173b318f692c4d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/* RoundRectangle2D.java -- represents a rectangle with rounded corners
   Copyright (C) 2000, 2002, 2003, 2004, 2006, Free Software Foundation

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package java.awt.geom;



/** This class implements a rectangle with rounded corners.
 * @author Tom Tromey (tromey@cygnus.com)
 * @date December 3, 2000
 */
public abstract class RoundRectangle2D extends RectangularShape
{
  /**
   * Return the arc height of this round rectangle.  The arc height and width
   * control the roundness of the corners of the rectangle.
   *
   * @return The arc height.
   *
   * @see #getArcWidth()
   */
  public abstract double getArcHeight();

  /**
   * Return the arc width of this round rectangle.  The arc width and height
   * control the roundness of the corners of the rectangle.
   *
   * @return The arc width.
   *
   * @see #getArcHeight()
   */
  public abstract double getArcWidth();

  /**
   * Set the values of this round rectangle.
   *
   * @param x The x coordinate
   * @param y The y coordinate
   * @param w The width
   * @param h The height
   * @param arcWidth The arc width
   * @param arcHeight The arc height
   */
  public abstract void setRoundRect(double x, double y, double w, double h,
                                    double arcWidth, double arcHeight);

  /**
   * Create a RoundRectangle2D.  This is protected because this class
   * is abstract and cannot be instantiated.
   */
  protected RoundRectangle2D()
  {
  }

  /**
   * Return true if this object contains the specified point.
   * @param x The x coordinate
   * @param y The y coordinate
   */
  public boolean contains(double x, double y)
  {
    double mx = getX();
    double mw = getWidth();
    if (x < mx || x >= mx + mw)
      return false;
    double my = getY();
    double mh = getHeight();
    if (y < my || y >= my + mh)
      return false;

    // Now check to see if the point is in range of an arc.
    double dy = Math.min(Math.abs(my - y), Math.abs(my + mh - y));
    double dx = Math.min(Math.abs(mx - x), Math.abs(mx + mw - x));

    // The arc dimensions are that of the corresponding ellipse
    // thus a 90 degree segment is half of that.
    double aw = getArcWidth() / 2.0;
    double ah = getArcHeight() / 2.0;
    if (dx > aw || dy > ah)
      return true;

    // At this point DX represents the distance from the nearest edge
    // of the rectangle.  But we want to transform it to represent the
    // scaled distance from the center of the ellipse that forms the
    // arc.  Hence this code:
    dy = (ah - dy) / ah;
    dx = (aw - dx) / aw;

    return dx * dx + dy * dy <= 1.0;
  }

  /**
   * Return true if this object contains the specified rectangle
   * @param x The x coordinate
   * @param y The y coordinate
   * @param w The width
   * @param h The height
   */
  public boolean contains(double x, double y, double w, double h)
  {
    // We have to check all four points here (for ordinary rectangles
    // we can just check opposing corners).
    return (contains(x, y) && contains(x, y + h) && contains(x + w, y + h)
           && contains(x + w, y));
  }

  /**
   * Return a new path iterator which iterates over this rectangle.
   *
   * @param at An affine transform to apply to the object
   */
  public PathIterator getPathIterator(final AffineTransform at)
  {
    double arcW = Math.min(getArcWidth(), getWidth());
    double arcH = Math.min(getArcHeight(), getHeight());

    // check for special cases...
    if (arcW <= 0 || arcH <= 0)
      {
        Rectangle2D r = new Rectangle2D.Double(getX(), getY(), getWidth(),
                getHeight());
        return r.getPathIterator(at);
      }
    else if (arcW >= getWidth() && arcH >= getHeight())
      {
        Ellipse2D e = new Ellipse2D.Double(getX(), getY(), getWidth(),
                getHeight());
        return e.getPathIterator(at);
      }

    // otherwise return the standard case...
    return new PathIterator()
      {
        double x = getX();
        double y = getY();
        double w = getWidth();
        double h = getHeight();
        double arcW = Math.min(getArcWidth(), w);
        double arcH = Math.min(getArcHeight(), h);
        Arc2D.Double arc = new Arc2D.Double();
        PathIterator corner;
        int step = -1;

        public int currentSegment(double[] coords)
        {
          if (corner != null) // steps 1, 3, 5 and 7
          {
            int r = corner.currentSegment(coords);
            if (r == SEG_MOVETO)
              r = SEG_LINETO;
            return r;
          }
          if (step == -1)
          {
            // move to the start position
            coords[0] = x + w - arcW / 2;
            coords[1] = y;
          }
          else if (step == 0)
          {
            // top line
            coords[0] = x + arcW / 2;
            coords[1] = y;
          }
          else if (step == 2)
          {
            // left line
            coords[0] = x;
            coords[1] = y + h - arcH / 2;
          }
          else if (step == 4)
          {
            // bottom line
            coords[0] = x + w - arcW / 2;
            coords[1] = y + h;
          }
          else if (step == 6)
          {
            // right line
              coords[0] = x + w;
              coords[1] = y + arcH / 2;
          }
          if (at != null)
            at.transform(coords, 0, coords, 0, 1);
          return step == -1 ? SEG_MOVETO : SEG_LINETO;
        }

        public int currentSegment(float[] coords) {
          if (corner != null) // steps 1, 3, 5 and 7
          {
            int r = corner.currentSegment(coords);
            if (r == SEG_MOVETO)
              r = SEG_LINETO;
            return r;
          }
          if (step == -1)
          {
            // move to the start position
            coords[0] = (float) (x + w - arcW / 2);
            coords[1] = (float) y;
          }
          else if (step == 0)
          {
            // top line
            coords[0] = (float) (x + arcW / 2);
            coords[1] = (float) y;
          }
          else if (step == 2)
          {
            // left line
            coords[0] = (float) x;
            coords[1] = (float) (y + h - arcH / 2);
          }
          else if (step == 4)
          {
            // bottom line
            coords[0] = (float) (x + w - arcW / 2);
            coords[1] = (float) (y + h);
          }
          else if (step == 6)
          {
            // right line
            coords[0] = (float) (x + w);
            coords[1] = (float) (y + arcH / 2);
          }
        if (at != null)
          at.transform(coords, 0, coords, 0, 1);
        return step == -1 ? SEG_MOVETO : SEG_LINETO;
      }

      public int getWindingRule() {
        return WIND_NON_ZERO;
      }

      public boolean isDone() {
        return step >= 8;
      }

      public void next()
      {
        if (corner != null)
          {
            corner.next();
            if (corner.isDone())
              {
                corner = null;
                step++;
              }
          }
        else
          {
            step++;
            if (step == 1)
              {
                // create top left corner
                arc.setArc(x, y, arcW, arcH, 90, 90, Arc2D.OPEN);
                corner = arc.getPathIterator(at);
              }
            else if (step == 3)
              {
                // create bottom left corner
                arc.setArc(x, y + h - arcH, arcW, arcH, 180, 90,
                        Arc2D.OPEN);
                corner = arc.getPathIterator(at);
              }
            else if (step == 5)
              {
                // create bottom right corner
                arc.setArc(x + w - arcW, y + h - arcH, arcW, arcH, 270, 90,
                        Arc2D.OPEN);
                corner = arc.getPathIterator(at);
              }
            else if (step == 7)
              {
                // create top right corner
                arc.setArc(x + w - arcW, y, arcW, arcH, 0, 90, Arc2D.OPEN);
                corner = arc.getPathIterator(at);
              }
          }
      }
    };
  }

  /**
   * Return true if the given rectangle intersects this shape.
   * @param x The x coordinate
   * @param y The y coordinate
   * @param w The width
   * @param h The height
   */
  public boolean intersects(double x, double y, double w, double h)
  {
    // Check if any corner is within the rectangle
    return (contains(x, y) || contains(x, y + h) || contains(x + w, y + h)
           || contains(x + w, y));
  }

  /**
   * Set the boundary of this round rectangle.
   * @param x The x coordinate
   * @param y The y coordinate
   * @param w The width
   * @param h The height
   */
  public void setFrame(double x, double y, double w, double h)
  {
    // This is a bit lame.
    setRoundRect(x, y, w, h, getArcWidth(), getArcHeight());
  }

  /**
   * Set the values of this round rectangle to be the same as those
   * of the argument.
   * @param rr The round rectangle to copy
   */
  public void setRoundRect(RoundRectangle2D rr)
  {
    setRoundRect(rr.getX(), rr.getY(), rr.getWidth(), rr.getHeight(),
                 rr.getArcWidth(), rr.getArcHeight());
  }

  /**
   * A subclass of RoundRectangle which keeps its parameters as
   * doubles.
   */
  public static class Double extends RoundRectangle2D
  {
    /** The height of the corner arc.  */
    public double archeight;

    /** The width of the corner arc.  */
    public double arcwidth;

    /** The x coordinate of this object.  */
    public double x;

    /** The y coordinate of this object.  */
    public double y;

    /** The width of this object.  */
    public double width;

    /** The height of this object.  */
    public double height;

    /**
     * Construct a new instance, with all parameters set to 0.
     */
    public Double()
    {
    }

    /**
     * Construct a new instance with the given arguments.
     * @param x The x coordinate
     * @param y The y coordinate
     * @param w The width
     * @param h The height
     * @param arcWidth The arc width
     * @param arcHeight The arc height
     */
    public Double(double x, double y, double w, double h, double arcWidth,
                  double arcHeight)
    {
      this.x = x;
      this.y = y;
      this.width = w;
      this.height = h;
      this.arcwidth = arcWidth;
      this.archeight = arcHeight;
    }

    public double getArcHeight()
    {
      return archeight;
    }

    public double getArcWidth()
    {
      return arcwidth;
    }

    public Rectangle2D getBounds2D()
    {
      return new Rectangle2D.Double(x, y, width, height);
    }

    public double getX()
    {
      return x;
    }

    public double getY()
    {
      return y;
    }

    public double getWidth()
    {
      return width;
    }

    public double getHeight()
    {
      return height;
    }

    public boolean isEmpty()
    {
      return width <= 0 || height <= 0;
    }

    public void setRoundRect(double x, double y, double w, double h,
                             double arcWidth, double arcHeight)
    {
      this.x = x;
      this.y = y;
      this.width = w;
      this.height = h;
      this.arcwidth = arcWidth;
      this.archeight = arcHeight;
    }
  } // class Double

  /**
   * A subclass of RoundRectangle which keeps its parameters as
   * floats.
   */
  public static class Float extends RoundRectangle2D
  {
    /** The height of the corner arc.  */
    public float archeight;

    /** The width of the corner arc.  */
    public float arcwidth;

    /** The x coordinate of this object.  */
    public float x;

    /** The y coordinate of this object.  */
    public float y;

    /** The width of this object.  */
    public float width;

    /** The height of this object.  */
    public float height;

    /**
     * Construct a new instance, with all parameters set to 0.
     */
    public Float()
    {
    }

    /**
     * Construct a new instance with the given arguments.
     * @param x The x coordinate
     * @param y The y coordinate
     * @param w The width
     * @param h The height
     * @param arcWidth The arc width
     * @param arcHeight The arc height
     */
    public Float(float x, float y, float w, float h, float arcWidth,
                 float arcHeight)
    {
      this.x = x;
      this.y = y;
      this.width = w;
      this.height = h;
      this.arcwidth = arcWidth;
      this.archeight = arcHeight;
    }

    public double getArcHeight()
    {
      return archeight;
    }

    public double getArcWidth()
    {
      return arcwidth;
    }

    public Rectangle2D getBounds2D()
    {
      return new Rectangle2D.Float(x, y, width, height);
    }

    public double getX()
    {
      return x;
    }

    public double getY()
    {
      return y;
    }

    public double getWidth()
    {
      return width;
    }

    public double getHeight()
    {
      return height;
    }

    public boolean isEmpty()
    {
      return width <= 0 || height <= 0;
    }

    /**
     * Sets the dimensions for this rounded rectangle.
     *
     * @param x  the x-coordinate of the top left corner.
     * @param y  the y-coordinate of the top left corner.
     * @param w  the width of the rectangle.
     * @param h  the height of the rectangle.
     * @param arcWidth  the arc width.
     * @param arcHeight  the arc height.
     *
     * @see #setRoundRect(double, double, double, double, double, double)
     */
    public void setRoundRect(float x, float y, float w, float h,
                             float arcWidth, float arcHeight)
    {
      this.x = x;
      this.y = y;
      this.width = w;
      this.height = h;
      this.arcwidth = arcWidth;
      this.archeight = arcHeight;
    }

    public void setRoundRect(double x, double y, double w, double h,
                             double arcWidth, double arcHeight)
    {
      this.x = (float) x;
      this.y = (float) y;
      this.width = (float) w;
      this.height = (float) h;
      this.arcwidth = (float) arcWidth;
      this.archeight = (float) arcHeight;
    }
  } // class Float
} // class RoundRectangle2D