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 com.apple.junit.java.graphics;
  27  @library ../regtesthelpers
  28  @build BITestUtils
  29  @run junit CopyAreaOnScreen
  30  */
  31 
  32 import junit.framework.*;
  33 import java.awt.*;
  34 import test.java.awt.regtesthelpers.BITestUtils;
  35 
  36 public class CopyAreaOnScreen extends TestCase
  37 {
  38     int w = 50, h = 50;
  39     int x1 = 100, y1 = 100;
  40     int dx = 50, dy = 0;
  41 
  42     public void test() throws Exception
  43     {
  44         Thread.sleep(1000);
  45 
  46         // start location
  47         final int c1x = x1+(w/2);
  48         final int c1y = y1+(h/2);
  49 
  50         //copy location
  51         final int c2x = c1x+dx;
  52         final int c2y = c1y+dy;
  53 
  54         Color c1 = frame.getPixel(c1x, c1y);
  55         Color c2 = frame.getPixel(c2x, c2y);
  56 
  57         // human readable messages
  58         String c1msg = "@(" + c1x + "," + c1y + ") : " + BITestUtils.Hex(c1.getRGB());
  59         String c2msg = "@(" + c2x + "," + c2y + ") : " + BITestUtils.Hex(c2.getRGB());
  60         assertEquals( "Colors " + c1msg + " and " + c2msg + " should match", c1.getRGB(), c2.getRGB());
  61     }
  62 
  63     public static Test suite()
  64     {
  65         return new TestSuite(CopyAreaOnScreen.class);
  66     }
  67 
  68     public static void main (String[] args) throws RuntimeException {
  69         TestResult tr = junit.textui.TestRunner.run(suite());
  70         if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  71             throw new RuntimeException("### Unexpected errors or failures.");
  72         }
  73     }
  74 
  75     public void setUp()
  76     {
  77         new MyFrame("CopyArea");
  78     }
  79 
  80     protected void tearDown()
  81     {
  82         frame.dispose();
  83     }
  84 
  85     MyFrame frame;
  86 
  87     class MyFrame extends Frame
  88     {
  89         Robot robot;
  90 
  91         public MyFrame(String str)
  92         {
  93             super(str);
  94 
  95             frame = this;
  96 
  97             init();
  98 
  99             // give the frame a chance to get up
 100             try
 101             {
 102                 Thread.sleep(1000);
 103             }
 104             catch (Exception e)
 105             {
 106                 throw new Error(e.getMessage());
 107             }
 108 
 109             try
 110             {
 111                 robot = new Robot();
 112             }
 113             catch (AWTException e)
 114             {
 115                 throw new Error(e.getMessage());
 116             }
 117 
 118             robot.setAutoWaitForIdle(true);
 119         }
 120 
 121         public void init()
 122         {
 123             setSize(400, 400);
 124             setVisible(true);
 125         }
 126 
 127         public void paint(Graphics g)
 128         {
 129             g.setColor(Color.red);
 130             g.fillRect(0, 0, 1000, 1000);
 131 
 132             g.setColor(Color.green);
 133             g.fillRect(x1, y1, w, h);
 134 
 135             g.setColor(Color.blue);
 136             g.fillRect(x1+dx, y1+dy, w, h);
 137 
 138             g.copyArea(x1, y1, w, h, dx, dy);
 139         }
 140 
 141         public Color getPixel(int x, int y)
 142         {
 143             int locX = (int)(getLocationOnScreen().getX())+x;
 144             int locY = (int)(getLocationOnScreen().getY())+y;
 145             return robot.getPixelColor(locX, locY);
 146         }
 147     }
 148 }