Path: blob/master/test/jdk/sun/java2d/DirectX/SwingOnScreenScrollingTest/SwingOnScreenScrollingTest.java
41152 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @key headful25* @bug 6630702 819861326* @summary Tests that scrolling after paint() is performed correctly.27* This is really only applicable to Vista28* @author [email protected]: area=Graphics29* @run main/othervm SwingOnScreenScrollingTest30*/3132import java.awt.AWTException;33import java.awt.Color;34import java.awt.Dimension;35import java.awt.EventQueue;36import java.awt.Graphics;37import java.awt.GraphicsEnvironment;38import java.awt.Point;39import java.awt.Rectangle;40import java.awt.Robot;41import java.awt.image.BufferedImage;42import java.io.File;43import java.lang.reflect.InvocationTargetException;44import javax.imageio.ImageIO;45import javax.swing.JFrame;46import javax.swing.JPanel;47import javax.swing.JScrollPane;4849public class SwingOnScreenScrollingTest extends JPanel {5051static JScrollPane pane;52static SwingOnScreenScrollingTest test;5354public SwingOnScreenScrollingTest() {55}5657public static void main(String[] args) {58int size = GraphicsEnvironment.59getLocalGraphicsEnvironment().60getDefaultScreenDevice().61getDefaultConfiguration().getColorModel().getPixelSize();62if (size < 16) {63System.err.println("<16 bit display mode detected. Test PASSED");64return;65}6667final JFrame f = new JFrame("SwingOnScreenScrollingTest");68try {69EventQueue.invokeAndWait(new Runnable() {70public void run() {71test = new SwingOnScreenScrollingTest();72pane = new JScrollPane(test);73f.add(pane);74f.pack();75f.setSize(100, 200);76f.setVisible(true);77}78});79} catch (InvocationTargetException ex) {80ex.printStackTrace();81} catch (InterruptedException ex) {82ex.printStackTrace();83}84try {85Thread.sleep(500);86} catch (InterruptedException ex) {87ex.printStackTrace();88}89EventQueue.invokeLater(new Runnable() {90public void run() {91BufferedImage bi;92bi = new BufferedImage(100, 300,93BufferedImage.TYPE_INT_RGB);94Graphics gg = bi.getGraphics();95test.paint(gg);96for (int y = 80; y < 200; y +=10) {97test.scrollRectToVisible(new Rectangle(0, y, 100, 100));98try {99Thread.sleep(200);100} catch (InterruptedException ex) {101ex.printStackTrace();102}103}104Point p = pane.getViewport().getLocationOnScreen();105Robot r = null;106try {107r = new Robot();108} catch (AWTException ex) {109throw new RuntimeException(ex);110}111bi = r.createScreenCapture(new Rectangle(p.x+5, p.y+5, 30, 30));112for (int y = 0; y < bi.getHeight(); y++) {113for (int x = 0; x < bi.getHeight(); x++) {114int rgb = bi.getRGB(x, y);115if (bi.getRGB(x, y) != Color.red.getRGB()) {116System.err.printf("Test Failed at (%d,%d) c=0x%x\n",117x, y, rgb);118try {119String name =120"SwingOnScreenScrollingTest_out.png";121ImageIO.write(bi, "png", new File(name));122System.err.println("Wrote grabbed image to "+123name);124} catch (Throwable ex) {}125throw new RuntimeException("Test failed");126}127}128}129System.out.println("Test PASSED.");130f.dispose();131}132});133}134135protected void paintComponent(Graphics g) {136g.setColor(Color.green);137g.fillRect(0, 0, getWidth(), 100);138g.setColor(Color.red);139g.fillRect(0, 100, getWidth(), getHeight()-100);140}141142public Dimension getPreferredSize() {143return new Dimension(100, 300);144}145}146147148