I'm learning to use bufferstrategy with canvas, I coded this class which is then added to a JFrame in another class. I'm trying to draw a rectangle, but the canvas is empty. The console says
at java.awt.Component$FlipBufferStrategy.createBuffers(Unknown Source)
at java.awt.Component$FlipBufferStrategy.<init>(Unknown Source)
at java.awt.Component$FlipSubRegionBufferStrategy.<init>(Unknown Source)
at java.awt.Component.createBufferStrategy(Unknown Source)
at java.awt.Canvas.createBufferStrategy(Unknown Source)
at java.awt.Component.createBufferStrategy(Unknown Source)
at java.awt.Canvas.createBufferStrategy(Unknown Source)
at myPanel.draw(Pannello.java:72)
at myPanel.run(Pannello.java:59)
at java.lang.Thread.run(Unknown Source)
And here's the code. I've debugged it and it goes into every method that I've made. So basically now I don't know why it's not showing my rectangle.
public class myPanel extends Canvas implements Runnable {
//FIELDS
private static final long serialVersionUID = 1L;
public static int WIDTH = 1024;
public static int HEIGHT = WIDTH / 16 * 9;
private boolean running;
private Thread t1;
public synchronized void start (){
running = true;
t1 = new Thread (this);
t1.start(); // calls run()
}
//INIT
public myPanel(){
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
requestFocus();
}
//Main runnable
public void run(){
while (running){
update();
draw();
}
}
public void update(){
}
public void draw(){
BufferStrategy bs = getBufferStrategy();
if (bs== null){
createBufferStrategy(3);
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillOval(0, 0, 20, 20);
g.dispose();
bs.show();
}
}