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