1 /*
   2  * Copyright (c) 2007, 2008, 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  * @test
  25  * @bug 6630702
  26  * @summary Tests that scrolling after paint() is performed correctly.
  27  *          This is really only applicable to Vista
  28  * @author Dmitri.Trembovetski@sun.com: area=Graphics
  29  * @run main/othervm SwingOnScreenScrollingTest
  30  * run main/othervm -Dsun.java2d.opengl=True SwingOnScreenScrollingTest
  31  */
  32 
  33 import java.awt.AWTException;
  34 import java.awt.Color;
  35 import java.awt.Dimension;
  36 import java.awt.EventQueue;
  37 import java.awt.Graphics;
  38 import java.awt.GraphicsEnvironment;
  39 import java.awt.Point;
  40 import java.awt.Rectangle;
  41 import java.awt.Robot;
  42 import java.awt.image.BufferedImage;
  43 import java.io.File;
  44 import java.lang.reflect.InvocationTargetException;
  45 import javax.imageio.ImageIO;
  46 import javax.swing.JFrame;
  47 import javax.swing.JPanel;
  48 import javax.swing.JScrollPane;
  49 
  50 public class SwingOnScreenScrollingTest extends JPanel {
  51 
  52     static JScrollPane pane;
  53     static SwingOnScreenScrollingTest test;
  54 
  55     public SwingOnScreenScrollingTest() {
  56     }
  57 
  58     public static void main(String[] args) {
  59         int size = GraphicsEnvironment.
  60             getLocalGraphicsEnvironment().
  61                 getDefaultScreenDevice().
  62                     getDefaultConfiguration().getColorModel().getPixelSize();
  63         if (size < 16) {
  64             System.err.println("<16 bit display mode detected. Test PASSED");
  65             return;
  66         }
  67 
  68         final JFrame f = new JFrame("SwingOnScreenScrollingTest");
  69         try {
  70             EventQueue.invokeAndWait(new Runnable() {
  71                 public void run() {
  72                     test = new SwingOnScreenScrollingTest();
  73                     pane = new JScrollPane(test);
  74                     f.add(pane);
  75                     f.pack();
  76                     f.setSize(100, 200);
  77                     f.setVisible(true);
  78                 }
  79             });
  80         } catch (InvocationTargetException ex) {
  81             ex.printStackTrace();
  82         } catch (InterruptedException ex) {
  83             ex.printStackTrace();
  84         }
  85         try {
  86             Thread.sleep(500);
  87         } catch (InterruptedException ex) {
  88             ex.printStackTrace();
  89         }
  90         EventQueue.invokeLater(new Runnable() {
  91             public void run() {
  92                 BufferedImage bi;
  93                 bi = new BufferedImage(100, 300,
  94                                        BufferedImage.TYPE_INT_RGB);
  95                 Graphics gg = bi.getGraphics();
  96                 test.paint(gg);
  97                 for (int y = 80; y < 200; y +=10) {
  98                     test.scrollRectToVisible(new Rectangle(0, y, 100, 100));
  99                     try {
 100                         Thread.sleep(200);
 101                     } catch (InterruptedException ex) {
 102                         ex.printStackTrace();
 103                     }
 104                 }
 105                 Point p = pane.getViewport().getLocationOnScreen();
 106                 Robot r = null;
 107                 try {
 108                     r = new Robot();
 109                 } catch (AWTException ex) {
 110                     throw new RuntimeException(ex);
 111                 }
 112                 bi = r.createScreenCapture(new Rectangle(p.x+5, p.y+5, 30, 30));
 113                 for (int y = 0; y < bi.getHeight(); y++) {
 114                     for (int x = 0; x < bi.getHeight(); x++) {
 115                         int rgb = bi.getRGB(x, y);
 116                         if (bi.getRGB(x, y) != Color.red.getRGB()) {
 117                             System.err.printf("Test Failed at (%d,%d) c=0x%x\n",
 118                                               x, y, rgb);
 119                             try {
 120                                 String name =
 121                                     "SwingOnScreenScrollingTest_out.png";
 122                                 ImageIO.write(bi, "png", new File(name));
 123                                 System.err.println("Wrote grabbed image to "+
 124                                                    name);
 125                             } catch (Throwable ex) {}
 126                             throw new RuntimeException("Test failed");
 127                         }
 128                     }
 129                 }
 130                 System.out.println("Test PASSED.");
 131                 f.dispose();
 132             }
 133         });
 134     }
 135 
 136     protected void paintComponent(Graphics g) {
 137         g.setColor(Color.green);
 138         g.fillRect(0, 0, getWidth(), 100);
 139         g.setColor(Color.red);
 140         g.fillRect(0, 100, getWidth(), getHeight()-100);
 141     }
 142 
 143     public Dimension getPreferredSize() {
 144         return new Dimension(100, 300);
 145     }
 146 }