JDK-6750920 : Window transparency throws IAE if called before pack or setSize
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 6u10
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_vista
  • CPU: generic
  • Submitted: 2008-09-22
  • Updated: 2011-01-19
  • Resolved: 2008-11-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6 JDK 7
6u12 b01Fixed 7Fixed
Description
Window transparency throws IAE if called before pack or setSize. The test case below throws IAE. However, if pack is moved before the call to setWindowOpaque, all is fine.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TPTest extends JFrame {
    
    public TPTest() {
        super("TPTest");
        
        add(new JButton("Hi!"));
    }

    private static void createAndShowGUI(String[] args) {
        TPTest test = new TPTest();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //test.setSize(400, 400);
        //test.setLocationRelativeTo(null);
        //test.setVisible(true);
        test.setUndecorated(true);
        com.sun.awt.AWTUtilities.setWindowOpaque(test, false);
        test.pack();
        test.setVisible(true);
    }
    
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI(args);
            }
        });
    }
}

IAE:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0
	at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:999)
	at sun.awt.image.SunVolatileImage.getBackupImage(SunVolatileImage.java:225)
	at sun.awt.image.VolatileSurfaceManager.getBackupSurface(VolatileSurfaceManager.java:252)
	at sun.awt.image.VolatileSurfaceManager.initialize(VolatileSurfaceManager.java:108)
	at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:72)
	at sun.awt.image.SunVolatileImage.<init>(SunVolatileImage.java:101)
	at java.awt.GraphicsConfiguration.createCompatibleVolatileImage(GraphicsConfiguration.java:284)
	at java.awt.GraphicsConfiguration.createCompatibleVolatileImage(GraphicsConfiguration.java:218)
	at sun.awt.windows.TranslucentWindowPainter$VIWindowPainter.getBackBuffer(TranslucentWindowPainter.java:279)
	at sun.awt.windows.TranslucentWindowPainter.updateWindow(TranslucentWindowPainter.java:131)
	at sun.awt.windows.WWindowPeer.updateWindow(WWindowPeer.java:652)
	at sun.awt.windows.WWindowPeer.setOpaque(WWindowPeer.java:639)
	at java.awt.Window.applyOpaque(Window.java:3299)
	at java.awt.Window.addNotify(Window.java:652)
	at java.awt.Frame.addNotify(Frame.java:469)
	at java.awt.Window.pack(Window.java:686)
	at TPTest.createAndShowGUI(TPTest.java:21)
	at TPTest.access$000(TPTest.java:5)
	at TPTest$1.run(TPTest.java:28)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Comments
EVALUATION We need to check if the window is zero-sized in the WWindowPeer.updateWindow(). If so, the updateWindow() request should be ignored.
23-09-2008

