/* * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * */ import java.awt.*; import java.awt.image.*; import javax.swing.*; import java.io.File; /** * This test checks for the following driver issues: * Nvidia Issue #232931: Swing apps not visible when FBO codepath enabled * Nvidia Issue #232935: Swing apps crash when FBO codepath enabled * Also, JDK-8007971 - Linux painting issue (both Nvidia and ATI) */ public class JPanelTest extends JPanel { private static final int TESTW = 100; private static final int TESTH = 100; private static boolean done, show; private static volatile BufferedImage capture; static JPanelTest instance = null; // single instance! static BufferedImage ref = null; static JFrame frame = null; private static Robot robot = null; // detect lower corners to exclude them from comparison // it is necessary on mac Aqua etc. private static int xCornerTolerance = 3; private static int yCornerTolerance = 3; private static boolean isLowerCorner(int x1, int y1, int x2, int y2, int x, int y) { return (x < x1+xCornerTolerance && y > y2-yCornerTolerance) || (x > x2-xCornerTolerance && y > y2-yCornerTolerance); } public void paintComponent(Graphics g) { super.paintComponent(g); renderPattern((Graphics2D)g); } private static void renderPattern(Graphics2D g) { g.setColor(Color.blue); g.fillRect(0, 0, 50, 50); g.setColor(Color.green); g.fillRect(50, 0, 50, 50); g.setColor(Color.red); g.fillRect(0, 50, 50, 50); g.setColor(Color.yellow); g.fillRect(50, 50, 50, 50); } private static BufferedImage createReferenceImage() { BufferedImage bi = new BufferedImage(TESTW, TESTH, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); renderPattern(g); g.dispose(); return bi; } static private void saveBImage(BufferedImage image, File file) { if(image != null) { try { javax.imageio.ImageIO.write(image, "PNG", file); } catch (Exception e) { throw new RuntimeException("Could not write image file "+file.getPath()); } } else { throw new RuntimeException("BufferedImage was set to null"); } } private static void compareImages(BufferedImage imgExpected, BufferedImage imgActual) { for (int y = 0; y < imgExpected.getHeight(); y++) { for (int x = 0; x < imgExpected.getWidth(); x++) { if(isLowerCorner(0, 0, TESTW, TESTH, x, y)) { continue; } int expected = imgExpected.getRGB(x, y); int actual = imgActual.getRGB(x, y); if (actual != expected) { File file2 = new File("jpt_exp.png"); saveBImage(imgExpected, file2); File file1 = new File("jpt_act.png"); saveBImage(imgActual, file1); throw new RuntimeException("Test failed at x="+x+" y="+y+ " (expected="+ Integer.toHexString(expected) + " actual="+ Integer.toHexString(actual) + ")"); } } } } public Dimension getPreferredSize() { return new Dimension(TESTW, TESTH); } public static void main(String[] args) { boolean show = (args.length == 1) && ("-show".equals(args[0])); System.err.println("________________________________________"); try { robot = new Robot(); }catch(Exception exro) { throw new RuntimeException("Failed: Robot creation problem"); } try{ SwingUtilities.invokeAndWait(new Runnable() { public void run() { frame = new JFrame(); instance = new JPanelTest(); instance.setBounds(0,0,100,100); frame.add(instance); frame.pack(); frame.setVisible(true); ref = createReferenceImage(); } }); }catch(InterruptedException iex) { throw new RuntimeException("Failed: Interrupted"); }catch(java.lang.reflect.InvocationTargetException itexx ) { itexx.printStackTrace(); throw new RuntimeException(itexx.getCause()); } try{Thread.sleep(2000);}catch(Exception exx){} // as of now, realSync doesn't work on Xfce, embedded WMs etc. //((sun.awt.SunToolkit)(Toolkit.getDefaultToolkit())).realSync(); Point pt1 = instance.getLocationOnScreen(); Rectangle rect = new Rectangle(pt1.x, pt1.y, TESTW, TESTH); capture = robot.createScreenCapture(rect); if (!show) { frame.dispose(); } compareImages(ref, capture); } }