2016-12-10 50 views
0

每当我尝试渲染图形到我的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; 
    } 

} 
+0

您粘贴的代码不会生成。请提供一个完整的示例。 –

+0

图形应呈现给JPanel,而不是JFrame。 –

回答

0

你的代码中包含很多错误,并不感到惊讶它不工作。这是什么?

BattleWindow = new BattleWindow(1000, 600, "Battle", p, e, this); 

当你创建一个类的实例,你必须给它一个名字:

BattleWindow bw = new BattleWindow(1000, 600, "Battle", p, e, this); 

尝试看看基本语法和java的规则,并试图重申你的代码。

+0

我知道我只是使用了错误的命名约定。我有一个对象战斗窗口,并命名我的变量BattleWindow。但更改名称并不能解决问题。 – benbylife

+0

不,它不会',但你应该再看看你的代码并清理它。 – Erninger

+0

好吧,我仍然需要一个解决方案... – benbylife