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 Asserts that a constructed frame with many buffered sub images does not returns null
  27  * @summary com.apple.junit.java.graphics.images
  28  * @run junit SubImages01
  29  */
  30 
  31 import junit.framework.*;
  32 import javax.swing.*;
  33 import java.awt.*;
  34 import java.awt.image.BufferedImage;
  35 import java.util.Vector;
  36 
  37 public class SubImages01 extends TestCase {
  38 
  39     public static Test suite() {
  40         return new TestSuite(SubImages01.class);
  41     }
  42 
  43     public static void main (String[] args) throws RuntimeException {
  44         TestResult tr = junit.textui.TestRunner.run(suite());
  45         if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  46             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures.");
  47         }
  48     }
  49 
  50     public class SubImageFrame extends JFrame {
  51         static final int w = 400;
  52         static final int h = 400;
  53         static final int rows = 20;
  54         static final int cols = 20;
  55         static final int siw = w / cols;
  56         static final int sih = h / rows;
  57 
  58         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  59         GraphicsDevice gd = ge.getDefaultScreenDevice();
  60         GraphicsConfiguration gc = gd.getDefaultConfiguration();
  61         BufferedImage bi = gc.createCompatibleImage( w, h );
  62 
  63         boolean initialized = false;
  64         boolean painted = false;
  65         Vector<BufferedImage> imageCache = new Vector<BufferedImage>( 16 );
  66         Vector<Point> pointCache = new Vector<Point>( 16 );
  67 
  68         class TestPanel extends JPanel {
  69             public TestPanel() {
  70                 setPreferredSize( new Dimension( w + (rows * cols), h + (rows * cols) ) );
  71             }
  72 
  73             public void paint( Graphics g ) {
  74                 super.paint( g );
  75                 Dimension dim = getSize();
  76                 if (initialized) {
  77                     if (bi != null) {
  78                         for (int i = 0; i < imageCache.size(); i++) {
  79                             BufferedImage si = (BufferedImage) imageCache.get( i );
  80                             Point p = (Point) pointCache.get( i );
  81                             g.drawImage( si, p.x + i, p.y + i, null );
  82                         }
  83                     }
  84                     if (!painted) {
  85                         painted = true;
  86                     }
  87                 }
  88                 g.setColor( Color.red );
  89                 g.drawRect( 0, 0, dim.width - 1, dim.height - 1 );
  90             }
  91         }
  92 
  93         public SubImageFrame() throws Exception {
  94             super( "SubImageFrame" );
  95             initBaseImage();
  96             initSubImages();
  97             getContentPane().add( new TestPanel() );
  98             pack();
  99             initialized = true;
 100             setVisible( true );
 101         }
 102 
 103         // Intialize the base image with a gradient fill.  We could use anything here.
 104         void initBaseImage() {
 105             Graphics2D ig = (Graphics2D) bi.createGraphics();
 106             GradientPaint gp = new GradientPaint( 0, 0, Color.red, w, h, Color.cyan, false );
 107             ig.setPaint( gp );
 108             ig.fillRect( 0, 0, w, h );
 109             ig.dispose();
 110         }
 111 
 112         // Chop the base image into 16 imageCache and cache them in a Vector
 113         void initSubImages() {
 114             for (int x = 0; x < w; x += siw) {
 115                 int ww = x + siw < w? siw: w - x;
 116                 for (int y = 0; y < h; y += sih) {
 117                     int hh = y + sih < h? sih: h - y;
 118                     BufferedImage subi = bi.getSubimage( x, y, ww, hh );
 119                     imageCache.addElement( subi );
 120                     pointCache.addElement( new Point( x, y ) );
 121                 }
 122             }
 123         }
 124 
 125     }
 126 
 127     public void testSubImages() throws Exception {
 128         SubImageFrame sif = null;
 129         try {
 130             sif = new SubImageFrame();
 131             Thread.sleep(1000);
 132         }
 133         finally  {
 134             assertNotNull(sif);
 135             sif.dispose();
 136         }
 137     }
 138 }