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