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/3612381>: Graphics.drawImage ignores bgcolor parameter
  27  @summary com.apple.junit.java.graphics.DrawImage;
  28  @run main DrawImageWithBackground
  29  */
  30 
  31 import junit.framework.*;
  32 
  33 import javax.swing.*;
  34 import java.awt.*;
  35 import java.awt.image.BufferedImage;
  36 
  37 public class DrawImageWithBackground extends TestCase {
  38 
  39     static GraphicsEnvironment        ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  40     static GraphicsDevice             gd = ge.getDefaultScreenDevice();
  41     static GraphicsConfiguration    gc = gd.getDefaultConfiguration();
  42 
  43     protected JFrame frame;
  44     protected Image src,dst;
  45 
  46     //the panel with the image on it.
  47     private class ImagePanel extends JComponent {
  48 
  49         public ImagePanel() {
  50             setOpaque(true);
  51             setVisible(true);
  52         }
  53 
  54         public Dimension getPreferredSize()  {
  55             return new Dimension(150, 150);
  56         }
  57 
  58         protected void paintComponent(Graphics g)  {
  59             g.drawImage(dst, 0,  0 , null);
  60         }
  61     }
  62 
  63     protected void setUp() {
  64         frame = new JFrame( "TestCase" );
  65         src = gc.createCompatibleImage( 150, 150, Transparency.TRANSLUCENT );
  66         dst = gc.createCompatibleImage( 150, 150, Transparency.TRANSLUCENT );
  67 
  68         JComponent content = (JComponent) frame.getContentPane();
  69         content.setLayout(new FlowLayout());
  70         content.add(new ImagePanel());
  71     }
  72 
  73     public void testDrawImage() throws InterruptedException {
  74 
  75         Graphics g = dst.getGraphics();
  76         g.setColor(Color.white);
  77         g.fillRect(0, 0, 150, 150);
  78         g.drawImage(src, 0,  0 , Color.orange, null); // <--- this is the meat of the test
  79         g.dispose();
  80 
  81         frame.setLocation( 100, 100 );
  82         frame.pack();
  83         frame.setVisible( true );
  84 
  85         Thread.sleep(250); //make sure the frame shows up on the screen
  86 
  87         int currColor = ((BufferedImage)dst).getRGB(75,75);
  88         // System.out.println( "Actual\t" + Integer.toHexString(currColor));
  89         // System.out.println( "Expected\t" + Integer.toHexString(0xffffc800));
  90         assertEquals("pixel in middle should be orange", 0xffffc800, currColor);
  91     }
  92 
  93     public void tearDown() {
  94         frame.dispose();
  95     }
  96 
  97     // Boilerplate below so it can be run from the command line
  98     public static Test suite() {
  99         return new TestSuite(DrawImageWithBackground.class);
 100     }
 101 
 102     public static void main (String[] args) throws RuntimeException {
 103         TestResult tr = junit.textui.TestRunner.run(suite());
 104         if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
 105             throw new RuntimeException("### Unexpected JUnit errors or failures.");
 106         }
 107     }
 108 }