SUGGESTED FIX --- old/src/windows/classes/sun/awt/windows/WWindowPeer.java 2008-09-23 16:38:26.000000000 +0400 +++ new/src/windows/classes/sun/awt/windows/WWindowPeer.java 2008-09-23 16:38:26.000000000 +0400 @@ -647,6 +647,11 @@ return; } + Component target = (Component)this.target; + if (target.getWidth() <= 0 || target.getHeight() <= 0) { + return; + } + TranslucentWindowPainter currentPainter = painter; if (currentPainter!= null) { currentPainter.updateWindow(backBuffer); --- /dev/null 2008-04-09 14:36:21.096432000 +0400 +++ new/test/com/sun/awt/Translucency/ZeroSizeUpdateWindowIAE.java 2008-09-23 16:38:26.000000000 +0400 @@ -0,0 +1,379 @@ +/* + @test + @bug 6750920 + @summary Enabling the non-opaque mode throws IAE if made before pack() + @author anthony.petrov@...: area=awt.toplevel + @run main ZeroSizeUpdateWindowIAE +*/ + +/** + * ZeroSizeUpdateWindowIAE.java + * + * summary: Enabling the non-opaque mode throws IAE if made before pack() + */ + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import com.sun.awt.AWTUtilities; + +public class ZeroSizeUpdateWindowIAE +{ + private static GraphicsConfiguration getTranslucencyCapableGC() { + GraphicsEnvironment env = + GraphicsEnvironment.getLocalGraphicsEnvironment(); + GraphicsDevice[] devices = env.getScreenDevices(); + + for (int i = 0; i < devices.length; i++) { + GraphicsConfiguration[] configs = devices[i].getConfigurations(); + for (int j = 0; j < configs.length; j++) { + if (AWTUtilities.isTranslucencyCapable(configs[j])) { + return configs[j]; + } + } + } + return null; + } + + + private static void init() + { + String[] instructions = + { + "This is an AUTOMATIC test, simply wait until it is done.", + "The result (passed or failed) will be shown in the", + "message window below." + }; + Sysout.createDialog( ); + Sysout.printInstructions( instructions ); + + if (!AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.PERPIXEL_TRANSLUCENT)) { + System.out.println("Either the Toolkit or the native system does not support per-pixel translucency."); + pass(); + return; + } + + GraphicsConfiguration gc = getTranslucencyCapableGC(); + if (gc == null) { + System.out.println("No translucency-capable GC found!"); + pass(); + return; + } + + JFrame f = new JFrame(gc); + f.add(new JButton("OK")); + f.setUndecorated(true); + AWTUtilities.setWindowOpaque(f, false); + + try { + f.pack(); + } catch (IllegalArgumentException iae) { + fail("pack() throws IllegalArgumentException: " + iae); + return; + } + + pass(); + }//End init() + + + + /***************************************************** + * Standard Test Machinery Section + * DO NOT modify anything in this section -- it's a + * standard chunk of code which has all of the + * synchronisation necessary for the test harness. + * By keeping it the same in all tests, it is easier + * to read and understand someone else's test, as + * well as insuring that all tests behave correctly + * with the test harness. + * There is a section following this for test- + * classes + ******************************************************/ + private static boolean theTestPassed = false; + private static boolean testGeneratedInterrupt = false; + private static String failureMessage = ""; + + private static Thread mainThread = null; + + private static int sleepTime = 300000; + + // Not sure about what happens if multiple of this test are + // instantiated in the same VM. Being static (and using + // static vars), it aint gonna work. Not worrying about + // it for now. + public static void main( String args[] ) throws InterruptedException + { + mainThread = Thread.currentThread(); + try + { + init(); + } + catch( TestPassedException e ) + { + //The test passed, so just return from main and harness will + // interepret this return as a pass + return; + } + //At this point, neither test pass nor test fail has been + // called -- either would have thrown an exception and ended the + // test, so we know we have multiple threads. + + //Test involves other threads, so sleep and wait for them to + // called pass() or fail() + try + { + Thread.sleep( sleepTime ); + //Timed out, so fail the test + throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" ); + } + catch (InterruptedException e) + { + //The test harness may have interrupted the test. If so, rethrow the exception + // so that the harness gets it and deals with it. + if( ! testGeneratedInterrupt ) throw e; + + //reset flag in case hit this code more than once for some reason (just safety) + testGeneratedInterrupt = false; + + if ( theTestPassed == false ) + { + throw new RuntimeException( failureMessage ); + } + } + + }//main + + public static synchronized void setTimeoutTo( int seconds ) + { + sleepTime = seconds * 1000; + } + + public static synchronized void pass() + { + Sysout.println( "The test passed." ); + Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); + //first check if this is executing in main thread + if ( mainThread == Thread.currentThread() ) + { + //Still in the main thread, so set the flag just for kicks, + // and throw a test passed exception which will be caught + // and end the test. + theTestPassed = true; + throw new TestPassedException(); + } + theTestPassed = true; + testGeneratedInterrupt = true; + mainThread.interrupt(); + }//pass() + + public static synchronized void fail() + { + //test writer didn't specify why test failed, so give generic + fail( "it just plain failed! :-)" ); + } + + public static synchronized void fail( String whyFailed ) + { + Sysout.println( "The test failed: " + whyFailed ); + Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); + //check if this called from main thread + if ( mainThread == Thread.currentThread() ) + { + //If main thread, fail now 'cause not sleeping + throw new RuntimeException( whyFailed ); + } + theTestPassed = false; + testGeneratedInterrupt = true; + failureMessage = whyFailed; + mainThread.interrupt(); + }//fail() + +}// class ZeroSizeUpdateWindowIAE + +//This exception is used to exit from any level of call nesting +// when it's determined that the test has passed, and immediately +// end the test. +class TestPassedException extends RuntimeException +{ +} + +//*********** End Standard Test Machinery Section ********** + + +//************ Begin classes defined for the test **************** + +// if want to make listeners, here is the recommended place for them, then instantiate +// them in init() + +/* Example of a class which may be written as part of a test +class NewClass implements anInterface + { + static int newVar = 0; + + public void eventDispatched(AWTEvent e) + { + //Counting events to see if we get enough + eventCount++; + + if( eventCount == 20 ) + { + //got enough events, so pass + + ZeroSizeUpdateWindowIAE.pass(); + } + else if( tries == 20 ) + { + //tried too many times without getting enough events so fail + + ZeroSizeUpdateWindowIAE.fail(); + } + + }// eventDispatched() + + }// NewClass class + +*/ + + +//************** End classes defined for the test ******************* + + + + +/**************************************************** + Standard Test Machinery + DO NOT modify anything below -- it's a standard + chunk of code whose purpose is to make user + interaction uniform, and thereby make it simpler + to read and understand someone else's test. + ****************************************************/ + +/** + This is part of the standard test machinery. + It creates a dialog (with the instructions), and is the interface + for sending text messages to the user. + To print the instructions, send an array of strings to Sysout.createDialog + WithInstructions method. Put one line of instructions per array entry. + To display a message for the tester to see, simply call Sysout.println + with the string to be displayed. + This mimics System.out.println but works within the test harness as well + as standalone. + */ + +class Sysout +{ + private static TestDialog dialog; + + public static void createDialogWithInstructions( String[] instructions ) + { + dialog = new TestDialog( new Frame(), "Instructions" ); + dialog.printInstructions( instructions ); + dialog.setVisible(true); + println( "Any messages for the tester will display here." ); + } + + public static void createDialog( ) + { + dialog = new TestDialog( new Frame(), "Instructions" ); + String[] defInstr = { "Instructions will appear here. ", "" } ; + dialog.printInstructions( defInstr ); + dialog.setVisible(true); + println( "Any messages for the tester will display here." ); + } + + + public static void printInstructions( String[] instructions ) + { + dialog.printInstructions( instructions ); + } + + + public static void println( String messageIn ) + { + dialog.displayMessage( messageIn ); + System.out.println(messageIn); + } + +}// Sysout class + +/** + This is part of the standard test machinery. It provides a place for the + test instructions to be displayed, and a place for interactive messages + to the user to be displayed. + To have the test instructions displayed, see Sysout. + To have a message to the user be displayed, see Sysout. + Do not call anything in this dialog directly. + */ +class TestDialog extends Dialog +{ + + TextArea instructionsText; + TextArea messageText; + int maxStringLength = 80; + + //DO NOT call this directly, go through Sysout + public TestDialog( Frame frame, String name ) + { + super( frame, name ); + int scrollBoth = TextArea.SCROLLBARS_BOTH; + instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); + add( "North", instructionsText ); + + messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); + add("Center", messageText); + + pack(); + + setVisible(true); + }// TestDialog() + + //DO NOT call this directly, go through Sysout + public void printInstructions( String[] instructions ) + { + //Clear out any current instructions + instructionsText.setText( "" ); + + //Go down array of instruction strings + + String printStr, remainingStr; + for( int i=0; i < instructions.length; i++ ) + { + //chop up each into pieces maxSringLength long + remainingStr = instructions[ i ]; + while( remainingStr.length() > 0 ) + { + //if longer than max then chop off first max chars to print + if( remainingStr.length() >= maxStringLength ) + { + //Try to chop on a word boundary + int posOfSpace = remainingStr. + lastIndexOf( ' ', maxStringLength - 1 ); + + if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; + + printStr = remainingStr.substring( 0, posOfSpace + 1 ); + remainingStr = remainingStr.substring( posOfSpace + 1 ); + } + //else just print + else + { + printStr = remainingStr; + remainingStr = ""; + } + + instructionsText.append( printStr + "\n" ); + + }// while + + }// for + + }//printInstructions() + + //DO NOT call this directly, go through Sysout + public void displayMessage( String messageIn ) + { + messageText.append( messageIn + "\n" ); + System.out.println(messageIn); + } + +}// TestDialog class
23-09-2008

WORK AROUND Call pack() before enabling the transparency mode.
23-09-2008