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 
  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  * @key headful
  36  * @bug 8000629
  37  * @summary Temporary backbuffer in the DrawImage should not fill background
  38  * outside of source image bounds.
  39  * @author Sergey Bylokhov
  40  */
  41 public final class IncorrectBounds {
  42 
  43     private static final int width = 400;
  44     private static final int height = 400;
  45 
  46     public static void main(final String[] args) {
  47         GraphicsEnvironment ge =
  48                 GraphicsEnvironment.getLocalGraphicsEnvironment();
  49         GraphicsConfiguration gc =
  50                 ge.getDefaultScreenDevice().getDefaultConfiguration();
  51         VolatileImage vi = gc.createCompatibleVolatileImage(width / 4,
  52                                                             height / 4);
  53         final BufferedImage bi = new BufferedImage(width, height,
  54                                                    BufferedImage.TYPE_INT_ARGB);
  55         while (true) {
  56             vi.validate(gc);
  57             Graphics2D g2d = vi.createGraphics();
  58             g2d.setColor(Color.green);
  59             g2d.fillRect(0, 0, width / 4, height / 4);
  60             g2d.dispose();
  61 
  62             if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
  63                 try {
  64                     Thread.sleep(100);
  65                 } catch (InterruptedException ignored) {
  66                 }
  67                 continue;
  68             }
  69 
  70             Graphics2D g = bi.createGraphics();
  71             g.setComposite(AlphaComposite.Src);
  72             g.setColor(Color.red);
  73             g.fillRect(0, 0, width, height);
  74             // Use sx and sy outside of VI bounds.
  75             g.drawImage(vi, 0, 0, width / 2, height / 2, 0, 0, width * 2,
  76                         height * 2, null);
  77             g.dispose();
  78 
  79             if (vi.contentsLost()) {
  80                 try {
  81                     Thread.sleep(100);
  82                 } catch (InterruptedException ignored) {
  83                 }
  84                 continue;
  85             }
  86 
  87             for (int x = 0; x < width; ++x) {
  88                 for (int y = 0; y < height; ++y) {
  89                     if (x < width / 16 && y < height / 16) {
  90                         if (bi.getRGB(x, y) != Color.green.getRGB()) {
  91                             throw new RuntimeException("Test failed.");
  92                         }
  93                     } else {
  94                         if (bi.getRGB(x, y) != Color.red.getRGB()) {
  95                             throw new RuntimeException("Test failed.");
  96                         }
  97                     }
  98                 }
  99             }
 100             break;
 101         }
 102     }
 103 }