每当我尝试将图形渲染到我的JFrame时,它都不希望显示在JFrame上。
这是渲染方法:
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
g.setColor(Color.RED);
g.fillRect(0, 0, 1000, 600);
g.dispose();
bs.show();
}
以下是全班:
package FrameWork;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
public class Battle extends Canvas implements Runnable{
boolean running = false;
Thread thread;
Window gameWindow;
Handler handler= new Handler();
public Graphics g;
private GameObject p, e;
private BattleWindow BattleWindow;
public Battle(GameObject Player, GameObject Enemy){
p = Player;
e = Enemy;
BattleWindow = new BattleWindow(1000, 600, "Battle", p, e, this);
}
public void init(){
handler.addObject(e);
handler.object.add(p);
this.addKeyListener(new KeyInputBattle(handler));
}
public void createLevel(){
}
public synchronized void start(){
if(running)
return;
running = true;
createLevel();
thread = new Thread(this);
thread.start();
}
@Override
public void run() {
init();
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1){
tick();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println("BATTLE - FPS: " + frames + " TICKS: " + updates);
frames = 0;
updates = 0;
}
}
}
public void tick(){
handler.tick();
}
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
g.setColor(Color.RED);
g.fillRect(0, 0, 1000, 600);
g.dispose();
bs.show();
}
public void dispose(){
running = false;
}
}
答案 0 :(得分:0)
您的代码包含许多错误,并不会感到惊讶它不起作用。这是什么?
BattleWindow = new BattleWindow(1000, 600, "Battle", p, e, this);
在创建类的实例时,必须为其命名:
BattleWindow bw = new BattleWindow(1000, 600, "Battle", p, e, this);
尝试查看java的基本语法和规则,并尝试重新编写代码。