BufferStrategy problem on Mac

I set out to do some compatibility testing with the Mac and I blew up on my splash screen!

My splash screen fades in from white on a Canvas placed on a JWindow. This code works INTERMITTENLY!?!? Argh! There’s a 1 in 3 chance that my Splash Screen will work because 2 times out of 3 the Canvas’ size reverts to ( 0, 0 ) and my call to createBufferStrategy(2) blows up!
`
Exception in thread “main” java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0
at apple.awt.IntegerNIORaster.createNIORaster(IntegerNIORaster.java:22)
at apple.awt.CGraphicsConfig.createCompatibleImage(CGraphicsConfig.java:53)
at apple.awt.OSXOffScreenSurfaceData.createData(OSXOffScreenSurfaceData.java:115)
at apple.awt.OSXVolatileImage.createHWData(OSXVolatileImage.java:77)
at apple.awt.OSXVolatileImage.initAcceleratedBackground(OSXVolatileImage.java:87)
at sun.awt.image.SunVolatileImage.(SunVolatileImage.java:86)
at sun.awt.image.SunVolatileImage.(SunVolatileImage.java:77)
at apple.awt.OSXVolatileImage.(OSXVolatileImage.java:52)
at apple.awt.ComponentModel.createVolatileImage(ComponentModel.java:104)
at java.awt.Component.createVolatileImage(Component.java:2684)
at java.awt.Component$BltBufferStrategy.createBackBuffers(Component.java:3194)
at java.awt.Component$BltBufferStrategy.(Component.java:3180)
at java.awt.Component.createBufferStrategy(Component.java:2927)
at java.awt.Canvas.createBufferStrategy(Canvas.java:166)
at java.awt.Component.createBufferStrategy(Component.java:2867)
at java.awt.Canvas.createBufferStrategy(Canvas.java:141)
at org.???.cio.visual.comp.SplashScreen.(SplashScreen.java:42)
at org.???.cio.visual.comp.MainFrame.launchSplashScreen(MainFrame.java:265)
at org.???.cio.visual.comp.MainFrame.(MainFrame.java:81)
at org.???.cio.ExPattern.main(ExPattern.java:26)

`


            Canvas canvas = new Canvas();
            SplashScreenAnimator splashAnimator =
                    new SplashScreenAnimator( productName, versionNumber );
            System.out.println( splashAnimator.getPreferredSize() ); // see expected size
            canvas.setSize( splashAnimator.getPreferredSize() );
            getContentPane().add( canvas, BorderLayout.CENTER);
            pack();
            System.out.println( canvas.getHeight() + " " + canvas.getWidth() ); // see unexpected 0 0 
            canvas.createBufferStrategy( 2 );  //BOOM!

disclaimer: I’m not working on a game. in the future I’ll be doing more interactive graphics and i hope to have things to contribute.

So, which one of the Duncan’s are you exactly? :wink:

I’m the ghola Hait today :stuck_out_tongue:

The work around for my problem was simply to reset the size after calling pack().

Pack(), you will recall, needs to be called before you can create a buffer strategy. Why it makes my canvas (0, 0) intermittenly is one of the OSX team.

so did it work? seems when i do it. i get a white block drawn over the frame bounds. i can see stuff is being drawn behind the big white block (eye balls the 1 pixel edge of the frame).

I tried several different things… and I got it only running once (resetting the size after pack()) - and that was randomly. It didn’t worked again… d’uh.

Therefore I just throw BufferStrategy out of my code… with my own doublebuffering it worked out of the box on all testetd platforms (win/mac/linux).

I won’t ever use BufferStragegy again, if I don’t need it (the only case were it’s required is fullscreen).

aNt - Yup.


// in my JWindow subclass
        super(f);
        Canvas canvas = new Canvas();
        final SplashScreenAnimator splashAnimator =
                new SplashScreenAnimator( productName, versionNumber );
        canvas.setSize( splashAnimator.getPreferredSize() );
        getContentPane().add( canvas, BorderLayout.CENTER );
        pack();
        setSize( splashAnimator.getPreferredSize() );  // Mac issue.  otherwise Canvas will lose it's size
        canvas.setSize( splashAnimator.getPreferredSize() );  // Mac issue.  otherwise Canvas will lose it's size
        canvas.createBufferStrategy( 2 );

Since I started setting both my JWindows size and the Canvas’ size I haven’t crashed on my Mac.

Have you considered overriding canvas.getPreferredSize(), etc…

That’s what the layout manager will query when you call pack()… thus overriding whatever you did with setSize().