Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/BasicStroke/DashZeroWidth.java
38813 views
/*1* Copyright (c) 2003, 2017, 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*/2223import java.awt.BasicStroke;24import java.awt.Color;25import java.awt.Graphics2D;26import java.awt.GraphicsConfiguration;27import java.awt.GraphicsEnvironment;28import java.awt.Image;29import java.awt.geom.Line2D;30import java.awt.image.BufferedImage;31import java.awt.image.VolatileImage;3233import static java.awt.image.BufferedImage.TYPE_INT_ARGB;3435/**36* @test37* @bug 4779211 801981638* @summary REGRESSION: 1.4 Dashed lines disappear if BasicStroke width=0.039* @run main/othervm -Dsun.java2d.uiScale=1 DashZeroWidth40*/41public final class DashZeroWidth {4243public static void main(final String[] args) {44BufferedImage img = new BufferedImage(200, 40, TYPE_INT_ARGB);45draw(img);46validate(img);4748if (GraphicsEnvironment.isHeadless()) {49return;50}5152GraphicsConfiguration gc =53GraphicsEnvironment.getLocalGraphicsEnvironment()54.getDefaultScreenDevice().getDefaultConfiguration();5556VolatileImage vi = gc.createCompatibleVolatileImage(200, 40);57BufferedImage snapshot;58int attempt = 0;59while (true) {60if (++attempt > 10) {61throw new RuntimeException("Too many attempts: " + attempt);62}63vi.validate(gc);64draw(vi);65snapshot = vi.getSnapshot();66if (!vi.contentsLost()) {67break;68}69}70validate(snapshot);71}7273private static void draw(final Image img) {74float[] dashes = {10.0f, 10.0f};75BasicStroke bs = new BasicStroke(0.0f, BasicStroke.CAP_BUTT,76BasicStroke.JOIN_BEVEL, 10.0f, dashes,770.0f);78Graphics2D g = (Graphics2D) img.getGraphics();79g.setColor(Color.WHITE);80g.fillRect(0, 0, 200, 40);81Line2D line = new Line2D.Double(20, 20, 180, 20);82g.setColor(Color.BLACK);83g.setStroke(bs);84g.draw(line);85g.dispose();86}8788private static void validate(final BufferedImage img) {89int black = Color.black.getRGB();90int white = Color.white.getRGB();91int pointB = img.getRGB(25, 20); // point on the dash92int pointW = img.getRGB(35, 20); // point in the gap93if (pointB != black || pointW != white) {94throw new RuntimeException("Line should be visible and dashed");95}96}97}9899100