IncorrectSourceOffset.java revision 14851:980da45565c8
1/*
2 * Copyright (c) 2014, 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
24import java.awt.AlphaComposite;
25import java.awt.Color;
26import java.awt.Graphics2D;
27import java.awt.GraphicsConfiguration;
28import java.awt.GraphicsEnvironment;
29import java.awt.Image;
30import java.awt.image.BufferedImage;
31import java.awt.image.VolatileImage;
32import java.io.File;
33import java.io.IOException;
34
35import javax.imageio.ImageIO;
36
37/**
38 * @test
39 * @key headful
40 * @bug 8041129
41 * @summary Tests asymmetric source offsets.
42 * @author Sergey Bylokhov
43 */
44public final class IncorrectSourceOffset {
45
46    public static void main(final String[] args) throws IOException {
47        GraphicsEnvironment ge = GraphicsEnvironment
48                .getLocalGraphicsEnvironment();
49        GraphicsConfiguration gc = ge.getDefaultScreenDevice()
50                                     .getDefaultConfiguration();
51        VolatileImage vi = gc.createCompatibleVolatileImage(511, 255);
52        BufferedImage bi = new BufferedImage(511, 255,
53                                             BufferedImage.TYPE_INT_ARGB);
54        BufferedImage gold = new BufferedImage(511, 255,
55                                               BufferedImage.TYPE_INT_ARGB);
56        fill(gold);
57        while (true) {
58            vi.validate(gc);
59            fill(vi);
60            if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
61                try {
62                    Thread.sleep(100);
63                } catch (final InterruptedException ignored) {
64                }
65                continue;
66            }
67
68            Graphics2D big = bi.createGraphics();
69            big.drawImage(vi, 7, 11, 127, 111, 7, 11, 127, 111, null);
70            big.dispose();
71            if (vi.contentsLost()) {
72                try {
73                    Thread.sleep(100);
74                } catch (final InterruptedException ignored) {
75                }
76                continue;
77            }
78            break;
79        }
80
81        for (int x = 7; x < 127; ++x) {
82            for (int y = 11; y < 111; ++y) {
83                if (gold.getRGB(x, y) != bi.getRGB(x, y)) {
84                    ImageIO.write(gold, "png", new File("gold.png"));
85                    ImageIO.write(bi, "png", new File("bi.png"));
86                    throw new RuntimeException("Test failed.");
87                }
88            }
89        }
90    }
91
92    private static void fill(Image image) {
93        Graphics2D graphics = (Graphics2D) image.getGraphics();
94        graphics.setComposite(AlphaComposite.Src);
95        for (int i = 0; i < image.getHeight(null); ++i) {
96            graphics.setColor(new Color(i, 0, 0));
97            graphics.fillRect(0, i, image.getWidth(null), 1);
98        }
99        graphics.dispose();
100    }
101}
102