Skip to content

Commit 274e09d

Browse files
author
duke
committed
Backport e89fd1d2ceff82952a4859c0febe902412fcf064
1 parent 5d7aa66 commit 274e09d

File tree

7 files changed

+563
-0
lines changed

7 files changed

+563
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2000, 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 4363534
27+
* @summary This test verifies that setting a non-thin-line BasicStroke
28+
* on a Graphics2D obtained from a BufferedImage will correctly validate
29+
* the pipelines for the line-widening pipeline even if that is the only
30+
* non-default attribute on the graphics.
31+
*
32+
*/
33+
34+
import java.awt.BasicStroke;
35+
import java.awt.Color;
36+
import java.awt.Graphics2D;
37+
import java.awt.image.BufferedImage;
38+
39+
public class BasicStrokeValidate {
40+
41+
public static final int TESTW = 100;
42+
public static final int TESTH = 100;
43+
44+
public static void main(String[] args) {
45+
BufferedImage bi1 = createImage(false);
46+
BufferedImage bi2 = createImage(true);
47+
compare(bi1, bi2); // images should differ
48+
}
49+
50+
static BufferedImage createImage(boolean dashed) {
51+
BufferedImage bi = new BufferedImage(TESTW, TESTH, BufferedImage.TYPE_INT_RGB);
52+
Graphics2D g2d = bi.createGraphics();
53+
g2d.setColor(Color.white);
54+
g2d.fillRect(0, 0, TESTW, TESTH);
55+
g2d.setColor(Color.black);
56+
if (dashed) {
57+
g2d.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_SQUARE,
58+
BasicStroke.JOIN_MITER, 10.0f,
59+
new float[] {2.5f, 3.5f},
60+
0.0f));
61+
}
62+
g2d.drawRect(10, 10, TESTW-20, TESTH-20);
63+
g2d.setStroke(new BasicStroke(10f));
64+
g2d.drawRect(20, 20, TESTW-40, TESTH-40);
65+
return bi;
66+
}
67+
68+
static void compare(BufferedImage i1, BufferedImage i2) {
69+
boolean same = true;
70+
int w = i1.getWidth(), h = i1.getHeight();
71+
for (int y = 0; y < h; y++) {
72+
for (int x = 0; x < w; x++) {
73+
int p1 = i1.getRGB(x, y);
74+
int p2 = i2.getRGB(x, y);
75+
if (p1 != p2) {
76+
same = false;
77+
}
78+
}
79+
if (!same) {
80+
break;
81+
}
82+
}
83+
if (same) {
84+
throw new RuntimeException("No difference");
85+
}
86+
}
87+
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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 4191004
27+
* @summary Tests that no IllegalArgumentException is thrown when calling
28+
* drawImage with certain conditions
29+
* @key headful
30+
*/
31+
32+
import java.awt.AlphaComposite;
33+
import java.awt.BorderLayout;
34+
import java.awt.Color;
35+
import java.awt.Dimension;
36+
import java.awt.EventQueue;
37+
import java.awt.Frame;
38+
import java.awt.Graphics;
39+
import java.awt.Graphics2D;
40+
import java.awt.Image;
41+
import java.awt.MediaTracker;
42+
import java.awt.Toolkit;
43+
import java.awt.geom.GeneralPath;
44+
import java.awt.geom.Ellipse2D;
45+
import java.awt.geom.Rectangle2D;
46+
import java.awt.image.BufferedImage;
47+
import java.util.concurrent.CountDownLatch;
48+
import java.util.concurrent.TimeUnit;
49+
import javax.swing.JFrame;
50+
import javax.swing.JPanel;
51+
52+
public class DrawImageIAETest extends Frame {
53+
54+
static String filename = "/duke.gif";
55+
private volatile Image dimg;
56+
private volatile BufferedImage bimg;
57+
static volatile DrawImageIAETest app;
58+
static volatile JFrame jframe;
59+
static volatile boolean passed = true;
60+
static volatile Exception exception = null;
61+
static volatile CountDownLatch imageLatch = new CountDownLatch(1);
62+
63+
DrawImageIAETest(String title) {
64+
super(title);
65+
}
66+
67+
public static void main(final String[] args) throws Exception {
68+
EventQueue.invokeAndWait(DrawImageIAETest:: createUI);
69+
imageLatch.await(3, TimeUnit.MILLISECONDS);
70+
try {
71+
if (!passed) {
72+
throw new RuntimeException("Test FAILED: exception caught:" + exception);
73+
}
74+
} finally {
75+
if (jframe != null) {
76+
EventQueue.invokeAndWait(jframe::dispose);
77+
}
78+
if (app != null) {
79+
EventQueue.invokeAndWait(app::dispose);
80+
}
81+
}
82+
}
83+
84+
static void createUI() {
85+
app = new DrawImageIAETest("DrawImageIAETest");
86+
app.setLayout (new BorderLayout());
87+
app.setSize(200,200);
88+
app.setLocationRelativeTo(null);
89+
app.setVisible(true);
90+
91+
String file;
92+
try {
93+
String dir = System.getProperty("test.src",
94+
System.getProperty("user.dir"));
95+
file = dir + filename;
96+
} catch (Exception e) {
97+
file = "." + filename;
98+
}
99+
100+
Image textureAlphaSource = null;
101+
MediaTracker tracker = new MediaTracker(app);
102+
app.dimg = Toolkit.getDefaultToolkit().getImage(file);
103+
tracker.addImage(app.dimg, 1);
104+
try {
105+
tracker.waitForAll();
106+
imageLatch.countDown();
107+
} catch (Exception e) {
108+
System.err.println("Can't load images");
109+
}
110+
111+
if (app.dimg == null) {
112+
passed = false;
113+
return;
114+
}
115+
116+
jframe = new JFrame("Test DrawImageIAETest");
117+
jframe.setSize(300, 300);
118+
JPanel jpanel;
119+
jframe.getContentPane().add("Center", jpanel = new JPanel() {
120+
public void paint(Graphics _g) {
121+
Graphics2D g = (Graphics2D)_g;
122+
Dimension d = getSize();
123+
Graphics2D g2 = app.createGraphics2D(d.width, d.height);
124+
app.drawDemo(d.width, d.height, g2);
125+
g2.dispose();
126+
g.drawImage(app.bimg, 0, 0, app);
127+
}
128+
});
129+
jpanel.setSize(140, 140);
130+
jframe.setVisible(true);
131+
}
132+
133+
public void drawDemo(int w, int h, Graphics2D g2) {
134+
GeneralPath p1 = new GeneralPath();
135+
GeneralPath p2 = new GeneralPath();
136+
137+
int dukeX = 73;
138+
int dukeY = 26;
139+
140+
double x = 118;
141+
double y = 17;
142+
double ew = 50;
143+
double eh = 48;
144+
145+
p1.append(new Ellipse2D.Double(x, y, ew, eh), false);
146+
p2.append(new Rectangle2D.Double(x+5, y+5, ew-10, eh-10),false);
147+
148+
g2.setClip(p1);
149+
g2.clip(p2);
150+
try {
151+
g2.drawImage(dimg, dukeX, dukeY, null);
152+
} catch (IllegalArgumentException e) {
153+
passed = false;
154+
exception = e;
155+
}
156+
}
157+
158+
public Graphics2D createGraphics2D(int w, int h) {
159+
Graphics2D g2 = null;
160+
if (bimg == null || bimg.getWidth() != w || bimg.getHeight() != h) {
161+
bimg = (BufferedImage) createImage(w, h);
162+
}
163+
g2 = bimg.createGraphics();
164+
g2.setBackground(getBackground());
165+
g2.clearRect(0, 0, w, h);
166+
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
167+
return g2;
168+
}
169+
}
1.88 KB
Loading
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 4203598
27+
* @summary This test verifies that an image with transparent background can be displayed
28+
* correctly with the red background color given.
29+
* The correct display should be the sleeping Duke on a red background.
30+
*
31+
*/
32+
33+
import java.awt.Color;
34+
import java.awt.Graphics2D;
35+
import java.awt.image.BufferedImage;
36+
import java.io.File;
37+
import javax.imageio.ImageIO;
38+
39+
public class ImageRendering {
40+
41+
public static void main(String[] args) throws Exception {
42+
43+
String imgName = "snooze.gif";
44+
File file = new File(System.getProperty("test.src", "."), imgName);
45+
BufferedImage image = ImageIO.read(file);
46+
int w = image.getWidth();
47+
int h = image.getHeight();
48+
BufferedImage dest = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
49+
Graphics2D g2d = dest.createGraphics();
50+
g2d.drawImage(image, 0, 0, Color.red, null);
51+
int redPixel = Color.red.getRGB();
52+
for (int y = 0; y < h; y++) {
53+
for (int x = 0; x < w; x++) {
54+
int srcPixel = image.getRGB(x, y);
55+
if ((srcPixel & 0x0ff000000) == 0) {
56+
int destPix = dest.getRGB(x, y);
57+
if (destPix != redPixel) {
58+
throw new RuntimeException("Not red at x=" + x +
59+
" y=" + y +
60+
"pix = " + Integer.toHexString(destPix));
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}
2.79 KB
Loading

0 commit comments

Comments
 (0)