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 Simple test that draws couple of rectangle and then does getRGB() to verify that the correct color is returned. 27 * @summary com.apple.junit.java.graphics.images 28 * @run junit TestGetRGB 29 */ 30 31 import junit.framework.*; 32 import java.awt.*; 33 import java.awt.image.BufferedImage; 34 35 public class TestGetRGB extends TestCase { 36 37 static final int w = 400; 38 static final int h = 400; 39 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 40 GraphicsDevice gd = ge.getDefaultScreenDevice(); 41 GraphicsConfiguration gc = gd.getDefaultConfiguration(); 42 BufferedImage bi = gc.createCompatibleImage( w, h ); 43 44 45 public void testGetRGB() { 46 Graphics2D g2d = bi.createGraphics(); 47 g2d.setColor(Color.gray); 48 g2d.fillRect(0, 0, 400, 400); 49 g2d.setColor(Color.red); 50 g2d.fillRect(0, 0, 100, 100); 51 g2d.setColor(Color.green); 52 g2d.fillRect(110, 0, 100, 100); 53 54 assertEquals( "Testing for red at 10, 10", Color.red, new Color(bi.getRGB(10, 10))); 55 assertEquals( "Testing for gray at 105, 10", Color.gray, new Color(bi.getRGB(105, 10))); 56 assertEquals( "Testing for green at 150, 10", Color.green, new Color(bi.getRGB(150, 10))); 57 58 g2d.setColor(Color.blue); 59 g2d.fillRect(0, 0, 100, 100); 60 g2d.setColor(Color.black); 61 g2d.fillRect(110, 0, 100, 100); 62 63 assertEquals( "Testing for blue at 10, 10", Color.blue, new Color(bi.getRGB(10, 10))); 64 assertEquals( "Testing for black at 150, 10", Color.black, new Color(bi.getRGB(150, 10))); 65 } 66 67 68 public static Test suite() { 69 return new TestSuite(TestGetRGB.class); 70 } 71 72 73 public static void main (String[] args) throws RuntimeException { 74 TestResult tr = junit.textui.TestRunner.run(suite()); 75 if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) { 76 throw new RuntimeException("### FAILED: unexpected JUnit errors or failures."); 77 } 78 } 79 }