我正在学习将缓冲区策略与画布一起使用,我编写了这个类,然后将其添加到另一个类的 JFrame 中。我正在尝试绘制一个矩形,但画布是空的。控制台说
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)
这是代码。我已经调试了它,它进入了我所做的每一个方法。所以基本上现在我不知道为什么它没有显示我的矩形。
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();
}
}