每當我嘗試渲染圖形到我的JFrame時,它都不想顯示在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;
}
}
您粘貼的代碼不會生成。請提供一個完整的示例。 –
圖形應呈現給JPanel,而不是JFrame。 –