1 /*
   2  * Copyright (c) 2011, 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  * @test
  26  * @summary <rdar://problem/4756727> [JavaJDK16] Drawing text first with a custom composite might break
  27  * @summary com.apple.junit.java.graphics.images
  28  * @library ../../regtesthelpers
  29  * @build BITestUtils
  30  * @run junit CopyAreaOffScreen
  31  */
  32 
  33 import test.java.awt.regtesthelpers.BITestUtils;
  34 import junit.framework.*;
  35 import java.awt.*;
  36 import java.awt.image.BufferedImage;
  37 
  38 public class CopyAreaOffScreen extends TestCase {
  39     int w = 50, h = 50;
  40     int x1 = 100, y1 = 100;
  41     int dx = 50, dy = 0;
  42 
  43     public void testCopyArea() throws Exception {
  44         GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
  45 
  46         GraphicsDevice device = environment.getDefaultScreenDevice();
  47 
  48         GraphicsConfiguration config = device.getDefaultConfiguration();
  49 
  50         // Create an image that does not support transparency (Opaque)
  51         BufferedImage bi = config.createCompatibleImage(500, 500, Transparency.OPAQUE);
  52 
  53         Graphics2D big = bi.createGraphics();
  54 
  55         big.setColor(Color.red);
  56         big.fillRect(0, 0, 1000, 1000);
  57 
  58         big.setColor(Color.green);
  59         big.fillRect(x1, y1, w, h);
  60 
  61         big.setColor(Color.blue);
  62         big.fillRect(x1+dx, y1+dy, w, h);
  63 
  64         big.copyArea(x1, y1, w, h, dx, dy);
  65 
  66         Point origPixel = new Point(x1 + 10, y1 + 10);
  67         Point copiedPixel = new Point(x1 + dx + 10, y1 + dy + 10);
  68 
  69         int greenPixel = bi.getRGB(origPixel.x, origPixel.y);
  70         int copiedGreenPixel = bi.getRGB(copiedPixel.x, copiedPixel.y);
  71 
  72         // human readable messages
  73         String c1msg = "@(" + origPixel.x + "," + origPixel.y + ") : " + BITestUtils.Hex(greenPixel);
  74         String c2msg = "@(" + copiedPixel.x + "," + copiedPixel.y + ") : " + BITestUtils.Hex(copiedGreenPixel);
  75         assertEquals( "Colors " + c1msg + " and " + c2msg + " should match", greenPixel, copiedGreenPixel);
  76     }
  77 
  78     public static Test suite() {
  79         return new TestSuite(CopyAreaOffScreen.class);
  80     }
  81 
  82     public static void main (String[] args) throws RuntimeException {
  83         TestResult tr = junit.textui.TestRunner.run(suite());
  84         if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  85             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures.");
  86         }
  87     }
  88 
  89     public void setUp() {
  90         new MyFrame("CopyArea");
  91     }
  92 
  93     protected void tearDown() {
  94         frame.dispose();
  95     }
  96 
  97     MyFrame frame;
  98 
  99     class MyFrame extends Frame {
 100         Robot robot;
 101 
 102         public MyFrame(String str) {
 103             super(str);
 104 
 105             frame = this;
 106 
 107             init();
 108         }
 109 
 110         public void init() {
 111             setSize(400, 400);
 112             setVisible(true);
 113         }
 114 
 115         public void paint(Graphics g) {
 116             GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
 117 
 118             GraphicsDevice device = environment.getDefaultScreenDevice();
 119 
 120             GraphicsConfiguration config = device.getDefaultConfiguration();
 121 
 122             // Create an image that does not support transparency (Opaque)
 123             BufferedImage bi = config.createCompatibleImage(500, 500, Transparency.OPAQUE);
 124 
 125             Graphics2D big = bi.createGraphics();
 126 
 127             big.setColor(Color.red);
 128             big.fillRect(0, 0, 1000, 1000);
 129 
 130             big.setColor(Color.green);
 131             big.fillRect(x1, y1, w, h);
 132 
 133             big.setColor(Color.blue);
 134             big.fillRect(x1+dx, y1+dy, w, h);
 135 
 136             big.copyArea(x1, y1, w, h, dx, dy);
 137 
 138             g.drawImage(bi, 0, 0, null);
 139         }
 140     }
 141 }