|
1 /* |
|
2 * Copyright (c) 2018, 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.BasicStroke; |
|
26 import java.awt.Color; |
|
27 import java.awt.Graphics2D; |
|
28 import java.awt.RenderingHints; |
|
29 import java.awt.image.BufferedImage; |
|
30 import java.awt.image.Raster; |
|
31 import java.io.File; |
|
32 import java.io.IOException; |
|
33 import java.util.Arrays; |
|
34 import javax.imageio.ImageIO; |
|
35 |
|
36 /** |
|
37 * Simple Dashed Rect rendering test |
|
38 * |
|
39 * @test |
|
40 * @summary verify that dashed rectangle is properly rasterized |
|
41 * @bug 8202580 |
|
42 */ |
|
43 public class DashedRectTest { |
|
44 |
|
45 static final boolean SAVE_IMAGE = false; |
|
46 |
|
47 private final static int N = 10; |
|
48 |
|
49 final static float DASH_LEN = 3.0f; |
|
50 final static float DASH_PH = 5000f; |
|
51 |
|
52 final static int MAX = 100; |
|
53 |
|
54 public static void main(String[] args) { |
|
55 |
|
56 final int size = 200; |
|
57 |
|
58 // First display which renderer is tested: |
|
59 // JDK9 only: |
|
60 System.setProperty("sun.java2d.renderer.verbose", "true"); |
|
61 |
|
62 System.out.println("DashedRectClipTest: size = " + size); |
|
63 |
|
64 final BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); |
|
65 |
|
66 final Graphics2D g2d = (Graphics2D) image.getGraphics(); |
|
67 try { |
|
68 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); |
|
69 g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); |
|
70 g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); |
|
71 |
|
72 g2d.setClip(0, 0, size, size); |
|
73 |
|
74 g2d.setBackground(Color.WHITE); |
|
75 g2d.clearRect(0, 0, size, size); |
|
76 |
|
77 // Corrupt Marlin Dasher.dash cached array: |
|
78 g2d.setColor(Color.RED); |
|
79 g2d.setStroke(createBadStroke()); |
|
80 g2d.drawRect(20, 20, 50, 50); |
|
81 |
|
82 g2d.setStroke(createStroke()); |
|
83 |
|
84 g2d.setColor(Color.BLUE); |
|
85 |
|
86 for (int i = 0; i < N; i++) { |
|
87 final long start = System.nanoTime(); |
|
88 |
|
89 g2d.drawRect(5, 5, MAX, MAX); |
|
90 |
|
91 final long time = System.nanoTime() - start; |
|
92 |
|
93 System.out.println("paint: duration= " + (1e-6 * time) + " ms."); |
|
94 } |
|
95 |
|
96 if (SAVE_IMAGE) { |
|
97 try { |
|
98 final File file = new File("DashedRectClipTest-MAX-" + MAX + "-dashed.png"); |
|
99 |
|
100 System.out.println("Writing file: " + file.getAbsolutePath()); |
|
101 ImageIO.write(image, "PNG", file); |
|
102 } catch (IOException ex) { |
|
103 ex.printStackTrace(); |
|
104 } |
|
105 } |
|
106 |
|
107 // Check image on few pixels: |
|
108 final Raster raster = image.getData(); |
|
109 |
|
110 // 10, 5 = blue |
|
111 checkPixel(raster, 10, 5, Color.BLUE.getRGB()); |
|
112 |
|
113 } finally { |
|
114 g2d.dispose(); |
|
115 } |
|
116 } |
|
117 |
|
118 private static void checkPixel(final Raster raster, |
|
119 final int x, final int y, |
|
120 final int expected) { |
|
121 |
|
122 final int[] rgb = (int[]) raster.getDataElements(x, y, null); |
|
123 |
|
124 if (rgb[0] != expected) { |
|
125 throw new IllegalStateException("bad pixel at (" + x + ", " + y |
|
126 + ") = " + rgb[0] + " expected: " + expected); |
|
127 } |
|
128 } |
|
129 |
|
130 private static BasicStroke createStroke() { |
|
131 return new BasicStroke(2f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f, |
|
132 new float[]{DASH_LEN}, DASH_PH) { |
|
133 |
|
134 }; |
|
135 } |
|
136 |
|
137 private static BasicStroke createBadStroke() { |
|
138 final float[] dash = new float[100]; |
|
139 Arrays.fill(dash, 19.333f); |
|
140 |
|
141 return new BasicStroke(2f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f, dash, DASH_PH); |
|
142 } |
|
143 |
|
144 } |