Path: blob/master/test/jdk/sun/java2d/marlin/DashedRectTest.java
41149 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324import java.awt.BasicStroke;25import java.awt.Color;26import java.awt.Graphics2D;27import java.awt.RenderingHints;28import java.awt.image.BufferedImage;29import java.awt.image.Raster;30import java.io.File;31import java.io.IOException;32import java.util.Arrays;33import javax.imageio.ImageIO;3435/**36* Simple Dashed Rect rendering test37*38* @test39* @summary verify that dashed rectangle is properly rasterized40* @bug 820258041*/42public class DashedRectTest {4344static final boolean SAVE_IMAGE = false;4546private final static int N = 10;4748final static float DASH_LEN = 3.0f;49final static float DASH_PH = 5000f;5051final static int MAX = 100;5253public static void main(String[] args) {5455final int size = 200;5657// First display which renderer is tested:58// JDK9 only:59System.setProperty("sun.java2d.renderer.verbose", "true");6061System.out.println("DashedRectClipTest: size = " + size);6263final BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);6465final Graphics2D g2d = (Graphics2D) image.getGraphics();66try {67g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);68g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);69g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);7071g2d.setClip(0, 0, size, size);7273g2d.setBackground(Color.WHITE);74g2d.clearRect(0, 0, size, size);7576// Corrupt Marlin Dasher.dash cached array:77g2d.setColor(Color.RED);78g2d.setStroke(createBadStroke());79g2d.drawRect(20, 20, 50, 50);8081g2d.setStroke(createStroke());8283g2d.setColor(Color.BLUE);8485for (int i = 0; i < N; i++) {86final long start = System.nanoTime();8788g2d.drawRect(5, 5, MAX, MAX);8990final long time = System.nanoTime() - start;9192System.out.println("paint: duration= " + (1e-6 * time) + " ms.");93}9495if (SAVE_IMAGE) {96try {97final File file = new File("DashedRectClipTest-MAX-" + MAX + "-dashed.png");9899System.out.println("Writing file: " + file.getAbsolutePath());100ImageIO.write(image, "PNG", file);101} catch (IOException ex) {102ex.printStackTrace();103}104}105106// Check image on few pixels:107final Raster raster = image.getData();108109// 10, 5 = blue110checkPixel(raster, 10, 5, Color.BLUE.getRGB());111112} finally {113g2d.dispose();114}115}116117private static void checkPixel(final Raster raster,118final int x, final int y,119final int expected) {120121final int[] rgb = (int[]) raster.getDataElements(x, y, null);122123if (rgb[0] != expected) {124throw new IllegalStateException("bad pixel at (" + x + ", " + y125+ ") = " + rgb[0] + " expected: " + expected);126}127}128129private static BasicStroke createStroke() {130return new BasicStroke(2f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f,131new float[]{DASH_LEN}, DASH_PH) {132133};134}135136private static BasicStroke createBadStroke() {137final float[] dash = new float[100];138Arrays.fill(dash, 19.333f);139140return new BasicStroke(2f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f, dash, DASH_PH);141}142143}144145146