Skip to content

Commit 90c2c0b

Browse files
committedSep 24, 2024
8340143: Open source several Java2D rendering loop tests.
Reviewed-by: psadhukhan
1 parent 212e329 commit 90c2c0b

File tree

5 files changed

+394
-0
lines changed

5 files changed

+394
-0
lines changed
 
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 1999, 2024, 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+
* @test
26+
* @bug 4238978
27+
* @summary This test verifies that the correct blitting loop is being used.
28+
* The correct output should have a yellow border on the top and
29+
* left sides of a red box. The incorrect output would have only
30+
* a red box -- no yellow border."
31+
*/
32+
33+
import java.awt.Color;
34+
import java.awt.Graphics2D;
35+
import java.awt.image.BufferedImage;
36+
37+
public class ARGBBgToRGB {
38+
39+
public static void main(String[] argv) {
40+
BufferedImage bi = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
41+
Graphics2D big = bi.createGraphics();
42+
big.setColor(Color.red);
43+
big.fillRect(30, 30, 150, 150);
44+
45+
BufferedImage bi2 = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
46+
Graphics2D big2 = bi2.createGraphics();
47+
big2.drawImage(bi, 0, 0, Color.yellow, null);
48+
49+
int expectYellowPix = bi2.getRGB(0, 0);
50+
int expectRedPix = bi2.getRGB(50, 50);
51+
if ((expectYellowPix != Color.yellow.getRGB()) ||
52+
(expectRedPix != Color.red.getRGB()))
53+
{
54+
throw new RuntimeException("Unexpected colors " + expectYellowPix + " " + expectRedPix);
55+
}
56+
}
57+
}
+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright (c) 1999, 2024, 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+
* @test
26+
* @bug 4188744
27+
* @summary This test verifies that copyArea performs correctly for negative offset values.
28+
* The correct output shows that the text area is moved to the left and down,
29+
* leaving some garbage on the right and the top.
30+
* The incorrect copy would show the text area garbled and no text is legible.
31+
* @library /java/awt/regtesthelpers
32+
* @build PassFailJFrame
33+
* @run main/manual CopyNegative
34+
*/
35+
36+
import java.awt.Color;
37+
import java.awt.Dimension;
38+
import java.awt.Frame;
39+
import java.awt.Graphics;
40+
import java.awt.Image;
41+
import java.awt.Panel;
42+
43+
public class CopyNegative extends Panel {
44+
45+
private static final String INSTRUCTIONS = """
46+
This test verifies that copyArea performs correctly for negative offset values.
47+
The test draws text in an image, then copies the contents repeatedly.
48+
The correct output shows that the text is moved to the left and down,
49+
leaving some garbage on the top / right and some legible text at the bottom left.
50+
The incorrect copy would show the whole text area garbled and no text is legible.
51+
""";
52+
53+
public static void main(String[] argv) throws Exception {
54+
PassFailJFrame.builder()
55+
.title("CopyNegativeTest")
56+
.instructions(INSTRUCTIONS)
57+
.testUI(CopyNegative::createUI)
58+
.testTimeOut(5)
59+
.rows(10)
60+
.columns(50)
61+
.build()
62+
.awaitAndCheck();
63+
}
64+
65+
Image img;
66+
67+
static final int W = 200, H = 200;
68+
69+
static Frame createUI() {
70+
Frame f = new Frame("CopyNegative");
71+
f.add(new CopyNegative());
72+
f.pack();
73+
return f;
74+
}
75+
76+
public Dimension getPreferredSize() {
77+
return new Dimension(W, H);
78+
}
79+
80+
private void doCopy() {
81+
Graphics g = img.getGraphics();
82+
g.setColor(Color.white);
83+
g.fillRect(0, 0, W, H);
84+
g.setColor(Color.black);
85+
String text = "Some Text To Display, it is long enough to fill the entire display line.";
86+
StringBuffer sb = new StringBuffer(text);
87+
88+
for (int i = 1; i < 50; i++) {
89+
g.drawString(sb.toString(), 5,20 * i - 10);
90+
sb.insert(0, Integer.toString(i));
91+
}
92+
for (int i = 0 ; i < 20 ; i++ ) {
93+
g.copyArea(0, 0, W, H, -3, 3);
94+
}
95+
}
96+
97+
public void paint(Graphics g) {
98+
img = createImage(W, H);
99+
doCopy();
100+
g.drawImage(img, 0, 0, this);
101+
}
102+
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 1999, 2024, 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+
* @test
26+
* @bug 4181172
27+
* @summary Confirm that solid white fill is not dithered on an 8-bit indexed surface.
28+
* The test draws two areas filled with white solid color.
29+
* The upper left square is filled in aliasing mode and
30+
* the lower right square is filled in anti-aliasing mode.
31+
*/
32+
33+
import java.awt.Color;
34+
import java.awt.Graphics2D;
35+
import java.awt.RenderingHints;
36+
import java.awt.image.BufferedImage;
37+
38+
public class DitheredSolidFill {
39+
40+
public static void main(String args[]) {
41+
BufferedImage bi = new BufferedImage(120, 120, BufferedImage.TYPE_BYTE_INDEXED);
42+
Graphics2D g2D = bi.createGraphics();
43+
44+
g2D.setColor(Color.black);
45+
g2D.fillRect(0, 0, 100, 100);
46+
47+
g2D.setColor(Color.white);
48+
g2D.fillRect(5, 5, 40, 40);
49+
checkPixels(bi, 5, 5, 40, 40);
50+
51+
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
52+
g2D.fillRect(55, 55, 40, 40);
53+
checkPixels(bi, 55, 55, 40, 40);
54+
}
55+
56+
static void checkPixels(BufferedImage bi, int x, int y, int w, int h) {
57+
// pixel can be off white, but must be the same in all cases.
58+
int expectedPix = bi.getRGB(x, y);
59+
for (int x0 = x; x0 < x + w; x0++) {
60+
for (int y0 = y; y0 < y + h; y0++) {
61+
if (bi.getRGB(x0, y0) != expectedPix) {
62+
try {
63+
javax.imageio.ImageIO.write(bi, "png", new java.io.File("failed.png"));
64+
} catch (Exception e) {
65+
}
66+
throw new RuntimeException("Not expected pix : " +
67+
Integer.toHexString(bi.getRGB(x0, y0)) +
68+
" at " + x0 + "," + y0);
69+
}
70+
}
71+
}
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 1999, 2024, 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+
@test
26+
@bug 4236576
27+
@summary tests that a BufferedImage in TYPE_3BYTE_BGR format is correctly
28+
drawn when there is an offset between the Graphics clip bounds
29+
and the clip box of the underlying device context.
30+
@run main OffsetCalculationTest
31+
*/
32+
33+
import java.awt.Color;
34+
import java.awt.Graphics2D;
35+
import java.awt.Rectangle;
36+
import java.awt.image.BufferedImage;
37+
import java.awt.image.DataBuffer;
38+
39+
public class OffsetCalculationTest {
40+
41+
public static void main(String[] args) {
42+
BufferedImage srcImage = new BufferedImage(500, 500, BufferedImage.TYPE_3BYTE_BGR);
43+
44+
DataBuffer buffer = srcImage.getRaster().getDataBuffer();
45+
for (int i = 2; i < buffer.getSize(); i+=3) {
46+
// setting each pixel to blue via the data buffer elements.
47+
buffer.setElem(i - 2, 0xff);
48+
buffer.setElem(i - 1, 0);
49+
buffer.setElem(i, 0);
50+
}
51+
52+
int w = 200, h = 200;
53+
BufferedImage destImage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
54+
Graphics2D g = destImage.createGraphics();
55+
Rectangle r = new Rectangle(0, 0, w, h);
56+
g.setClip(r.x - 1, r.y, r.width + 1, r.height);
57+
g.drawImage(srcImage, 0, 0, null);
58+
59+
int bluepix = Color.blue.getRGB();
60+
for (int y = 0; y < w; y++) {
61+
for (int x = 0; x < h; x++) {
62+
if (destImage.getRGB(x, y) != bluepix) {
63+
throw new RuntimeException("Not Blue");
64+
}
65+
}
66+
}
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 1999, 2024, 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+
* @test
26+
* @bug 4088173
27+
* @summary This interactive test verifies that the XOR mode is not affecting
28+
* the clearRect() call. The correct output looks like:
29+
*
30+
* \ /
31+
* \ /
32+
* The backgound is blue.
33+
* The lines outside the central rectangle are green.
34+
* The central rectangle is also blue (the result of clearRect())
35+
* / \
36+
* / \
37+
*
38+
* @key headful
39+
* @run main XORClearRect
40+
*/
41+
42+
import java.awt.Color;
43+
import java.awt.EventQueue;
44+
import java.awt.Graphics;
45+
import java.awt.Frame;
46+
import java.awt.Panel;
47+
import java.awt.Point;
48+
import java.awt.Robot;
49+
50+
public class XORClearRect extends Panel {
51+
52+
public static void main(String args[]) throws Exception {
53+
EventQueue.invokeAndWait(XORClearRect::createUI);
54+
try {
55+
Robot robot = new Robot();
56+
robot.waitForIdle();
57+
robot.delay(2000);
58+
Point p = frame.getLocationOnScreen();
59+
int pix = robot.getPixelColor(p.x + 100, p.y + 100).getRGB();
60+
if (pix != Color.blue.getRGB()) {
61+
throw new RuntimeException("Not blue");
62+
}
63+
} finally {
64+
if (frame != null) {
65+
EventQueue.invokeAndWait(frame::dispose);
66+
}
67+
}
68+
}
69+
70+
static volatile Frame frame;
71+
72+
static void createUI() {
73+
frame = new Frame("XORClearRect");
74+
frame.setBackground(Color.blue);
75+
XORClearRect xor = new XORClearRect();
76+
frame.add(xor);
77+
frame.setSize(200,200);
78+
frame.setVisible(true);
79+
}
80+
81+
public XORClearRect() {
82+
setBackground(Color.blue);
83+
}
84+
85+
public void paint(Graphics g) {
86+
g.setColor(Color.green);
87+
g.drawLine(0,0,200,200);
88+
g.drawLine(0,200,200,0);
89+
g.setXORMode(Color.blue);
90+
g.clearRect(50,50,100,100); //expecting the rectangle to be filled
91+
// with the background color (blue)
92+
}
93+
}

0 commit comments

Comments
 (0)
Please sign in to comment.