ClassCastException and BufferStrategy

I just want to draw a black window! (sob.)

I was messing around with kevglass’ space invaders tutorial, but couldn’t get anything to draw to the window, due to the following exception:


Exception in thread "main" java.lang.ClassCastException
   at apple.awt.StrategyBufferImage.<init>(StrategyBufferImage.java:26)
        at apple.awt.ContainerModel.createBuffers(ContainerModel.java:155)
        at java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:3111)
        at java.awt.Component$FlipBufferStrategy.<init>(Component.java:3080)
        at java.awt.Component.createBufferStrategy(Component.java:2989)
        at java.awt.Canvas.createBufferStrategy(Canvas.java:166)
        at java.awt.Component.createBufferStrategy(Component.java:2921)
        at java.awt.Canvas.createBufferStrategy(Canvas.java:141)
        at Game.<init>(Game.java:25)
        at Game.main(Game.java:42)

I’m new to this, so I wouldn’t be surprised if it were due to some silly oversight on my part. Any suggestions, corrections or workarounds would be greatly appreciated. I tried to simplify as much as possible for troubleshooting purposes. Here’s the code:


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

public class Game extends Canvas {

      private boolean gameRunning = true;

      public Game() {
            JFrame container = new JFrame("Black Rectangle");
            JPanel panel = (JPanel) container.getContentPane();
            panel.setPreferredSize(new Dimension(800,600));
            panel.setLayout(null);
            
            setBounds(0,0,800,600);
            panel.add(this);
            
            setIgnoreRepaint(true);
            
            container.pack();
            container.setResizable(false);
            container.setVisible(true); 
            requestFocus();
            
            createBufferStrategy(2);
            BufferStrategy strategy = getBufferStrategy();
      
            while (gameRunning) {
                  
                  Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
                  g.setColor(Color.black);
                  g.fillRect(0,0,800,600);
            
                  g.dispose();
                  strategy.show();
                  
                  try { Thread.sleep(10); } catch (Exception e) {}
            }
      }

      public static void main(String args[]) {
            new Game();
      }
      
}

I mean, this should work, right?

I’m running Mac OS X 10.3.8 with Java 1.42 and an nVidia GeForce FX 5200 video card.

Definitely something strange going on. I copied the code exactly and it works fine for me.

I may have overlooked something (a speciality of mine), but it seems ok to me.

Could be an Apple-specific problem. It compiles and runs fine on my system (Windows XP, Java 1.4.2).

Well, good and bad news.

Bad: The problem definitely seems to be a problem with Apple’s implementation of BufferStrategy.

Good: It seems to work fine when I use the BufferStrategy with a Frame instead of a subclass of Canvas.

Not an ideal fix to the problem, but I guess it will have to do.

Edit: Crap. That means I can’t use accelerated graphics, doesn’t it?
Crap.

what? bufferstrategy from a frame can’t be accelerated? :o

This has nothing to do with hardware acceleration - which is not implemented AT ALL on the Mac for Java 1.4.2.

It seems to be a quirk in the Apple implementation of BufferStrategies… perhaps a bug should be filed with Apple.

Thankfully, according to rumors, we are not far away from the release of Mac OS X 10.4, and with it Java 5 for the Mac, with faster graphics and nice stuff like that.