Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/BasicStroke/DashOffset.java
41149 views
1
/*
2
* Copyright (c) 2001, 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.Graphics;
27
import java.awt.Graphics2D;
28
import java.awt.GraphicsConfiguration;
29
import java.awt.GraphicsEnvironment;
30
import java.awt.Image;
31
import java.awt.image.BufferedImage;
32
import java.awt.image.IndexColorModel;
33
import java.awt.image.VolatileImage;
34
import java.io.File;
35
import java.io.IOException;
36
import java.util.Arrays;
37
import java.util.stream.Collectors;
38
import javax.imageio.ImageIO;
39
40
import static java.awt.image.BufferedImage.TYPE_INT_RGB;
41
42
/*
43
* @test
44
* @bug 4469881 8217263 8218682
45
* @key headful
46
* @summary Verifies that dashed rectangles drawn to the screen line
47
* up with their undashed counterparts
48
* @author flar
49
* @run main/othervm -Dsun.java2d.uiScale=1 DashOffset
50
*/
51
public class DashOffset {
52
53
private static final BasicStroke dash =
54
new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
55
BasicStroke.JOIN_MITER, 10.0f,
56
new float[] {2.0f, 2.0f}, 0.0f);
57
58
private static final Color COLOR1 = Color.BLUE;
59
private static final Color COLOR2 = Color.GREEN;
60
61
private static final Color BACKGROUND = Color.WHITE;
62
63
private static final int WIDTH = 20;
64
private static final int HEIGHT = 20;
65
66
private static final int OFFSET = 2;
67
68
private static final int MAX_DASH_LENGTH = 3;
69
70
public static void main(String[] argv) throws Exception {
71
final boolean saveImage = argv.length > 0 && "-save".equals(argv[0]);
72
73
final BufferedImage img = new BufferedImage(WIDTH, HEIGHT,
74
TYPE_INT_RGB);
75
try {
76
draw(img);
77
validate(img);
78
} finally {
79
if (saveImage) {
80
save(img, "bufferedImage.png");
81
}
82
}
83
84
BufferedImage snapshot = null;
85
try {
86
final GraphicsConfiguration gc =
87
GraphicsEnvironment.getLocalGraphicsEnvironment()
88
.getDefaultScreenDevice()
89
.getDefaultConfiguration();
90
if (gc.getColorModel() instanceof IndexColorModel) {
91
System.err.println("Skipping VolatileImage because of IndexColorModel");
92
return;
93
}
94
95
VolatileImage vi = gc.createCompatibleVolatileImage(WIDTH, HEIGHT);
96
int attempt = 0;
97
do {
98
vi.validate(gc);
99
draw(vi);
100
snapshot = vi.getSnapshot();
101
} while (vi.contentsLost() && ++attempt <= 10);
102
if (attempt > 10) {
103
throw new RuntimeException("Too many attempts: " + attempt);
104
}
105
validate(snapshot);
106
} finally {
107
if (saveImage && snapshot != null) {
108
save(snapshot, "volatileImage.png");
109
}
110
}
111
}
112
113
private static void draw(final Image img) {
114
Graphics g = img.getGraphics();
115
g.setColor(BACKGROUND);
116
g.fillRect(0, 0, WIDTH, HEIGHT);
117
g.setColor(COLOR1);
118
g.drawRect(OFFSET, OFFSET, WIDTH - OFFSET * 2, HEIGHT - OFFSET * 2);
119
g.setColor(COLOR2);
120
g.clipRect(OFFSET, OFFSET, WIDTH - OFFSET * 2 + 1, HEIGHT - OFFSET * 2 + 1);
121
((Graphics2D) g).setStroke(dash);
122
g.drawRect(OFFSET, OFFSET, WIDTH - OFFSET * 2, HEIGHT - OFFSET * 2);
123
g.dispose();
124
}
125
126
private static void validate(final BufferedImage img) {
127
checkHorizontalLine(img, OFFSET);
128
checkHorizontalLine(img, HEIGHT - OFFSET);
129
checkVerticalLine(img, OFFSET);
130
checkVerticalLine(img, WIDTH - OFFSET);
131
checkCorners(img);
132
}
133
134
private static void checkHorizontalLine(final BufferedImage img,
135
final int y) {
136
int prev = img.getRGB(OFFSET, y);
137
int curr;
138
int count = 1;
139
checkColor(OFFSET, y, prev, COLOR1, COLOR2);
140
for (int x = OFFSET + 1; x <= WIDTH - OFFSET; x++) {
141
curr = img.getRGB(x, y);
142
if (curr != prev) {
143
checkColor(x, y, curr, COLOR1, COLOR2);
144
checkCount(x, y, count);
145
prev = curr;
146
count = 1;
147
} else {
148
count++;
149
}
150
if (x < WIDTH - OFFSET) {
151
checkColor(x, y - 1, img.getRGB(x, y - 1), BACKGROUND);
152
checkColor(x, y + 1, img.getRGB(x, y + 1), BACKGROUND);
153
}
154
}
155
checkCount(WIDTH - OFFSET, y, count);
156
}
157
158
private static void checkVerticalLine(final BufferedImage img,
159
final int x) {
160
int prev = img.getRGB(x, OFFSET);
161
checkColor(x, OFFSET, prev, COLOR1, COLOR2);
162
int count = 1;
163
for (int y = OFFSET + 1; y <= HEIGHT - OFFSET; y++) {
164
int curr = img.getRGB(x, y);
165
if (curr != prev) {
166
checkColor(x, y, curr, COLOR1, COLOR2);
167
checkCount(x, y, count);
168
prev = curr;
169
count = 1;
170
} else {
171
count++;
172
}
173
if (y < HEIGHT - OFFSET) {
174
checkColor(x - 1, y, img.getRGB(x - 1, y), BACKGROUND);
175
checkColor(x + 1, y, img.getRGB(x + 1, y), BACKGROUND);
176
}
177
}
178
checkCount(x, HEIGHT - OFFSET, count);
179
}
180
181
private static void checkCorners(final BufferedImage img) {
182
int[][] corners = {
183
{OFFSET - 1, OFFSET - 1},
184
{OFFSET, OFFSET - 1},
185
{OFFSET - 1, OFFSET + 1},
186
187
{OFFSET - 1, HEIGHT - OFFSET},
188
{OFFSET - 1, HEIGHT - OFFSET + 1},
189
{OFFSET, HEIGHT - OFFSET + 1},
190
191
{WIDTH - OFFSET, OFFSET - 1},
192
{WIDTH - OFFSET + 1, OFFSET - 1},
193
{WIDTH - OFFSET + 1, OFFSET},
194
195
{WIDTH - OFFSET + 1, HEIGHT - OFFSET},
196
{WIDTH - OFFSET + 1, HEIGHT - OFFSET + 1},
197
{WIDTH - OFFSET, HEIGHT - OFFSET + 1},
198
};
199
200
for (int[] corner : corners) {
201
int color = img.getRGB(corner[0], corner[1]);
202
checkColor(corner[0], corner[1], color, BACKGROUND);
203
}
204
}
205
206
private static void checkColor(final int x, final int y,
207
final int color,
208
final Color... validColors) {
209
checkColor(x, y, color, Arrays.stream(validColors)
210
.mapToInt(Color::getRGB)
211
.toArray());
212
}
213
214
private static void checkColor(final int x, final int y,
215
final int color,
216
final int... validColors) {
217
for (int valid : validColors) {
218
if (color == valid) {
219
return;
220
}
221
}
222
throw new RuntimeException("Unexpected color at " + x + ", " + y
223
+ ": " + Integer.toHexString(color) + "; expected: "
224
+ Arrays.stream(validColors)
225
.mapToObj(Integer::toHexString)
226
.collect(Collectors.joining(", ")));
227
}
228
229
private static void checkCount(final int x, final int y, final int count) {
230
if (count > MAX_DASH_LENGTH) {
231
throw new RuntimeException("Dash is longer than " + MAX_DASH_LENGTH
232
+ " at " + x + ", " + y);
233
}
234
}
235
236
private static void save(final BufferedImage img,
237
final String fileName) throws IOException {
238
ImageIO.write(img, "png", new File(fileName));
239
}
240
241
}
242
243