Path: blob/master/test/jdk/java/awt/FileDialog/ImageOperations.java
41149 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.AlphaComposite;24import java.awt.Color;25import java.awt.Dialog;26import java.awt.FileDialog;27import java.awt.Graphics2D;28import java.awt.Image;29import java.awt.image.BufferedImage;30import java.awt.image.ImageObserver;31import java.io.File;32import java.io.IOException;3334import javax.imageio.ImageIO;3536import static java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE;3738/**39* @test40* @key headful41* @bug 823827642* @summary Verifies that create/prepare/checkImage work properly for FileDialog43*/44public final class ImageOperations {4546public static void main(String[] args) throws Exception {47BufferedImage gold = new BufferedImage(255, 255, TYPE_INT_ARGB_PRE);48BufferedImage target = new BufferedImage(255, 255, TYPE_INT_ARGB_PRE);49fill(gold);5051FileDialog fd = new FileDialog((Dialog) null);52fd.pack();53try {54Image image = fd.createImage(gold.getSource());55if (image == null) {56throw new NullPointerException();57}58if (!fd.prepareImage(image, null)) {59Thread.sleep(100);60}61if ((fd.checkImage(image, null) & ImageObserver.ALLBITS) == 0) {62throw new RuntimeException("Image should be loaded already");63}6465Graphics2D graphics = (Graphics2D) target.getGraphics();66graphics.setComposite(AlphaComposite.Src);67graphics.drawImage(image, 0, 0, null);68graphics.dispose();6970validate(gold, target);71} finally {72fd.dispose();73}74}7576/**77* Fills the whole image using different alpha for each row.78*79* @param image to fill80*/81private static void fill(Image image) {82Graphics2D graphics = (Graphics2D) image.getGraphics();83graphics.setComposite(AlphaComposite.Src);84graphics.setColor(Color.GREEN);85graphics.fillRect(0, 0, image.getWidth(null), image.getHeight(null));86for (int i = 0; i < 255; ++i) {87graphics.setColor(new Color(23, 127, 200, i));88graphics.fillRect(0, i, image.getWidth(null), 1);89}90graphics.dispose();91}9293private static void validate(BufferedImage bi, BufferedImage goldbi)94throws IOException {95for (int x = 0; x < bi.getWidth(); ++x) {96for (int y = 0; y < bi.getHeight(); ++y) {97if (goldbi.getRGB(x, y) != bi.getRGB(x, y)) {98System.out.println("x = " + x);99System.out.println("y = " + y);100ImageIO.write(bi, "png", new File("actual.png"));101ImageIO.write(goldbi, "png", new File("expected.png"));102throw new RuntimeException("Test failed.");103}104}105}106}107}108109110