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 testEqual(pf.trimToSize(), p2d); 191 pd = new Path2D.Double(p2d); 192 testEqual(pd, p2d); 193 testEqual(pf.trimToSize(), p2d); 194 c = (Path2D)p2d.clone(); 195 testEqual(c, p2d); 196 testEqual(c.trimToSize(), p2d); 197 gp = new GeneralPath(p2d); 198 testEqual(gp, p2d); 199 testEqual(gp.trimToSize(), p2d); 200 201 pf = new Path2D.Float(p2d); 202 testIterator(pf, p2d); 203 testIterator(pf.trimToSize(), p2d); 204 pd = new Path2D.Double(p2d); 205 testIterator(pd, p2d); 206 testIterator(pf.trimToSize(), p2d); 207 c = (Path2D)p2d.clone(); 208 testIterator(c, p2d); 209 testIterator(c.trimToSize(), p2d); 210 gp = new GeneralPath(p2d); 211 testIterator(gp, p2d); 212 testIterator(gp.trimToSize(), p2d); 213 214 pf = new Path2D.Float(p2d); 215 testFlattening(pf, p2d); 216 testFlattening(pf.trimToSize(), p2d); 217 pd = new Path2D.Double(p2d); 218 testFlattening(pd, p2d); 219 testFlattening(pf.trimToSize(), p2d); 220 c = (Path2D)p2d.clone(); 221 testFlattening(c, p2d); 222 testFlattening(c.trimToSize(), p2d); 223 gp = new GeneralPath(p2d); 224 testFlattening(gp, p2d); 225 testFlattening(gp.trimToSize(), p2d); 226 227 pf = new Path2D.Float(p2d); 228 testAddMove(pf); 229 testAddMove(pf.trimToSize()); 230 pd = new Path2D.Double(p2d); 231 testAddMove(pd); 232 testAddMove(pf.trimToSize()); 233 c = (Path2D)p2d.clone(); 234 testAddMove(c); 235 testAddMove(c.trimToSize()); 236 gp = new GeneralPath(p2d); 237 testAddMove(gp); 238 testAddMove(gp.trimToSize()); 239 240 // These should expect exception if empty 241 pf = new Path2D.Float(p2d); 242 testAddLine(pf, isEmpty); 243 testAddLine(pf.trimToSize(), isEmpty); 244 pd = new Path2D.Double(p2d); 245 testAddLine(pd, isEmpty); 246 testAddLine(pf.trimToSize(), isEmpty); 247 c = (Path2D)p2d.clone(); 248 testAddLine(c, isEmpty); 249 testAddLine(c.trimToSize(), isEmpty); 250 gp = new GeneralPath(p2d); 251 testAddLine(gp, isEmpty); 252 testAddLine(gp.trimToSize(), isEmpty); 253 254 pf = new Path2D.Float(p2d); 255 testAddQuad(pf, isEmpty); 256 testAddQuad(pf.trimToSize(), isEmpty); 257 pd = new Path2D.Double(p2d); 258 testAddQuad(pd, isEmpty); 259 testAddQuad(pf.trimToSize(), isEmpty); 260 c = (Path2D)p2d.clone(); 261 testAddQuad(c, isEmpty); 262 testAddQuad(c.trimToSize(), isEmpty); 263 gp = new GeneralPath(p2d); 264 testAddQuad(gp, isEmpty); 265 testAddQuad(gp.trimToSize(), isEmpty); 266 267 pf = new Path2D.Float(p2d); 268 testAddCubic(pf, isEmpty); 269 testAddCubic(pf.trimToSize(), isEmpty); 270 pd = new Path2D.Double(p2d); 271 testAddCubic(pd, isEmpty); 272 testAddCubic(pf.trimToSize(), isEmpty); 273 c = (Path2D)p2d.clone(); 274 testAddCubic(c, isEmpty); 275 testAddCubic(c.trimToSize(), isEmpty); 276 gp = new GeneralPath(p2d); 277 testAddCubic(gp, isEmpty); 278 testAddCubic(gp.trimToSize(), isEmpty); 279 280 pf = new Path2D.Float(p2d); 281 testAddClose(pf, isEmpty); 282 testAddClose(pf.trimToSize(), isEmpty); 283 pd = new Path2D.Double(p2d); 284 testAddClose(pd, isEmpty); 285 testAddClose(pf.trimToSize(), isEmpty); 286 c = (Path2D)p2d.clone(); 287 testAddClose(c, isEmpty); 288 testAddClose(c.trimToSize(), isEmpty); 289 gp = new GeneralPath(p2d); 290 testAddClose(gp, isEmpty); 291 testAddClose(gp.trimToSize(), isEmpty); 292 293 pf = new Path2D.Float(p2d); 294 testGetBounds(pf, p2d); 295 testGetBounds(pf.trimToSize(), p2d); 296 pd = new Path2D.Double(p2d); 297 testGetBounds(pd, p2d); 298 testGetBounds(pf.trimToSize(), p2d); 299 c = (Path2D)p2d.clone(); 300 testGetBounds(c, p2d); 301 testGetBounds(c.trimToSize(), p2d); 302 gp = new GeneralPath(p2d); 303 testGetBounds(gp, p2d); 304 testGetBounds(gp.trimToSize(), p2d); 305 306 pf = new Path2D.Float(p2d); 307 testTransform(pf); 308 testTransform(pf.trimToSize()); 309 pd = new Path2D.Double(p2d); 310 testTransform(pd); 311 testTransform(pf.trimToSize()); 312 c = (Path2D)p2d.clone(); 313 testTransform(c); 314 testTransform(c.trimToSize()); 315 gp = new GeneralPath(p2d); 316 testTransform(gp); 317 testTransform(gp.trimToSize()); 318 319 pf = new Path2D.Float(p2d); 320 testIntersect(pf, p2d); 321 testIntersect(pf.trimToSize(), p2d); 322 pd = new Path2D.Double(p2d); 323 testIntersect(pd, p2d); 324 testIntersect(pf.trimToSize(), p2d); 325 c = (Path2D)p2d.clone(); 326 testIntersect(c, p2d); 327 testIntersect(c.trimToSize(), p2d); 328 gp = new GeneralPath(p2d); 329 testIntersect(gp, p2d); 330 testIntersect(gp.trimToSize(), p2d); 331 332 pf = new Path2D.Float(p2d); 333 testContains(pf, p2d); 334 testContains(pf.trimToSize(), p2d); 335 pd = new Path2D.Double(p2d); 336 testContains(pd, p2d); 337 testContains(pf.trimToSize(), p2d); 338 c = (Path2D)p2d.clone(); 339 testContains(c, p2d); 340 testContains(c.trimToSize(), p2d); 341 gp = new GeneralPath(p2d); 342 testContains(gp, p2d); 343 testContains(gp.trimToSize(), p2d); 344 345 pf = new Path2D.Float(p2d); 346 testGetCurrentPoint(pf, p2d); 347 testGetCurrentPoint(pf.trimToSize(), p2d); 348 pd = new Path2D.Double(p2d); 349 testGetCurrentPoint(pd, p2d); 350 testGetCurrentPoint(pf.trimToSize(), p2d); 351 c = (Path2D)p2d.clone(); 352 testGetCurrentPoint(c, p2d); 353 testGetCurrentPoint(c.trimToSize(), p2d); 354 gp = new GeneralPath(p2d); 355 testGetCurrentPoint(gp, p2d); 356 testGetCurrentPoint(gp.trimToSize(), p2d); 357 } 358 359 static void testEqual(Path2D pathA, Path2D pathB) { 360 final PathIterator itA = pathA.getPathIterator(null); 361 final PathIterator itB = pathB.getPathIterator(null); 362 363 float[] coordsA = new float[6]; 364 float[] coordsB = new float[6]; 365 366 int n = 0; 367 for (; !itA.isDone() && !itB.isDone(); itA.next(), itB.next(), n++) { 368 int typeA = itA.currentSegment(coordsA); 369 int typeB = itB.currentSegment(coordsB); 370 371 if (typeA != typeB) { 372 throw new IllegalStateException("Path-segment[" + n + "] " 373 + " type are not equals [" + typeA + "|" + typeB + "] !"); 374 } 375 if (!equalsArray(coordsA, coordsB, getLength(typeA))) { 376 throw new IllegalStateException("Path-segment[" + n + "] coords" |