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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 import java.awt.AlphaComposite;
  27 import java.awt.Color;
  28 import java.awt.Graphics2D;
  29 import java.awt.GraphicsConfiguration;
  30 import java.awt.GraphicsEnvironment;
  31 import java.awt.Rectangle;
  32 import java.awt.TexturePaint;
  33 import java.awt.image.BufferedImage;
  34 import java.awt.image.VolatileImage;
  35 
  36 /**
  37  * @test
  38  * @bug 8000629
  39  * @summary TexturePaint areas shouldn't separates.
  40  * @author Sergey Bylokhov
  41  */
  42 public class FillTexturePaint {
  43 
  44     private static TexturePaint shape;
  45     private static final int size = 400;
  46 
  47     static {
  48         BufferedImage bi = new BufferedImage(50, 50,
  49                                              BufferedImage.TYPE_INT_RGB);
  50         Graphics2D gi = bi.createGraphics();
  51         gi.setBackground(Color.GREEN);
  52         gi.clearRect(0, 0, 50, 50);
  53         shape = new TexturePaint(bi, new Rectangle(0, 0, 50, 50));
  54     }
  55 
  56     public static void main(final String[] args) {
  57         GraphicsEnvironment ge =
  58                 GraphicsEnvironment.getLocalGraphicsEnvironment();
  59         GraphicsConfiguration gc =
  60                 ge.getDefaultScreenDevice().getDefaultConfiguration();
  61         VolatileImage vi = gc.createCompatibleVolatileImage(size, size);
  62         while (true) {
  63             vi.validate(gc);
  64             Graphics2D g2d = vi.createGraphics();
  65             g2d.setComposite(AlphaComposite.Src);
  66             g2d.setPaint(shape);
  67             g2d.fill(new Rectangle(0, 0, size, size));
  68             g2d.dispose();
  69 
  70             if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
  71                 try {
  72                     Thread.sleep(100);
  73                 } catch (InterruptedException ignored) {
  74                 }
  75                 continue;
  76             }
  77 
  78             BufferedImage bi = vi.getSnapshot();
  79 
  80             if (vi.contentsLost()) {
  81                 try {
  82                     Thread.sleep(100);
  83                 } catch (InterruptedException ignored) {
  84                 }
  85                 continue;
  86             }
  87 
  88             for (int x = 0; x < size; ++x) {
  89                 for (int y = 0; y < size; ++y) {
  90                     if (bi.getRGB(x, y) != Color.GREEN.getRGB()) {
  91                         throw new RuntimeException("Test failed.");
  92                     }
  93                 }
  94             }
  95             break;
  96         }
  97     }
  98 }