jdk/src/share/classes/javax/swing/text/html/Map.java
author darcy
Wed, 22 Jan 2014 23:20:58 -0800
changeset 22567 5816a47fa4dd
parent 5506 202f599c92aa
child 23697 e556a715949f
permissions -rw-r--r--
8032047: Fix static lint warnings in client libraries 8032048: Add static lint warning to build of jdk repository Reviewed-by: pchelko, serb, erikj
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: 1639
diff changeset
     2
 * Copyright (c) 1998, 2008, 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: 1639
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: 1639
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: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text.html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.Polygon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.text.AttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Map is used to represent a map element that is part of an HTML document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Once a Map has been created, and any number of areas have been added,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * you can test if a point falls inside the map via the contains method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author  Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
class Map implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /** Name of the Map. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private String           name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /** An array of AttributeSets. */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    44
    private Vector<AttributeSet>           areaAttributes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /** An array of RegionContainments, will slowly grow to match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * length of areaAttributes as needed. */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    47
    private Vector<RegionContainment>           areas;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public Map() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public Map(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Returns the name of the Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Defines a region of the Map, based on the passed in AttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public void addArea(AttributeSet as) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (as == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (areaAttributes == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    71
            areaAttributes = new Vector<AttributeSet>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        areaAttributes.addElement(as.copyAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Removes the previously created area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public void removeArea(AttributeSet as) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (as != null && areaAttributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            int numAreas = (areas != null) ? areas.size() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            for (int counter = areaAttributes.size() - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                 counter--) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    84
                if (areaAttributes.elementAt(counter).isEqual(as)){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    areaAttributes.removeElementAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    if (counter < numAreas) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        areas.removeElementAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Returns the AttributeSets representing the differet areas of the Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public AttributeSet[] getAreas() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int numAttributes = (areaAttributes != null) ? areaAttributes.size() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                            0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (numAttributes != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            AttributeSet[]    retValue = new AttributeSet[numAttributes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            areaAttributes.copyInto(retValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Returns the AttributeSet that contains the passed in location,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <code>x</code>, <code>y</code>. <code>width</code>, <code>height</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * gives the size of the region the map is defined over. If a matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * area is found, the AttribueSet for it is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public AttributeSet getArea(int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        int      numAttributes = (areaAttributes != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                 areaAttributes.size() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (numAttributes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            int      numAreas = (areas != null) ? areas.size() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (areas == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   123
                areas = new Vector<RegionContainment>(numAttributes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            for (int counter = 0; counter < numAttributes; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                if (counter >= numAreas) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    areas.addElement(createRegionContainment
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   128
                            (areaAttributes.elementAt(counter)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   130
                RegionContainment rc = areas.elementAt(counter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                if (rc != null && rc.contains(x, y, width, height)) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   132
                    return areaAttributes.elementAt(counter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Creates and returns an instance of RegionContainment that can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * used to test if a particular point lies inside a region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    protected RegionContainment createRegionContainment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                  (AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        Object     shape = attributes.getAttribute(HTML.Attribute.SHAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (shape == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            shape = "rect";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (shape instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            String                shapeString = ((String)shape).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            RegionContainment     rc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if (shapeString.equals("rect")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    rc = new RectangleRegionContainment(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                else if (shapeString.equals("circle")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    rc = new CircleRegionContainment(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                else if (shapeString.equals("poly")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    rc = new PolygonRegionContainment(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                else if (shapeString.equals("default")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    rc = DefaultRegionContainment.sharedInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            } catch (RuntimeException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                // Something wrong with attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                rc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Creates and returns an array of integers from the String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <code>stringCoords</code>. If one of the values represents a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * % the returned value with be negative. If a parse error results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * from trying to parse one of the numbers null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    static protected int[] extractCoords(Object stringCoords) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (stringCoords == null || !(stringCoords instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        StringTokenizer    st = new StringTokenizer((String)stringCoords,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                                    ", \t\n\r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        int[]              retValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        int                numCoords = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        while(st.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            String         token = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            int            scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (token.endsWith("%")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                scale = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                token = token.substring(0, token.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                scale = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                int       intValue = Integer.parseInt(token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                if (retValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    retValue = new int[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                else if(numCoords == retValue.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    int[]    temp = new int[retValue.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    System.arraycopy(retValue, 0, temp, 0, retValue.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    retValue = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                retValue[numCoords++] = intValue * scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (numCoords > 0 && numCoords != retValue.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            int[]    temp = new int[numCoords];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            System.arraycopy(retValue, 0, temp, 0, numCoords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            retValue = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Defines the interface used for to check if a point is inside a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    interface RegionContainment {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         * Returns true if the location <code>x</code>, <code>y</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
         * falls inside the region defined in the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         * <code>width</code>, <code>height</code> is the size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
         * the enclosing region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        public boolean contains(int x, int y, int width, int height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Used to test for containment in a rectangular region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    static class RectangleRegionContainment implements RegionContainment {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        /** Will be non-null if one of the values is a percent, and any value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         * that is non null indicates it is a percent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         * (order is x, y, width, height). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        float[]       percents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        /** Last value of width passed in. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int           lastWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        /** Last value of height passed in. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        int           lastHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        /** Top left. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        int           x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        int           y0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        /** Bottom right. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        int           x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        int           y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        public RectangleRegionContainment(AttributeSet as) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            int[]    coords = Map.extractCoords(as.getAttribute(HTML.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                                           Attribute.COORDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            percents = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (coords == null || coords.length != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                throw new RuntimeException("Unable to parse rectangular area");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                x0 = coords[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                y0 = coords[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                x1 = coords[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                y1 = coords[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                if (x0 < 0 || y0 < 0 || x1 < 0 || y1 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    percents = new float[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    lastWidth = lastHeight = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    for (int counter = 0; counter < 4; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        if (coords[counter] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                            percents[counter] = Math.abs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                        (coords[counter]) / 100.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                            percents[counter] = -1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        public boolean contains(int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (percents == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                return contains(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (lastWidth != width || lastHeight != height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                lastWidth = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                lastHeight = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                if (percents[0] != -1.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    x0 = (int)(percents[0] * width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                if (percents[1] != -1.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    y0 = (int)(percents[1] * height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                if (percents[2] != -1.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    x1 = (int)(percents[2] * width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                if (percents[3] != -1.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    y1 = (int)(percents[3] * height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return contains(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        public boolean contains(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return ((x >= x0 && x <= x1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    (y >= y0 && y <= y1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Used to test for containment in a polygon region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    static class PolygonRegionContainment extends Polygon implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                 RegionContainment {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        /** If any value is a percent there will be an entry here for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         * percent value. Use percentIndex to find out the index for it. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        float[]           percentValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        int[]             percentIndexs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        /** Last value of width passed in. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        int               lastWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        /** Last value of height passed in. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        int               lastHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        public PolygonRegionContainment(AttributeSet as) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            int[]    coords = Map.extractCoords(as.getAttribute(HTML.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                                                COORDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            if (coords == null || coords.length == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                coords.length % 2 != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                throw new RuntimeException("Unable to parse polygon area");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                int        numPercents = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                lastWidth = lastHeight = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                for (int counter = coords.length - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                     counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    if (coords[counter] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        numPercents++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                if (numPercents > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    percentIndexs = new int[numPercents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    percentValues = new float[numPercents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    for (int counter = coords.length - 1, pCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                         counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                        if (coords[counter] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                            percentValues[pCounter] = coords[counter] /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                                      -100.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                            percentIndexs[pCounter] = counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                            pCounter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    percentIndexs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    percentValues = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                npoints = coords.length / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                xpoints = new int[npoints];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                ypoints = new int[npoints];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                for (int counter = 0; counter < npoints; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    xpoints[counter] = coords[counter + counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    ypoints[counter] = coords[counter + counter + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        public boolean contains(int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (percentValues == null || (lastWidth == width &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                          lastHeight == height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                return contains(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            // Force the bounding box to be recalced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            bounds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            lastWidth = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            lastHeight = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            float fWidth = (float)width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            float fHeight = (float)height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            for (int counter = percentValues.length - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                 counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                if (percentIndexs[counter] % 2 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    // x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    xpoints[percentIndexs[counter] / 2] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                            (int)(percentValues[counter] * fWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    // y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    ypoints[percentIndexs[counter] / 2] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                            (int)(percentValues[counter] * fHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            return contains(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Used to test for containment in a circular region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    static class CircleRegionContainment implements RegionContainment {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        /** X origin of the circle. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        int           x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        /** Y origin of the circle. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        int           y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        /** Radius of the circle. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        int           radiusSquared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        /** Non-null indicates one of the values represents a percent. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        float[]       percentValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        /** Last value of width passed in. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        int           lastWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        /** Last value of height passed in. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        int           lastHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        public CircleRegionContainment(AttributeSet as) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            int[]    coords = Map.extractCoords(as.getAttribute(HTML.Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                                                                COORDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if (coords == null || coords.length != 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                throw new RuntimeException("Unable to parse circular area");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            x = coords[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            y = coords[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            radiusSquared = coords[2] * coords[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            if (coords[0] < 0 || coords[1] < 0 || coords[2] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                lastWidth = lastHeight = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                percentValues = new float[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                for (int counter = 0; counter < 3; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    if (coords[counter] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        percentValues[counter] = coords[counter] /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                                                 -100.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        percentValues[counter] = -1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                percentValues = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        public boolean contains(int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            if (percentValues != null && (lastWidth != width ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                                          lastHeight != height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                int      newRad = Math.min(width, height) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                lastWidth = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                lastHeight = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                if (percentValues[0] != -1.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    this.x = (int)(percentValues[0] * width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                if (percentValues[1] != -1.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    this.y = (int)(percentValues[1] * height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                if (percentValues[2] != -1.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    radiusSquared = (int)(percentValues[2] *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                                   Math.min(width, height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    radiusSquared *= radiusSquared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return (((x - this.x) * (x - this.x) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                     (y - this.y) * (y - this.y)) <= radiusSquared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * An implementation that will return true if the x, y location is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * inside a rectangle defined by origin 0, 0, and width equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * width passed in, and height equal to height passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    static class DefaultRegionContainment implements RegionContainment {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        /** A global shared instance. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        static DefaultRegionContainment  si = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        public static DefaultRegionContainment sharedInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if (si == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                si = new DefaultRegionContainment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            return si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        public boolean contains(int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            return (x <= width && x >= 0 && y >= 0 && y <= width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
}