jdk/src/share/classes/java/awt/geom/Rectangle2D.java
author henryjen
Tue, 10 Jun 2014 16:18:54 -0700
changeset 24865 09b1d992ca72
parent 5506 202f599c92aa
permissions -rw-r--r--
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo Reviewed-by: mduigou, lancea, alanb, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.geom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * The <code>Rectangle2D</code> class describes a rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * defined by a location {@code (x,y)} and dimension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * {@code (w x h)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class is only the abstract superclass for all objects that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * store a 2D rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * The actual storage representation of the coordinates is left to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * the subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author      Jim Graham
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public abstract class Rectangle2D extends RectangularShape {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * The bitmask that indicates that a point lies to the left of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static final int OUT_LEFT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * The bitmask that indicates that a point lies above
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static final int OUT_TOP = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * The bitmask that indicates that a point lies to the right of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static final int OUT_RIGHT = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * The bitmask that indicates that a point lies below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static final int OUT_BOTTOM = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * The <code>Float</code> class defines a rectangle specified in float
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static class Float extends Rectangle2D implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
         * The X coordinate of this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        public float x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         * The Y coordinate of this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        public float y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
         * The width of this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        public float width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         * The height of this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        public float height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         * Constructs a new <code>Rectangle2D</code>, initialized to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         * location (0.0,&nbsp;0.0) and size (0.0,&nbsp;0.0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        public Float() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         * Constructs and initializes a <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         * from the specified <code>float</code> coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         * @param x the X coordinate of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         *          of the newly constructed <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         * @param y the Y coordinate of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         *          of the newly constructed <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         * @param w the width of the newly constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         *          <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         * @param h the height of the newly constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         *          <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        public Float(float x, float y, float w, float h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            setRect(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        public double getX() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return (double) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        public double getY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return (double) y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public double getWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return (double) width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        public double getHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return (double) height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return (width <= 0.0f) || (height <= 0.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         * Sets the location and size of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         * to the specified <code>float</code> values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         * @param x the X coordinate of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         *          of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         * @param y the Y coordinate of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         *          of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         * @param w the width of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         * @param h the height of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        public void setRect(float x, float y, float w, float h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            this.x = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            this.y = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            this.width = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            this.height = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        public void setRect(double x, double y, double w, double h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            this.x = (float) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            this.y = (float) y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            this.width = (float) w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            this.height = (float) h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        public void setRect(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            this.x = (float) r.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            this.y = (float) r.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            this.width = (float) r.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            this.height = (float) r.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        public int outcode(double x, double y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
             * Note on casts to double below.  If the arithmetic of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
             * x+w or y+h is done in float, then some bits may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
             * lost if the binary exponents of x/y and w/h are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
             * similar.  By converting to double before the addition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
             * we force the addition to be carried out in double to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
             * avoid rounding error in the comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
             * See bug 4320890 for problems that this inaccuracy causes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            int out = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            if (this.width <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                out |= OUT_LEFT | OUT_RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            } else if (x < this.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                out |= OUT_LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            } else if (x > this.x + (double) this.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                out |= OUT_RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (this.height <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                out |= OUT_TOP | OUT_BOTTOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            } else if (y < this.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                out |= OUT_TOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            } else if (y > this.y + (double) this.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                out |= OUT_BOTTOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        public Rectangle2D getBounds2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return new Float(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        public Rectangle2D createIntersection(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            Rectangle2D dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (r instanceof Float) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                dest = new Rectangle2D.Float();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                dest = new Rectangle2D.Double();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            Rectangle2D.intersect(this, r, dest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        public Rectangle2D createUnion(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            Rectangle2D dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (r instanceof Float) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                dest = new Rectangle2D.Float();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                dest = new Rectangle2D.Double();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            Rectangle2D.union(this, r, dest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         * Returns the <code>String</code> representation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         * <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         * @return a <code>String</code> representing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            return getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                + "[x=" + x +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                ",y=" + y +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                ",w=" + width +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                ",h=" + height + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         * JDK 1.6 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        private static final long serialVersionUID = 3798716824173675777L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * The <code>Double</code> class defines a rectangle specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * double coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public static class Double extends Rectangle2D implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         * The X coordinate of this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        public double x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         * The Y coordinate of this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        public double y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         * The width of this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        public double width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * The height of this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        public double height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         * Constructs a new <code>Rectangle2D</code>, initialized to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
         * location (0,&nbsp;0) and size (0,&nbsp;0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        public Double() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
         * Constructs and initializes a <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
         * from the specified <code>double</code> coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         * @param x the X coordinate of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
         *          of the newly constructed <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
         * @param y the Y coordinate of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
         *          of the newly constructed <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
         * @param w the width of the newly constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
         *          <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         * @param h the height of the newly constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         *          <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        public Double(double x, double y, double w, double h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            setRect(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        public double getX() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        public double getY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            return y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        public double getWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        public double getHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            return (width <= 0.0) || (height <= 0.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        public void setRect(double x, double y, double w, double h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            this.x = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            this.y = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            this.width = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            this.height = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        public void setRect(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            this.x = r.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            this.y = r.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            this.width = r.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            this.height = r.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        public int outcode(double x, double y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            int out = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            if (this.width <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                out |= OUT_LEFT | OUT_RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            } else if (x < this.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                out |= OUT_LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            } else if (x > this.x + this.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                out |= OUT_RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            if (this.height <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                out |= OUT_TOP | OUT_BOTTOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            } else if (y < this.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                out |= OUT_TOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            } else if (y > this.y + this.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                out |= OUT_BOTTOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        public Rectangle2D getBounds2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            return new Double(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        public Rectangle2D createIntersection(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            Rectangle2D dest = new Rectangle2D.Double();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            Rectangle2D.intersect(this, r, dest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            return dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        public Rectangle2D createUnion(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            Rectangle2D dest = new Rectangle2D.Double();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            Rectangle2D.union(this, r, dest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            return dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
         * Returns the <code>String</code> representation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
         * <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
         * @return a <code>String</code> representing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
         * <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            return getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                + "[x=" + x +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                ",y=" + y +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                ",w=" + width +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                ",h=" + height + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         * JDK 1.6 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        private static final long serialVersionUID = 7771313791441850493L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * This is an abstract class that cannot be instantiated directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * Type-specific implementation subclasses are available for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * instantiation and provide a number of formats for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * the information necessary to satisfy the various accessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * methods below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @see java.awt.geom.Rectangle2D.Float
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @see java.awt.geom.Rectangle2D.Double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @see java.awt.Rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    protected Rectangle2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Sets the location and size of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * to the specified <code>double</code> values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @param x the X coordinate of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *          of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @param y the Y coordinate of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *          of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @param w the width of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @param h the height of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    public abstract void setRect(double x, double y, double w, double h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Sets this <code>Rectangle2D</code> to be the same as the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @param r the specified <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    public void setRect(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Tests if the specified line segment intersects the interior of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @param x1 the X coordinate of the start point of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *           line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @param y1 the Y coordinate of the start point of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *           line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @param x2 the X coordinate of the end point of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *           line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @param y2 the Y coordinate of the end point of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *           line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @return <code>true</code> if the specified line segment intersects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * the interior of this <code>Rectangle2D</code>; <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    public boolean intersectsLine(double x1, double y1, double x2, double y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        int out1, out2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        if ((out2 = outcode(x2, y2)) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        while ((out1 = outcode(x1, y1)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            if ((out1 & out2) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            if ((out1 & (OUT_LEFT | OUT_RIGHT)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                double x = getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                if ((out1 & OUT_RIGHT) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    x += getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                y1 = y1 + (x - x1) * (y2 - y1) / (x2 - x1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                x1 = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                double y = getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                if ((out1 & OUT_BOTTOM) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    y += getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                x1 = x1 + (y - y1) * (x2 - x1) / (y2 - y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                y1 = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * Tests if the specified line segment intersects the interior of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @param l the specified {@link Line2D} to test for intersection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * with the interior of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @return <code>true</code> if the specified <code>Line2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * intersects the interior of this <code>Rectangle2D</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    public boolean intersectsLine(Line2D l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return intersectsLine(l.getX1(), l.getY1(), l.getX2(), l.getY2());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Determines where the specified coordinates lie with respect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * to this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * This method computes a binary OR of the appropriate mask values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * indicating, for each side of this <code>Rectangle2D</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * whether or not the specified coordinates are on the same side
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * of the edge as the rest of this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @param x the specified X coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @param y the specified Y coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @return the logical OR of all appropriate out codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @see #OUT_LEFT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @see #OUT_TOP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @see #OUT_RIGHT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @see #OUT_BOTTOM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    public abstract int outcode(double x, double y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * Determines where the specified {@link Point2D} lies with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * respect to this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * This method computes a binary OR of the appropriate mask values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * indicating, for each side of this <code>Rectangle2D</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * whether or not the specified <code>Point2D</code> is on the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * side of the edge as the rest of this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @param p the specified <code>Point2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @return the logical OR of all appropriate out codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @see #OUT_LEFT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @see #OUT_TOP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @see #OUT_RIGHT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @see #OUT_BOTTOM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    public int outcode(Point2D p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return outcode(p.getX(), p.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * Sets the location and size of the outer bounds of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * <code>Rectangle2D</code> to the specified rectangular values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @param x the X coordinate of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *          of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @param y the Y coordinate of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *          of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @param w the width of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @param h the height of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    public void setFrame(double x, double y, double w, double h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        setRect(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    public Rectangle2D getBounds2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        return (Rectangle2D) clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    public boolean contains(double x, double y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        double x0 = getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        double y0 = getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        return (x >= x0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                y >= y0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                x < x0 + getWidth() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                y < y0 + getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    public boolean intersects(double x, double y, double w, double h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        if (isEmpty() || w <= 0 || h <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        double x0 = getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        double y0 = getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        return (x + w > x0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                y + h > y0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                x < x0 + getWidth() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                y < y0 + getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    public boolean contains(double x, double y, double w, double h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        if (isEmpty() || w <= 0 || h <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        double x0 = getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        double y0 = getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        return (x >= x0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                y >= y0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                (x + w) <= x0 + getWidth() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                (y + h) <= y0 + getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * Returns a new <code>Rectangle2D</code> object representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * intersection of this <code>Rectangle2D</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * @param r the <code>Rectangle2D</code> to be intersected with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * @return the largest <code>Rectangle2D</code> contained in both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *          the specified <code>Rectangle2D</code> and in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *          <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    public abstract Rectangle2D createIntersection(Rectangle2D r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * Intersects the pair of specified source <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * objects and puts the result into the specified destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * <code>Rectangle2D</code> object.  One of the source rectangles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * can also be the destination to avoid creating a third Rectangle2D
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * object, but in this case the original points of this source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * rectangle will be overwritten by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @param src1 the first of a pair of <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * objects to be intersected with each other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @param src2 the second of a pair of <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * objects to be intersected with each other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * @param dest the <code>Rectangle2D</code> that holds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * results of the intersection of <code>src1</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * <code>src2</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    public static void intersect(Rectangle2D src1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                                 Rectangle2D src2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                                 Rectangle2D dest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        double x1 = Math.max(src1.getMinX(), src2.getMinX());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        double y1 = Math.max(src1.getMinY(), src2.getMinY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        double x2 = Math.min(src1.getMaxX(), src2.getMaxX());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        double y2 = Math.min(src1.getMaxY(), src2.getMaxY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        dest.setFrame(x1, y1, x2-x1, y2-y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * Returns a new <code>Rectangle2D</code> object representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * union of this <code>Rectangle2D</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * @param r the <code>Rectangle2D</code> to be combined with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * @return the smallest <code>Rectangle2D</code> containing both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * the specified <code>Rectangle2D</code> and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    public abstract Rectangle2D createUnion(Rectangle2D r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * Unions the pair of source <code>Rectangle2D</code> objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * and puts the result into the specified destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * <code>Rectangle2D</code> object.  One of the source rectangles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * can also be the destination to avoid creating a third Rectangle2D
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * object, but in this case the original points of this source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * rectangle will be overwritten by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * @param src1 the first of a pair of <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * objects to be combined with each other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @param src2 the second of a pair of <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * objects to be combined with each other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @param dest the <code>Rectangle2D</code> that holds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * results of the union of <code>src1</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * <code>src2</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    public static void union(Rectangle2D src1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                             Rectangle2D src2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                             Rectangle2D dest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        double x1 = Math.min(src1.getMinX(), src2.getMinX());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        double y1 = Math.min(src1.getMinY(), src2.getMinY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        double x2 = Math.max(src1.getMaxX(), src2.getMaxX());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        double y2 = Math.max(src1.getMaxY(), src2.getMaxY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        dest.setFrameFromDiagonal(x1, y1, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * Adds a point, specified by the double precision arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * <code>newx</code> and <code>newy</code>, to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * <code>Rectangle2D</code>.  The resulting <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * is the smallest <code>Rectangle2D</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * contains both the original <code>Rectangle2D</code> and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * specified point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * After adding a point, a call to <code>contains</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * added point as an argument does not necessarily return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * <code>true</code>. The <code>contains</code> method does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * return <code>true</code> for points on the right or bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * edges of a rectangle. Therefore, if the added point falls on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * the left or bottom edge of the enlarged rectangle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * <code>contains</code> returns <code>false</code> for that point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * @param newx the X coordinate of the new point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * @param newy the Y coordinate of the new point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    public void add(double newx, double newy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        double x1 = Math.min(getMinX(), newx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        double x2 = Math.max(getMaxX(), newx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        double y1 = Math.min(getMinY(), newy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        double y2 = Math.max(getMaxY(), newy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        setRect(x1, y1, x2 - x1, y2 - y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * Adds the <code>Point2D</code> object <code>pt</code> to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * The resulting <code>Rectangle2D</code> is the smallest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * <code>Rectangle2D</code> that contains both the original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * <code>Rectangle2D</code> and the specified <code>Point2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * After adding a point, a call to <code>contains</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * added point as an argument does not necessarily return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * <code>true</code>. The <code>contains</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * method does not return <code>true</code> for points on the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * or bottom edges of a rectangle. Therefore, if the added point falls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * on the left or bottom edge of the enlarged rectangle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * <code>contains</code> returns <code>false</code> for that point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @param     pt the new <code>Point2D</code> to add to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    public void add(Point2D pt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        add(pt.getX(), pt.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * Adds a <code>Rectangle2D</code> object to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * <code>Rectangle2D</code>.  The resulting <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * is the union of the two <code>Rectangle2D</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * @param r the <code>Rectangle2D</code> to add to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    public void add(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        double x1 = Math.min(getMinX(), r.getMinX());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        double x2 = Math.max(getMaxX(), r.getMaxX());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        double y1 = Math.min(getMinY(), r.getMinY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        double y2 = Math.max(getMaxY(), r.getMaxY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        setRect(x1, y1, x2 - x1, y2 - y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * Returns an iteration object that defines the boundary of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * The iterator for this class is multi-threaded safe, which means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * that this <code>Rectangle2D</code> class guarantees that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * modifications to the geometry of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * object do not affect any iterations of that geometry that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * are already in process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * @param at an optional <code>AffineTransform</code> to be applied to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * the coordinates as they are returned in the iteration, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * <code>null</code> if untransformed coordinates are desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @return    the <code>PathIterator</code> object that returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *          geometry of the outline of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *          <code>Rectangle2D</code>, one segment at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    public PathIterator getPathIterator(AffineTransform at) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        return new RectIterator(this, at);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * Returns an iteration object that defines the boundary of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * flattened <code>Rectangle2D</code>.  Since rectangles are already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * flat, the <code>flatness</code> parameter is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * The iterator for this class is multi-threaded safe, which means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * that this <code>Rectangle2D</code> class guarantees that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * modifications to the geometry of this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * object do not affect any iterations of that geometry that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * are already in process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @param at an optional <code>AffineTransform</code> to be applied to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * the coordinates as they are returned in the iteration, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * <code>null</code> if untransformed coordinates are desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * @param flatness the maximum distance that the line segments used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * approximate the curved segments are allowed to deviate from any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * point on the original curve.  Since rectangles are already flat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * the <code>flatness</code> parameter is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * @return    the <code>PathIterator</code> object that returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     *          geometry of the outline of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     *          <code>Rectangle2D</code>, one segment at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        return new RectIterator(this, at);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * Returns the hashcode for this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @return the hashcode for this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        long bits = java.lang.Double.doubleToLongBits(getX());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        bits += java.lang.Double.doubleToLongBits(getY()) * 37;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        bits += java.lang.Double.doubleToLongBits(getWidth()) * 43;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        bits += java.lang.Double.doubleToLongBits(getHeight()) * 47;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        return (((int) bits) ^ ((int) (bits >> 32)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * Determines whether or not the specified <code>Object</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * equal to this <code>Rectangle2D</code>.  The specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * <code>Object</code> is equal to this <code>Rectangle2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * if it is an instance of <code>Rectangle2D</code> and if its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * location and size are the same as this <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * @param obj an <code>Object</code> to be compared with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * @return     <code>true</code> if <code>obj</code> is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     *                     of <code>Rectangle2D</code> and has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     *                     the same values; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        if (obj instanceof Rectangle2D) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            Rectangle2D r2d = (Rectangle2D) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            return ((getX() == r2d.getX()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                    (getY() == r2d.getY()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                    (getWidth() == r2d.getWidth()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                    (getHeight() == r2d.getHeight()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
}