< prev index next >

test/java/awt/geom/Path2D/Path2DCopyConstructor.java

Print this page


   1 /*
   2  * Copyright (c) 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 import java.awt.Rectangle;
  26 import java.awt.geom.AffineTransform;
  27 import java.awt.geom.GeneralPath;
  28 import java.awt.geom.IllegalPathStateException;
  29 import java.awt.geom.Path2D;
  30 import java.awt.geom.PathIterator;
  31 import java.awt.geom.Point2D;
  32 import java.awt.geom.Rectangle2D;
  33 import java.util.Arrays;
  34 
  35 /**
  36  * @test
  37  * @bug 8076419
  38  * @summary Check Path2D copy constructor (trims arrays)
  39  *          and constructor with zero capacity

  40  * @run main Path2DCopyConstructor
  41  */
  42 public class Path2DCopyConstructor {
  43 
  44     private final static float EPSILON = 5e-6f;
  45     private final static float FLATNESS = 1e-2f;
  46 
  47     private final static AffineTransform at
  48         = AffineTransform.getScaleInstance(1.3, 2.4);
  49 
  50     private final static Rectangle2D.Double rect2d
  51         = new Rectangle2D.Double(3.2, 4.1, 5.0, 10.0);
  52 
  53     private final static Point2D.Double pt2d
  54         = new Point2D.Double(2.0, 2.5);
  55 
  56     public static boolean verbose;
  57 
  58     static void log(String msg) {
  59         if (verbose) {


 162 
 163     static Path2D addQuads(Path2D p2d) {
 164         for (int i = 0; i < 10; i++) {
 165             p2d.quadTo(1.1 * i, 1.2 * i, 1.3 * i, 1.4 * i);
 166         }
 167         return p2d;
 168     }
 169 
 170     static Path2D addMoveAndClose(Path2D p2d) {
 171         addMove(p2d);
 172         addClose(p2d);
 173         return p2d;
 174     }
 175 
 176     static Path2D addClose(Path2D p2d) {
 177         p2d.closePath();
 178         return p2d;
 179     }
 180 
 181     static void test(Path2D p2d, boolean isEmpty) {
 182         testEqual(new Path2D.Float(p2d), p2d);
 183         testEqual(new Path2D.Double(p2d), p2d);
 184         testEqual(new GeneralPath(p2d), p2d);
 185 
 186         testIterator(new Path2D.Float(p2d), p2d);
 187         testIterator(new Path2D.Double(p2d), p2d);
 188         testIterator((Path2D) p2d.clone(), p2d);
 189 
 190         testFlattening(new Path2D.Float(p2d), p2d);
 191         testFlattening(new Path2D.Double(p2d), p2d);
 192         testFlattening((Path2D) p2d.clone(), p2d);
 193 
 194         testAddMove(new Path2D.Float(p2d));
 195         testAddMove(new Path2D.Double(p2d));
 196         testAddMove((Path2D) p2d.clone());

























































 197 
 198         // These should expect exception if empty
 199         testAddLine(new Path2D.Float(p2d), isEmpty);
 200         testAddLine(new Path2D.Double(p2d), isEmpty);
 201         testAddLine((Path2D) p2d.clone(), isEmpty);
 202 
 203         testAddQuad(new Path2D.Float(p2d), isEmpty);
 204         testAddQuad(new Path2D.Double(p2d), isEmpty);
 205         testAddQuad((Path2D) p2d.clone(), isEmpty);
 206 
 207         testAddCubic(new Path2D.Float(p2d), isEmpty);
 208         testAddCubic(new Path2D.Double(p2d), isEmpty);
 209         testAddCubic((Path2D) p2d.clone(), isEmpty);
 210 
 211         testAddClose(new Path2D.Float(p2d), isEmpty);
 212         testAddClose(new Path2D.Double(p2d), isEmpty);
 213         testAddClose((Path2D) p2d.clone(), isEmpty);
 214 
 215         testGetBounds(new Path2D.Float(p2d), p2d);
 216         testGetBounds(new Path2D.Double(p2d), p2d);
 217         testGetBounds((Path2D) p2d.clone(), p2d);
 218 
 219         testTransform(new Path2D.Float(p2d));
 220         testTransform(new Path2D.Double(p2d));
 221         testTransform((Path2D) p2d.clone());
 222 
 223         testIntersect(new Path2D.Float(p2d), p2d);
 224         testIntersect(new Path2D.Double(p2d), p2d);
 225         testIntersect((Path2D) p2d.clone(), p2d);
 226 
 227         testContains(new Path2D.Float(p2d), p2d);
 228         testContains(new Path2D.Double(p2d), p2d);
 229         testContains((Path2D) p2d.clone(), p2d);
 230 
 231         testGetCurrentPoint(new Path2D.Float(p2d), p2d);
 232         testGetCurrentPoint(new Path2D.Double(p2d), p2d);
 233         testGetCurrentPoint((Path2D) p2d.clone(), p2d);





















































































































 234     }
 235 
 236     static void testEqual(Path2D pathA, Path2D pathB) {
 237         final PathIterator itA = pathA.getPathIterator(null);
 238         final PathIterator itB = pathB.getPathIterator(null);
 239 
 240         float[] coordsA = new float[6];
 241         float[] coordsB = new float[6];
 242 
 243         int n = 0;
 244         for (; !itA.isDone() && !itB.isDone(); itA.next(), itB.next(), n++) {
 245             int typeA = itA.currentSegment(coordsA);
 246             int typeB = itB.currentSegment(coordsB);
 247 
 248             if (typeA != typeB) {
 249                 throw new IllegalStateException("Path-segment[" + n + "] "
 250                     + " type are not equals [" + typeA + "|" + typeB + "] !");
 251             }
 252             if (!equalsArray(coordsA, coordsB, getLength(typeA))) {
 253                 throw new IllegalStateException("Path-segment[" + n + "] coords"


   1 /*
   2  * Copyright (c) 2015, 2017, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 import java.awt.Rectangle;
  26 import java.awt.geom.AffineTransform;
  27 import java.awt.geom.GeneralPath;
  28 import java.awt.geom.IllegalPathStateException;
  29 import java.awt.geom.Path2D;
  30 import java.awt.geom.PathIterator;
  31 import java.awt.geom.Point2D;
  32 import java.awt.geom.Rectangle2D;
  33 import java.util.Arrays;
  34 
  35 /**
  36  * @test
  37  * @bug 8076419 8078192
  38  * @summary Check Path2D copy constructor (trims arrays)
  39  *          and constructor with zero capacity
  40  *          and Path2D.trimToSize()
  41  * @run main Path2DCopyConstructor
  42  */
  43 public class Path2DCopyConstructor {
  44 
  45     private final static float EPSILON = 5e-6f;
  46     private final static float FLATNESS = 1e-2f;
  47 
  48     private final static AffineTransform at
  49         = AffineTransform.getScaleInstance(1.3, 2.4);
  50 
  51     private final static Rectangle2D.Double rect2d
  52         = new Rectangle2D.Double(3.2, 4.1, 5.0, 10.0);
  53 
  54     private final static Point2D.Double pt2d
  55         = new Point2D.Double(2.0, 2.5);
  56 
  57     public static boolean verbose;
  58 
  59     static void log(String msg) {
  60         if (verbose) {


 163 
 164     static Path2D addQuads(Path2D p2d) {
 165         for (int i = 0; i < 10; i++) {
 166             p2d.quadTo(1.1 * i, 1.2 * i, 1.3 * i, 1.4 * i);
 167         }
 168         return p2d;
 169     }
 170 
 171     static Path2D addMoveAndClose(Path2D p2d) {
 172         addMove(p2d);
 173         addClose(p2d);
 174         return p2d;
 175     }
 176 
 177     static Path2D addClose(Path2D p2d) {
 178         p2d.closePath();
 179         return p2d;
 180     }
 181 
 182     static void test(Path2D p2d, boolean isEmpty) {
 183         Path2D c;
 184         Path2D.Float pf;
 185         Path2D.Double pd;
 186         GeneralPath gp;
 187 
 188         pf = new Path2D.Float(p2d);
 189         testEqual(pf, p2d);
 190         pf.trimToSize();
 191         testEqual(pf, p2d);
 192         pd = new Path2D.Double(p2d);
 193         testEqual(pd, p2d);
 194         pd.trimToSize();
 195         testEqual(pd, p2d);
 196         c = (Path2D)p2d.clone();
 197         testEqual(c, p2d);
 198         c.trimToSize();
 199         testEqual(c, p2d);
 200         gp = new GeneralPath(p2d);
 201         testEqual(gp, p2d);
 202         gp.trimToSize();
 203         testEqual(gp, p2d);
 204 
 205         pf = new Path2D.Float(p2d);
 206         testIterator(pf, p2d);
 207         pf.trimToSize();
 208         testIterator(pf, p2d);
 209         pd = new Path2D.Double(p2d);
 210         testIterator(pd, p2d);
 211         pd.trimToSize();
 212         testIterator(pd, p2d);
 213         c = (Path2D)p2d.clone();
 214         testIterator(c, p2d);
 215         c.trimToSize();
 216         testIterator(c, p2d);
 217         gp = new GeneralPath(p2d);
 218         testIterator(gp, p2d);
 219         gp.trimToSize();
 220         testIterator(gp, p2d);
 221 
 222         pf = new Path2D.Float(p2d);
 223         testFlattening(pf, p2d);
 224         pf.trimToSize();
 225         testFlattening(pf, p2d);
 226         pd = new Path2D.Double(p2d);
 227         testFlattening(pd, p2d);
 228         pd.trimToSize();
 229         testFlattening(pd, p2d);
 230         c = (Path2D)p2d.clone();
 231         testFlattening(c, p2d);
 232         c.trimToSize();
 233         testFlattening(c, p2d);
 234         gp = new GeneralPath(p2d);
 235         testFlattening(gp, p2d);
 236         gp.trimToSize();
 237         testFlattening(gp, p2d);
 238 
 239         pf = new Path2D.Float(p2d);
 240         testAddMove(pf);
 241         pf.trimToSize();
 242         testAddMove(pf);
 243         pd = new Path2D.Double(p2d);
 244         testAddMove(pd);
 245         pd.trimToSize();
 246         testAddMove(pd);
 247         c = (Path2D)p2d.clone();
 248         testAddMove(c);
 249         c.trimToSize();
 250         testAddMove(c);
 251         gp = new GeneralPath(p2d);
 252         testAddMove(gp);
 253         gp.trimToSize();
 254         testAddMove(gp);
 255 
 256         // These should expect exception if empty
 257         pf = new Path2D.Float(p2d);
 258         testAddLine(pf, isEmpty);
 259         pf.trimToSize();
 260         testAddLine(pf, isEmpty);
 261         pd = new Path2D.Double(p2d);
 262         testAddLine(pd, isEmpty);
 263         pd.trimToSize();
 264         testAddLine(pd, isEmpty);
 265         c = (Path2D)p2d.clone();
 266         testAddLine(c, isEmpty);
 267         c.trimToSize();
 268         testAddLine(c, isEmpty);
 269         gp = new GeneralPath(p2d);
 270         testAddLine(gp, isEmpty);
 271         gp.trimToSize();
 272         testAddLine(gp, isEmpty);
 273 
 274         pf = new Path2D.Float(p2d);
 275         testAddQuad(pf, isEmpty);
 276         pf.trimToSize();
 277         testAddQuad(pf, isEmpty);
 278         pd = new Path2D.Double(p2d);
 279         testAddQuad(pd, isEmpty);
 280         pd.trimToSize();
 281         testAddQuad(pd, isEmpty);
 282         c = (Path2D)p2d.clone();
 283         testAddQuad(c, isEmpty);
 284         c.trimToSize();
 285         testAddQuad(c, isEmpty);
 286         gp = new GeneralPath(p2d);
 287         testAddQuad(gp, isEmpty);
 288         gp.trimToSize();
 289         testAddQuad(gp, isEmpty);
 290 
 291         pf = new Path2D.Float(p2d);
 292         testAddCubic(pf, isEmpty);
 293         pf.trimToSize();
 294         testAddCubic(pf, isEmpty);
 295         pd = new Path2D.Double(p2d);
 296         testAddCubic(pd, isEmpty);
 297         pd.trimToSize();
 298         testAddCubic(pd, isEmpty);
 299         c = (Path2D)p2d.clone();
 300         testAddCubic(c, isEmpty);
 301         c.trimToSize();
 302         testAddCubic(c, isEmpty);
 303         gp = new GeneralPath(p2d);
 304         testAddCubic(gp, isEmpty);
 305         gp.trimToSize();
 306         testAddCubic(gp, isEmpty);
 307 
 308         pf = new Path2D.Float(p2d);
 309         testAddClose(pf, isEmpty);
 310         pf.trimToSize();
 311         testAddClose(pf, isEmpty);
 312         pd = new Path2D.Double(p2d);
 313         testAddClose(pd, isEmpty);
 314         pd.trimToSize();
 315         testAddClose(pd, isEmpty);
 316         c = (Path2D)p2d.clone();
 317         testAddClose(c, isEmpty);
 318         c.trimToSize();
 319         testAddClose(c, isEmpty);
 320         gp = new GeneralPath(p2d);
 321         testAddClose(gp, isEmpty);
 322         gp.trimToSize();
 323         testAddClose(gp, isEmpty);
 324 
 325         pf = new Path2D.Float(p2d);
 326         testGetBounds(pf, p2d);
 327         pf.trimToSize();
 328         testGetBounds(pf, p2d);
 329         pd = new Path2D.Double(p2d);
 330         testGetBounds(pd, p2d);
 331         pd.trimToSize();
 332         testGetBounds(pd, p2d);
 333         c = (Path2D)p2d.clone();
 334         testGetBounds(c, p2d);
 335         c.trimToSize();
 336         testGetBounds(c, p2d);
 337         gp = new GeneralPath(p2d);
 338         testGetBounds(gp, p2d);
 339         gp.trimToSize();
 340         testGetBounds(gp, p2d);
 341 
 342         pf = new Path2D.Float(p2d);
 343         testTransform(pf);
 344         pf.trimToSize();
 345         testTransform(pf);
 346         pd = new Path2D.Double(p2d);
 347         testTransform(pd);
 348         pd.trimToSize();
 349         testTransform(pd);
 350         c = (Path2D)p2d.clone();
 351         testTransform(c);
 352         c.trimToSize();
 353         testTransform(c);
 354         gp = new GeneralPath(p2d);
 355         testTransform(gp);
 356         gp.trimToSize();
 357         testTransform(gp);
 358 
 359         pf = new Path2D.Float(p2d);
 360         testIntersect(pf, p2d);
 361         pf.trimToSize();
 362         testIntersect(pf, p2d);
 363         pd = new Path2D.Double(p2d);
 364         testIntersect(pd, p2d);
 365         pd.trimToSize();
 366         testIntersect(pd, p2d);
 367         c = (Path2D)p2d.clone();
 368         testIntersect(c, p2d);
 369         c.trimToSize();
 370         testIntersect(c, p2d);
 371         gp = new GeneralPath(p2d);
 372         testIntersect(gp, p2d);
 373         gp.trimToSize();
 374         testIntersect(gp, p2d);
 375 
 376         pf = new Path2D.Float(p2d);
 377         testContains(pf, p2d);
 378         pf.trimToSize();
 379         testContains(pf, p2d);
 380         pd = new Path2D.Double(p2d);
 381         testContains(pd, p2d);
 382         pd.trimToSize();
 383         testContains(pd, p2d);
 384         c = (Path2D)p2d.clone();
 385         testContains(c, p2d);
 386         c.trimToSize();
 387         testContains(c, p2d);
 388         gp = new GeneralPath(p2d);
 389         testContains(gp, p2d);
 390         gp.trimToSize();
 391         testContains(gp, p2d);
 392 
 393         pf = new Path2D.Float(p2d);
 394         testGetCurrentPoint(pf, p2d);
 395         pf.trimToSize();
 396         testGetCurrentPoint(pf, p2d);
 397         pd = new Path2D.Double(p2d);
 398         testGetCurrentPoint(pd, p2d);
 399         pd.trimToSize();
 400         testGetCurrentPoint(pd, p2d);
 401         c = (Path2D)p2d.clone();
 402         testGetCurrentPoint(c, p2d);
 403         c.trimToSize();
 404         testGetCurrentPoint(c, p2d);
 405         gp = new GeneralPath(p2d);
 406         testGetCurrentPoint(gp, p2d);
 407         gp.trimToSize();
 408         testGetCurrentPoint(gp, p2d);
 409     }
 410 
 411     static void testEqual(Path2D pathA, Path2D pathB) {
 412         final PathIterator itA = pathA.getPathIterator(null);
 413         final PathIterator itB = pathB.getPathIterator(null);
 414 
 415         float[] coordsA = new float[6];
 416         float[] coordsB = new float[6];
 417 
 418         int n = 0;
 419         for (; !itA.isDone() && !itB.isDone(); itA.next(), itB.next(), n++) {
 420             int typeA = itA.currentSegment(coordsA);
 421             int typeB = itB.currentSegment(coordsB);
 422 
 423             if (typeA != typeB) {
 424                 throw new IllegalStateException("Path-segment[" + n + "] "
 425                     + " type are not equals [" + typeA + "|" + typeB + "] !");
 426             }
 427             if (!equalsArray(coordsA, coordsB, getLength(typeA))) {
 428                 throw new IllegalStateException("Path-segment[" + n + "] coords"


< prev index next >