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:
1 2 3 4 5 6 7 8 9 10 11
| 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| 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.