Path: blob/master/test/jdk/java/awt/Graphics2D/FlipDrawImage/FlipDrawImage.java
41153 views
/*1* Copyright (c) 2013, 2016, 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*/222324import java.awt.AlphaComposite;25import java.awt.Color;26import java.awt.Graphics2D;27import java.awt.GraphicsConfiguration;28import java.awt.GraphicsEnvironment;29import java.awt.image.BufferedImage;30import java.awt.image.VolatileImage;3132/**33* @test34* @key headful35* @bug 800062936* @author Sergey Bylokhov37*/38public final class FlipDrawImage {3940private static final int width = 400;41private static final int height = 400;4243public static void main(final String[] args) {44GraphicsEnvironment ge =45GraphicsEnvironment.getLocalGraphicsEnvironment();46GraphicsConfiguration gc =47ge.getDefaultScreenDevice().getDefaultConfiguration();48VolatileImage vi = gc.createCompatibleVolatileImage(width, height);49final BufferedImage bi = new BufferedImage(width, height,50BufferedImage.TYPE_INT_ARGB);51while (true) {52vi.validate(gc);53Graphics2D g2d = vi.createGraphics();54g2d.setColor(Color.red);55g2d.fillRect(0, 0, width, height);56g2d.setColor(Color.green);57g2d.fillRect(0, 0, width / 2, height / 2);58g2d.dispose();5960if (vi.validate(gc) != VolatileImage.IMAGE_OK) {61try {62Thread.sleep(100);63} catch (InterruptedException ignored) {64}65continue;66}6768Graphics2D g = bi.createGraphics();69g.setComposite(AlphaComposite.Src);70g.setColor(Color.BLUE);71g.fillRect(0, 0, width, height);72// destination width and height are flipped and scale is used.73g.drawImage(vi, width / 2, height / 2, -width / 2, -height / 2,74null, null);75g.dispose();7677if (vi.contentsLost()) {78try {79Thread.sleep(100);80} catch (InterruptedException ignored) {81}82continue;83}8485for (int x = 0; x < width; ++x) {86for (int y = 0; y < height; ++y) {87if (x < width / 2 && y < height / 2) {88if (x >= width / 4 && y >= height / 4) {89if (bi.getRGB(x, y) != Color.green.getRGB()) {90throw new RuntimeException("Test failed.");91}92} else if (bi.getRGB(x, y) != Color.red.getRGB()) {93throw new RuntimeException("Test failed.");94}95} else {96if (bi.getRGB(x, y) != Color.BLUE.getRGB()) {97throw new RuntimeException("Test failed.");98}99}100}101}102break;103}104}105}106107108