gpt4 book ai didi

Java Buffer策略学习

转载 作者:行者123 更新时间:2023-11-29 03:31:47 24 4
gpt4 key购买 nike

我正在学习将 bufferstrategy 与 canvas 一起使用,我编写了这个类,然后将其添加到另一个类中的 JFrame。我正在尝试绘制一个矩形,但 Canvas 是空的。控制台显示

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();
}

最佳答案

这是我的初步回答,这应该可以解决您的问题。我将在下一次编辑中解释您的错误:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

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 JFrame frame;
private boolean running;
private Thread t1;


public myPanel(){
Dimension size = new Dimension(WIDTH, HEIGHT);

setPreferredSize(size);

frame = new JFrame();
}


public synchronized void start (){
running = true;
t1 = new Thread (this);
t1.start(); // calls run()
}


//Main runnable

public void run(){
while (running){
update();
draw();
}
}


public void update(){

}


public void draw(){
BufferStrategy bs = getBufferStrategy();
if (bs== null){
createBufferStrategy(3);
return;
}

Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillOval(0, 0, 20, 20);
g.dispose();
bs.show();
}

public static void main(String[] args){
myPanel myPanel = new myPanel();

myPanel.frame.setResizable(false);
myPanel.frame.setTitle("My frame");
myPanel.frame.add(myPanel);
myPanel.frame.pack();
myPanel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myPanel.frame.setLocationRelativeTo(null);
myPanel.frame.setVisible(true);

myPanel.start();
}
}

好的,首先,Java 中的惯例是将类名大写。我没有在这里做,但我希望你能记住。

其次,您的代码中缺少一些东西。您没有制作 JFrame,也没有 main 方法,我相信您知道每个 Java 程序都需要知道从哪里开始。您还需要一个构造函数来初始化 JFrame 并设置框架的大小。

第三,创建缓冲策略后需要return;

希望对您有所帮助。

编辑:

不是将 new myPanel() 添加到您的框架,而是添加 gameframe.add(游戏);

关于Java Buffer策略学习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17770830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com