|
| 1 | +/* |
| 2 | + * Copyright (c) 2003, 2022, 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 4975116 7040022 8023577 8025447 8286624 |
| 27 | + * @key headful |
| 28 | + * @summary verify the rounding of negative coordinates in Shape objects |
| 29 | + * @run main/othervm CoordinateTruncationBug |
| 30 | + */ |
| 31 | + |
| 32 | +import java.awt.Frame; |
| 33 | +import java.awt.Canvas; |
| 34 | +import java.awt.Graphics; |
| 35 | +import java.awt.Graphics2D; |
| 36 | +import java.awt.Robot; |
| 37 | +import java.awt.Rectangle; |
| 38 | +import java.awt.Point; |
| 39 | +import java.awt.Dimension; |
| 40 | +import java.awt.Color; |
| 41 | +import java.awt.AWTException; |
| 42 | +import java.awt.geom.Line2D; |
| 43 | +import java.awt.image.BufferedImage; |
| 44 | +import java.awt.image.VolatileImage; |
| 45 | + |
| 46 | +public class CoordinateTruncationBug { |
| 47 | + static boolean failure; |
| 48 | + static boolean verbose; |
| 49 | + |
| 50 | + static final int W = 80; |
| 51 | + static final int H = 80; |
| 52 | + |
| 53 | + static final Line2D vertline = new Line2D.Float(-0.7f, 0f, -0.7f, H); |
| 54 | + static final Line2D horizline = new Line2D.Float(0f, -0.7f, W, -0.7f); |
| 55 | + |
| 56 | + public static void main(String argv[]) { |
| 57 | + verbose = (argv.length > 0); |
| 58 | + new Screen().test(); |
| 59 | + new BufImg().test(); |
| 60 | + new VolImg().test(); |
| 61 | + if (failure) { |
| 62 | + throw new RuntimeException("Test failed due to 1 or more failures"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public static abstract class Test { |
| 67 | + public abstract String getName(); |
| 68 | + public abstract void makeDest(); |
| 69 | + public abstract void runTest(); |
| 70 | + public abstract void dispose(); |
| 71 | + public abstract BufferedImage getSnapshot(); |
| 72 | + |
| 73 | + public void test() { |
| 74 | + makeDest(); |
| 75 | + runTest(); |
| 76 | + dispose(); |
| 77 | + } |
| 78 | + |
| 79 | + public void runTest(Graphics2D g2d) { |
| 80 | + g2d.setColor(Color.white); |
| 81 | + g2d.fillRect(0, 0, W, H); |
| 82 | + |
| 83 | + if (!checkAllWhite()) { |
| 84 | + System.err.println("Aborting test of "+getName()+ |
| 85 | + " due to readback failure!"); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + g2d.setColor(Color.red); |
| 90 | + g2d.draw(vertline); |
| 91 | + g2d.draw(horizline); |
| 92 | + if (!checkAllWhite()) { |
| 93 | + System.err.println(getName()+" failed!"); |
| 94 | + failure = true; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public boolean checkAllWhite() { |
| 99 | + BufferedImage bimg = getSnapshot(); |
| 100 | + if (bimg == null) { |
| 101 | + System.err.println(getName()+" returned null snapshot!"); |
| 102 | + return false; |
| 103 | + } |
| 104 | + boolean ret = true; |
| 105 | + for (int y = 0; y < H; y++) { |
| 106 | + for (int x = 0; x < W; x++) { |
| 107 | + int rgb = bimg.getRGB(x, y); |
| 108 | + if (rgb != -1) { |
| 109 | + System.err.println(getName()+"("+x+", "+y+") == "+ |
| 110 | + Integer.toHexString(rgb)); |
| 111 | + if (verbose) { |
| 112 | + ret = false; |
| 113 | + } else { |
| 114 | + return false; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + return ret; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public static class Screen extends Test { |
| 124 | + Frame frame; |
| 125 | + TestCanvas canvas; |
| 126 | + |
| 127 | + public String getName() { |
| 128 | + return "Screen"; |
| 129 | + } |
| 130 | + |
| 131 | + public void makeDest() { |
| 132 | + frame = new Frame("Screen test"); |
| 133 | + frame.setUndecorated(true); |
| 134 | + canvas = new TestCanvas(this); |
| 135 | + frame.add(canvas); |
| 136 | + frame.pack(); |
| 137 | + frame.setLocationRelativeTo(null); |
| 138 | + } |
| 139 | + |
| 140 | + public void runTest() { |
| 141 | + frame.show(); |
| 142 | + canvas.waitForTest(); |
| 143 | + } |
| 144 | + |
| 145 | + public Graphics2D createGraphics() { |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + public BufferedImage getSnapshot() { |
| 150 | + // bypass window animation |
| 151 | + try { |
| 152 | + Thread.sleep(500); |
| 153 | + } catch (InterruptedException e) { |
| 154 | + }; |
| 155 | + |
| 156 | + try { |
| 157 | + Robot r = new Robot(); |
| 158 | + Point p = canvas.getLocationOnScreen(); |
| 159 | + return r.createScreenCapture(new Rectangle(p.x, p.y, W, H)); |
| 160 | + } catch (AWTException e) { |
| 161 | + return null; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + public void dispose() { |
| 166 | + frame.hide(); |
| 167 | + frame.dispose(); |
| 168 | + } |
| 169 | + |
| 170 | + public static class TestCanvas extends Canvas { |
| 171 | + Test test; |
| 172 | + boolean done; |
| 173 | + |
| 174 | + public TestCanvas(Test test) { |
| 175 | + this.test = test; |
| 176 | + } |
| 177 | + |
| 178 | + public Dimension getPreferredSize() { |
| 179 | + return new Dimension(W, H); |
| 180 | + } |
| 181 | + |
| 182 | + public synchronized void waitForTest() { |
| 183 | + while (!done) { |
| 184 | + try { |
| 185 | + wait(); |
| 186 | + } catch (InterruptedException e) { |
| 187 | + System.err.println(getName()+" interrupted!"); |
| 188 | + failure = true; |
| 189 | + break; |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + public void paint(Graphics g) { |
| 195 | + if (!done) { |
| 196 | + test.runTest((Graphics2D) g); |
| 197 | + notifyDone(); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + public synchronized void notifyDone() { |
| 202 | + done = true; |
| 203 | + notifyAll(); |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + public abstract static class Standalone extends Test { |
| 209 | + public abstract Graphics2D createGraphics(); |
| 210 | + |
| 211 | + public void runTest() { |
| 212 | + Graphics2D g2d = createGraphics(); |
| 213 | + runTest(g2d); |
| 214 | + g2d.dispose(); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + public static class BufImg extends Standalone { |
| 219 | + public BufferedImage bimg; |
| 220 | + |
| 221 | + public String getName() { |
| 222 | + return "BufferedImage"; |
| 223 | + } |
| 224 | + |
| 225 | + public void makeDest() { |
| 226 | + bimg = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB); |
| 227 | + } |
| 228 | + |
| 229 | + public Graphics2D createGraphics() { |
| 230 | + return bimg.createGraphics(); |
| 231 | + } |
| 232 | + |
| 233 | + public BufferedImage getSnapshot() { |
| 234 | + return bimg; |
| 235 | + } |
| 236 | + |
| 237 | + public void dispose() { |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + public static class VolImg extends Standalone { |
| 242 | + Frame frame; |
| 243 | + VolatileImage vimg; |
| 244 | + |
| 245 | + public String getName() { |
| 246 | + return "VolatileImage"; |
| 247 | + } |
| 248 | + |
| 249 | + public void makeDest() { |
| 250 | + frame = new Frame(); |
| 251 | + frame.setSize(W, H); |
| 252 | + frame.setLocationRelativeTo(null); |
| 253 | + frame.show(); |
| 254 | + vimg = frame.createVolatileImage(W, H); |
| 255 | + } |
| 256 | + |
| 257 | + public Graphics2D createGraphics() { |
| 258 | + return vimg.createGraphics(); |
| 259 | + } |
| 260 | + |
| 261 | + public BufferedImage getSnapshot() { |
| 262 | + return vimg.getSnapshot(); |
| 263 | + } |
| 264 | + |
| 265 | + public void dispose() { |
| 266 | + vimg.flush(); |
| 267 | + frame.hide(); |
| 268 | + frame.dispose(); |
| 269 | + } |
| 270 | + } |
| 271 | +} |
0 commit comments