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/3597830> graphics.drawImage() throws exception drawn with gradient background
  27  * @summary com.apple.junit.java.graphics.DrawImage
  28  * @library ../regtesthelpers
  29  * @build VisibilityValidator
  30  * @run junit DrawImageOverGradient
  31  */
  32 
  33 import test.java.awt.regtesthelpers.VisibilityValidator;
  34 import junit.framework.*;
  35 import javax.swing.*;
  36 import java.awt.*;
  37 import java.awt.image.BufferedImage;
  38 
  39 public class DrawImageOverGradient extends TestCase {
  40 
  41     protected JFrame frame;
  42 
  43     protected void setUp() {
  44         frame = new JFrame( "TestCase" );
  45 
  46         JComponent content = (JComponent) frame.getContentPane();
  47         content.setLayout(new FlowLayout());
  48         content.add(new ImagePanel());
  49     }
  50 
  51     public void testDrawImage() throws Exception {
  52         frame.setLocation( 100, 100 );
  53         frame.pack();
  54         VisibilityValidator.setVisibleAndConfirm(frame);
  55         Thread.sleep(50); //make sure the frame shows up on the screen
  56     }
  57 
  58     public void tearDown() {
  59         frame.dispose();
  60     }
  61 
  62     // Boilerplate below so it can be run from the command line
  63     public static Test suite() {
  64         return new TestSuite(DrawImageOverGradient.class);
  65     }
  66 
  67     public static void main (String[] args) throws RuntimeException {
  68         TestResult tr = junit.textui.TestRunner.run(suite());
  69         if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  70             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures.");
  71         }
  72     }
  73 
  74     //the panel with the image on it.
  75     private static class ImagePanel extends JComponent {
  76         BufferedImage image;
  77 
  78         public ImagePanel() {
  79 
  80             // exceptions used to be thrown when I tried to draw the image created below,
  81             // but only when I tried to draw it atop a GRADIENT:
  82             image = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB_PRE);
  83             Graphics2D g = image.createGraphics();
  84             g.setColor(Color.orange);
  85             g.drawLine(1, 1, image.getWidth()-1, image.getHeight()-1);
  86             g.drawLine(image.getWidth()-1, 1, 1, image.getHeight()-1);
  87 
  88             setOpaque(true);
  89             setVisible(true);
  90             setPreferredSize(new Dimension(80, 80));
  91         }
  92 
  93         protected void paintComponent(Graphics g)  {
  94             //the gradient
  95             Graphics2D g2 = (Graphics2D)g;
  96             GradientPaint gradient = new GradientPaint(0, 0, Color.white, getWidth()/3, getHeight()/3, Color.green, true); //true is for cyclic
  97             g2.setPaint(gradient);
  98             g2.fillRect(0, 0, getWidth(), getHeight());
  99 
 100             //draw the image and pray for no exceptions.
 101             g.drawImage(image, 0,  0 , null);
 102 
 103         }
 104     }
 105 }