< prev index next >

src/java.desktop/share/classes/java/awt/Polygon.java

Print this page


   1 /*
   2  * Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  65      * This value can be 0.
  66      *
  67      * @serial
  68      * @see #addPoint(int, int)
  69      * @since 1.0
  70      */
  71     public int npoints;
  72 
  73     /**
  74      * The array of X coordinates.  The number of elements in
  75      * this array might be more than the number of X coordinates
  76      * in this {@code Polygon}.  The extra elements allow new points
  77      * to be added to this {@code Polygon} without re-creating this
  78      * array.  The value of {@link #npoints npoints} is equal to the
  79      * number of valid points in this {@code Polygon}.
  80      *
  81      * @serial
  82      * @see #addPoint(int, int)
  83      * @since 1.0
  84      */
  85     public int xpoints[];
  86 
  87     /**
  88      * The array of Y coordinates.  The number of elements in
  89      * this array might be more than the number of Y coordinates
  90      * in this {@code Polygon}.  The extra elements allow new points
  91      * to be added to this {@code Polygon} without re-creating this
  92      * array.  The value of {@code npoints} is equal to the
  93      * number of valid points in this {@code Polygon}.
  94      *
  95      * @serial
  96      * @see #addPoint(int, int)
  97      * @since 1.0
  98      */
  99     public int ypoints[];
 100 
 101     /**
 102      * The bounds of this {@code Polygon}.
 103      * This value can be null.
 104      *
 105      * @serial
 106      * @see #getBoundingBox()
 107      * @see #getBounds()
 108      * @since 1.0
 109      */
 110     protected Rectangle bounds;
 111 
 112     /*
 113      * JDK 1.1 serialVersionUID
 114      */
 115     private static final long serialVersionUID = -6460061437900069969L;
 116 
 117     /*
 118      * Default length for xpoints and ypoints.
 119      */


 127         xpoints = new int[MIN_LENGTH];
 128         ypoints = new int[MIN_LENGTH];
 129     }
 130 
 131     /**
 132      * Constructs and initializes a {@code Polygon} from the specified
 133      * parameters.
 134      * @param xpoints an array of X coordinates
 135      * @param ypoints an array of Y coordinates
 136      * @param npoints the total number of points in the
 137      *                          {@code Polygon}
 138      * @exception  NegativeArraySizeException if the value of
 139      *                       {@code npoints} is negative.
 140      * @exception  IndexOutOfBoundsException if {@code npoints} is
 141      *             greater than the length of {@code xpoints}
 142      *             or the length of {@code ypoints}.
 143      * @exception  NullPointerException if {@code xpoints} or
 144      *             {@code ypoints} is {@code null}.
 145      * @since 1.0
 146      */
 147     public Polygon(int xpoints[], int ypoints[], int npoints) {
 148         // Fix 4489009: should throw IndexOutOfBoundsException instead
 149         // of OutOfMemoryError if npoints is huge and > {x,y}points.length
 150         if (npoints > xpoints.length || npoints > ypoints.length) {
 151             throw new IndexOutOfBoundsException("npoints > xpoints.length || "+
 152                                                 "npoints > ypoints.length");
 153         }
 154         // Fix 6191114: should throw NegativeArraySizeException with
 155         // negative npoints
 156         if (npoints < 0) {
 157             throw new NegativeArraySizeException("npoints < 0");
 158         }
 159         // Fix 6343431: Applet compatibility problems if arrays are not
 160         // exactly npoints in length
 161         this.npoints = npoints;
 162         this.xpoints = Arrays.copyOf(xpoints, npoints);
 163         this.ypoints = Arrays.copyOf(ypoints, npoints);
 164     }
 165 
 166     /**
 167      * Resets this {@code Polygon} object to an empty polygon.


 209      * @param deltaY the amount to translate along the Y axis
 210      * @since 1.1
 211      */
 212     public void translate(int deltaX, int deltaY) {
 213         for (int i = 0; i < npoints; i++) {
 214             xpoints[i] += deltaX;
 215             ypoints[i] += deltaY;
 216         }
 217         if (bounds != null) {
 218             bounds.translate(deltaX, deltaY);
 219         }
 220     }
 221 
 222     /*
 223      * Calculates the bounding box of the points passed to the constructor.
 224      * Sets {@code bounds} to the result.
 225      * @param xpoints[] array of <i>x</i> coordinates
 226      * @param ypoints[] array of <i>y</i> coordinates
 227      * @param npoints the total number of points
 228      */
 229     void calculateBounds(int xpoints[], int ypoints[], int npoints) {
 230         int boundsMinX = Integer.MAX_VALUE;
 231         int boundsMinY = Integer.MAX_VALUE;
 232         int boundsMaxX = Integer.MIN_VALUE;
 233         int boundsMaxY = Integer.MIN_VALUE;
 234 
 235         for (int i = 0; i < npoints; i++) {
 236             int x = xpoints[i];
 237             boundsMinX = Math.min(boundsMinX, x);
 238             boundsMaxX = Math.max(boundsMaxX, x);
 239             int y = ypoints[i];
 240             boundsMinY = Math.min(boundsMinY, y);
 241             boundsMaxY = Math.max(boundsMaxY, y);
 242         }
 243         bounds = new Rectangle(boundsMinX, boundsMinY,
 244                                boundsMaxX - boundsMinX,
 245                                boundsMaxY - boundsMinY);
 246     }
 247 
 248     /*
 249      * Resizes the bounding box to accommodate the specified coordinates.


   1 /*
   2  * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  65      * This value can be 0.
  66      *
  67      * @serial
  68      * @see #addPoint(int, int)
  69      * @since 1.0
  70      */
  71     public int npoints;
  72 
  73     /**
  74      * The array of X coordinates.  The number of elements in
  75      * this array might be more than the number of X coordinates
  76      * in this {@code Polygon}.  The extra elements allow new points
  77      * to be added to this {@code Polygon} without re-creating this
  78      * array.  The value of {@link #npoints npoints} is equal to the
  79      * number of valid points in this {@code Polygon}.
  80      *
  81      * @serial
  82      * @see #addPoint(int, int)
  83      * @since 1.0
  84      */
  85     public int[] xpoints;
  86 
  87     /**
  88      * The array of Y coordinates.  The number of elements in
  89      * this array might be more than the number of Y coordinates
  90      * in this {@code Polygon}.  The extra elements allow new points
  91      * to be added to this {@code Polygon} without re-creating this
  92      * array.  The value of {@code npoints} is equal to the
  93      * number of valid points in this {@code Polygon}.
  94      *
  95      * @serial
  96      * @see #addPoint(int, int)
  97      * @since 1.0
  98      */
  99     public int[] ypoints;
 100 
 101     /**
 102      * The bounds of this {@code Polygon}.
 103      * This value can be null.
 104      *
 105      * @serial
 106      * @see #getBoundingBox()
 107      * @see #getBounds()
 108      * @since 1.0
 109      */
 110     protected Rectangle bounds;
 111 
 112     /*
 113      * JDK 1.1 serialVersionUID
 114      */
 115     private static final long serialVersionUID = -6460061437900069969L;
 116 
 117     /*
 118      * Default length for xpoints and ypoints.
 119      */


 127         xpoints = new int[MIN_LENGTH];
 128         ypoints = new int[MIN_LENGTH];
 129     }
 130 
 131     /**
 132      * Constructs and initializes a {@code Polygon} from the specified
 133      * parameters.
 134      * @param xpoints an array of X coordinates
 135      * @param ypoints an array of Y coordinates
 136      * @param npoints the total number of points in the
 137      *                          {@code Polygon}
 138      * @exception  NegativeArraySizeException if the value of
 139      *                       {@code npoints} is negative.
 140      * @exception  IndexOutOfBoundsException if {@code npoints} is
 141      *             greater than the length of {@code xpoints}
 142      *             or the length of {@code ypoints}.
 143      * @exception  NullPointerException if {@code xpoints} or
 144      *             {@code ypoints} is {@code null}.
 145      * @since 1.0
 146      */
 147     public Polygon(int[] xpoints, int[] ypoints, int npoints) {
 148         // Fix 4489009: should throw IndexOutOfBoundsException instead
 149         // of OutOfMemoryError if npoints is huge and > {x,y}points.length
 150         if (npoints > xpoints.length || npoints > ypoints.length) {
 151             throw new IndexOutOfBoundsException("npoints > xpoints.length || "+
 152                                                 "npoints > ypoints.length");
 153         }
 154         // Fix 6191114: should throw NegativeArraySizeException with
 155         // negative npoints
 156         if (npoints < 0) {
 157             throw new NegativeArraySizeException("npoints < 0");
 158         }
 159         // Fix 6343431: Applet compatibility problems if arrays are not
 160         // exactly npoints in length
 161         this.npoints = npoints;
 162         this.xpoints = Arrays.copyOf(xpoints, npoints);
 163         this.ypoints = Arrays.copyOf(ypoints, npoints);
 164     }
 165 
 166     /**
 167      * Resets this {@code Polygon} object to an empty polygon.


 209      * @param deltaY the amount to translate along the Y axis
 210      * @since 1.1
 211      */
 212     public void translate(int deltaX, int deltaY) {
 213         for (int i = 0; i < npoints; i++) {
 214             xpoints[i] += deltaX;
 215             ypoints[i] += deltaY;
 216         }
 217         if (bounds != null) {
 218             bounds.translate(deltaX, deltaY);
 219         }
 220     }
 221 
 222     /*
 223      * Calculates the bounding box of the points passed to the constructor.
 224      * Sets {@code bounds} to the result.
 225      * @param xpoints[] array of <i>x</i> coordinates
 226      * @param ypoints[] array of <i>y</i> coordinates
 227      * @param npoints the total number of points
 228      */
 229     void calculateBounds(int[] xpoints, int[] ypoints, int npoints) {
 230         int boundsMinX = Integer.MAX_VALUE;
 231         int boundsMinY = Integer.MAX_VALUE;
 232         int boundsMaxX = Integer.MIN_VALUE;
 233         int boundsMaxY = Integer.MIN_VALUE;
 234 
 235         for (int i = 0; i < npoints; i++) {
 236             int x = xpoints[i];
 237             boundsMinX = Math.min(boundsMinX, x);
 238             boundsMaxX = Math.max(boundsMaxX, x);
 239             int y = ypoints[i];
 240             boundsMinY = Math.min(boundsMinY, y);
 241             boundsMaxY = Math.max(boundsMaxY, y);
 242         }
 243         bounds = new Rectangle(boundsMinX, boundsMinY,
 244                                boundsMaxX - boundsMinX,
 245                                boundsMaxY - boundsMinY);
 246     }
 247 
 248     /*
 249      * Resizes the bounding box to accommodate the specified coordinates.


< prev index next >