|
| 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 | +} |
0 commit comments