1 /* 2 * Copyright (c) 2003, 2019, 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 import java.awt.BasicStroke; 25 import java.awt.Color; 26 import java.awt.Graphics2D; 27 import java.awt.GraphicsConfiguration; 28 import java.awt.GraphicsEnvironment; 29 import java.awt.Image; 30 import java.awt.geom.Line2D; 31 import java.awt.image.BufferedImage; 32 import java.awt.image.IndexColorModel; 33 import java.awt.image.VolatileImage; 34 35 import static java.awt.image.BufferedImage.TYPE_INT_ARGB; 36 37 /** 38 * @test 39 * @bug 4779211 8019816 8198411 40 * @summary REGRESSION: 1.4 Dashed lines disappear if BasicStroke width=0.0 41 * @run main/othervm -Dsun.java2d.uiScale=1 DashZeroWidth 42 */ 43 public final class DashZeroWidth { 44 45 public static void main(final String[] args) { 46 BufferedImage img = new BufferedImage(200, 40, TYPE_INT_ARGB); 47 draw(img); 48 validate(img); 49 50 if (GraphicsEnvironment.isHeadless()) { 51 return; 52 } 53 54 GraphicsConfiguration gc = 55 GraphicsEnvironment.getLocalGraphicsEnvironment() 56 .getDefaultScreenDevice().getDefaultConfiguration(); 57 if (gc.getColorModel() instanceof IndexColorModel) { 58 return; 59 } 60 61 VolatileImage vi = gc.createCompatibleVolatileImage(200, 40); 62 BufferedImage snapshot; 63 int attempt = 0; 64 while (true) { 65 if (++attempt > 10) { 66 throw new RuntimeException("Too many attempts: " + attempt); 67 } 68 vi.validate(gc); 69 draw(vi); 70 snapshot = vi.getSnapshot(); 71 if (!vi.contentsLost()) { 72 break; 73 } 74 } 75 validate(snapshot); 76 } 77 78 private static void draw(final Image img) { 79 float[] dashes = {10.0f, 10.0f}; 80 BasicStroke bs = new BasicStroke(0.0f, BasicStroke.CAP_BUTT, 81 BasicStroke.JOIN_BEVEL, 10.0f, dashes, 82 0.0f); 83 Graphics2D g = (Graphics2D) img.getGraphics(); 84 g.setColor(Color.WHITE); 85 g.fillRect(0, 0, 200, 40); 86 Line2D line = new Line2D.Double(20, 20, 180, 20); 87 g.setColor(Color.BLACK); 88 g.setStroke(bs); 89 g.draw(line); 90 g.dispose(); 91 } 92 93 private static void validate(final BufferedImage img) { 94 int black = Color.black.getRGB(); 95 int white = Color.white.getRGB(); 96 int pointB = img.getRGB(25, 20); // point on the dash 97 int pointW = img.getRGB(35, 20); // point in the gap 98 if (pointB != black || pointW != white) { 99 throw new RuntimeException("Line should be visible and dashed"); 100 } 101 } 102 }