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.