public class Game extends JFrame
{
public Game()
{
this.setUndecorated(true);
initComponents();
this.setSize(500,500);
this.setVisible(true);
GameCanvas c = new GameCanvas();
this.add(c,BorderLayout.CENTER);
}
public class GameCanvas extends Canvas
{
boolean repaintInProgress = false;
public GameCanvas()
{
setFocusable(true);
this.setBackground(Color.BLACK);
this.setSize(500.500);
setIgnoreRepaint(true);
UpdateTimer update = new UpdateTimer(this);
new Timer(16, update).start();
}
public void myRepaint()
{
if(repaintInProgress)
return;
repaintInProgress = true;
Dimension size = getSize();
BufferStrategy strategy = getBufferStrategy();
Graphics graphics = strategy.getDrawGraphics();
graphics.fillRect(0, 0, size.width, size.height);
//draw stuff
repaintInProgress = false;
}
public class UpdateTimer implements ActionListener
{
GameCanvas gc;
UpdateTimer(GameCanvas gc)
{
this.gc = gc;
}
public void actionPerformed(ActionEvent e)
{
gc.myRepaint();
}
}
}
}
The code will get to the line:
Graphics graphics = strategy.getDrawGraphics();
before erroring out. Now from what i understand about java graphics, the only reason this should happen is because the canvas is not shown before the line Graphics graphics = strategy.getDrawGraphics(); is called. BUT i have the JFrame show before the canvas is even created and added to the JFrame?! I'm a bit confused as well beacause i have made a similar game skeleton and it works fully, but this one will not?! Any Help is greatly appreciated and thanks in advance for any help! Lookin forward to an explination on this one!

New Topic/Question
Reply




MultiQuote






|