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