1 /* 2 * Copyright (c) 2020, 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.AlphaComposite; 25 import java.awt.Color; 26 import java.awt.Dialog; 27 import java.awt.FileDialog; 28 import java.awt.Graphics2D; 29 import java.awt.Image; 30 import java.awt.image.BufferedImage; 31 import java.awt.image.ImageObserver; 32 import java.io.File; 33 import java.io.IOException; 34 35 import javax.imageio.ImageIO; 36 37 import static java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE; 38 39 /** 40 * @test 41 * @key headful 42 * @bug 8238276 43 * @summary Verifies that create/prepare/checkImage work properly for FileDialog 44 */ 45 public final class ImageOperations { 46 47 public static void main(String[] args) throws Exception { 48 BufferedImage gold = new BufferedImage(255, 255, TYPE_INT_ARGB_PRE); 49 BufferedImage target = new BufferedImage(255, 255, TYPE_INT_ARGB_PRE); 50 fill(gold); 51 52 FileDialog fd = new FileDialog((Dialog) null); 53 fd.pack(); 54 try { 55 Image image = fd.createImage(gold.getSource()); 56 if (image == null) { 57 throw new NullPointerException(); 58 } 59 if (!fd.prepareImage(image, null)) { 60 Thread.sleep(100); 61 } 62 if ((fd.checkImage(image, null) & ImageObserver.ALLBITS) == 0) { 63 throw new RuntimeException("Image should be loaded already"); 64 } 65 66 Graphics2D graphics = (Graphics2D) target.getGraphics(); 67 graphics.setComposite(AlphaComposite.Src); 68 graphics.drawImage(image, 0, 0, null); 69 graphics.dispose(); 70 71 validate(gold, target); 72 } finally { 73 fd.dispose(); 74 } 75 } 76 77 /** 78 * Fills the whole image using different alpha for each row. 79 * 80 * @param image to fill 81 */ 82 private static void fill(Image image) { 83 Graphics2D graphics = (Graphics2D) image.getGraphics(); 84 graphics.setComposite(AlphaComposite.Src); 85 graphics.setColor(Color.GREEN); 86 graphics.fillRect(0, 0, image.getWidth(null), image.getHeight(null)); 87 for (int i = 0; i < 255; ++i) { 88 graphics.setColor(new Color(23, 127, 200, i)); 89 graphics.fillRect(0, i, image.getWidth(null), 1); 90 } 91 graphics.dispose(); 92 } 93 94 private static void validate(BufferedImage bi, BufferedImage goldbi) 95 throws IOException { 96 for (int x = 0; x < bi.getWidth(); ++x) { 97 for (int y = 0; y < bi.getHeight(); ++y) { 98 if (goldbi.getRGB(x, y) != bi.getRGB(x, y)) { 99 System.out.println("x = " + x); 100 System.out.println("y = " + y); 101 ImageIO.write(bi, "png", new File("actual.png")); 102 ImageIO.write(goldbi, "png", new File("expected.png")); 103 throw new RuntimeException("Test failed."); 104 } 105 } 106 } 107 } 108 }