Path: blob/master/test/jdk/java/awt/BasicStroke/DashZeroWidth.java
41149 views
/*1* Copyright (c) 2003, 2019, 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.IndexColorModel;32import java.awt.image.VolatileImage;3334import static java.awt.image.BufferedImage.TYPE_INT_ARGB;3536/**37* @test38* @bug 4779211 8019816 819841139* @key headful40* @summary REGRESSION: 1.4 Dashed lines disappear if BasicStroke width=0.041* @run main/othervm -Dsun.java2d.uiScale=1 DashZeroWidth42*/43public final class DashZeroWidth {4445public static void main(final String[] args) {46BufferedImage img = new BufferedImage(200, 40, TYPE_INT_ARGB);47draw(img);48validate(img);4950GraphicsConfiguration gc =51GraphicsEnvironment.getLocalGraphicsEnvironment()52.getDefaultScreenDevice().getDefaultConfiguration();53if (gc.getColorModel() instanceof IndexColorModel) {54System.err.println("Skipping VolatileImage because of IndexColorModel");55return;56}5758VolatileImage vi = gc.createCompatibleVolatileImage(200, 40);59BufferedImage snapshot;60int attempt = 0;61while (true) {62if (++attempt > 10) {63throw new RuntimeException("Too many attempts: " + attempt);64}65vi.validate(gc);66draw(vi);67snapshot = vi.getSnapshot();68if (!vi.contentsLost()) {69break;70}71}72validate(snapshot);73}7475private static void draw(final Image img) {76float[] dashes = {10.0f, 10.0f};77BasicStroke bs = new BasicStroke(0.0f, BasicStroke.CAP_BUTT,78BasicStroke.JOIN_BEVEL, 10.0f, dashes,790.0f);80Graphics2D g = (Graphics2D) img.getGraphics();81g.setColor(Color.WHITE);82g.fillRect(0, 0, 200, 40);83Line2D line = new Line2D.Double(20, 20, 180, 20);84g.setColor(Color.BLACK);85g.setStroke(bs);86g.draw(line);87g.dispose();88}8990private static void validate(final BufferedImage img) {91int black = Color.black.getRGB();92int white = Color.white.getRGB();93int pointB = img.getRGB(25, 20); // point on the dash94int pointW = img.getRGB(35, 20); // point in the gap95if (pointB != black || pointW != white) {96throw new RuntimeException("Line should be visible and dashed");97}98}99}100101102