2016-07-06 118 views
0
package local.ryan.grid; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

import javax.swing.JFrame; 

public class Game implements Runnable, KeyListener { 

    public boolean isRunning = false; 

    private long lastTime; 
    private double fps; 
    public int gamestage = 0; 

    public int width = 300, height = 300; 
    public String title = "Le Go!"; 

    public JFrame frame = new Frame(2, width, height, title); 

    public int x = 0, y = 0, velX = 0, velY = 0; 

    public static void main(String[] args) throws InterruptedException { 

     Thread game = new Thread(new Game()); 
     game.start(); 

    } 

    public void run() { 

     frame.addKeyListener(this); 
     frame.setFocusable(true); 
     frame.setFocusTraversalKeysEnabled(false); 
     start(); 

     while(isRunning) { 

      lastTime = System.nanoTime(); 

      try { 

       // Limit Each Frame, to 60 fps. 
       // Prevents performance issues with multiple objects. 

      Thread.sleep(1000/60); // 1000 miliseconds (1 second)/60 frames-per-second ~ 17 ms. 



      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

      render(frame.getGraphics()); 

      fps = 1000000000D/(System.nanoTime() - lastTime); //one second(nano) divided by amount of time it takes for one frame to finish 
      lastTime = System.nanoTime(); 

      // Change to integer, to remove decimals. 
      System.out.println("FPS: " + (int) fps + "."); 

     } 

    } 

    public void render(Graphics c) { 

     c.clearRect(0, 0, width, height); 
     Graphics2D g = (Graphics2D)c; 

     g.setColor(Color.RED); 
     g.fillRect(x, y, 30, 60); 

     g.dispose(); 

    } 

    public void start() { 

     if(isRunning) 
      return; 

     isRunning = true; 


    } 

    public void stop() { 

     if(!isRunning) 
      return; 

     isRunning = false; 


    } 

    @Override 
    public void keyPressed(KeyEvent e) { 

     if(e.getKeyCode() == KeyEvent.VK_D) { 

      velX += 1; 
      actionPerformed(); 
      try { 
       Thread.sleep(50); 
      } catch (InterruptedException e1) { 
       e1.printStackTrace(); 
      } 

     } 


    } 

    @Override 
    public void keyReleased(KeyEvent e) { 
    } 

    @Override 
    public void keyTyped(KeyEvent e) { 
    } 

    public void actionPerformed() { 

     x = x + velX; 
     y = y + velY; 
     render(frame.getGraphics()); 

    } 

} 

我該如何防止渲染中的閃爍,它一切正常,除了煩人時閃爍。我想我可以添加一個if語句來檢查VelX或VelY是否等於0,但是當我移動廣場時它會閃爍!請收拾我。如何防止Java 2D圖形閃爍?

+0

考慮繪製一個輕量級組件,覆蓋它的'paintComponent'方法。請參見[this](http://www.oracle.com/technetwork/java/painting-140037.html)和[this](https://docs.oracle.com/javase/tutorial/uiswing/painting/) – copeg

回答

0

這可能有一些原因,但看看你的代碼現在的原因是因爲它沒有雙緩衝。當您的圖形是雙緩衝區時,計算機將繪製到虛擬屏幕,然後用顯示的屏幕切換該屏幕,防止閃爍。

像這樣的實現了雙緩衝:

private void render(Graphics g) 
{ 
    BufferedImage bufferedImage = new BufferedImage(500, 500, BufferedImage.TYPE_ARGB); 
    Graphics2D g2d = bufferedImage.createGraphics(); 
    //paint using g2d ... 

    Graphics2D g2dComponent = (Graphics2D) g; 
    g2dComponent.drawImage(bufferedImage, null, 0, 0); 

}

欲瞭解更多信息,我相信Java文檔有一些很好的教程。

+0

當我將方法更改爲代碼時,屏幕上沒有顯示任何內容。 – Ryan

+0

如果您決定使用JFrame,如[本教程](http://www.java2s.com/Tutorial/Java/0261__2D-Graphics/Doublebuffer.htm)。我個人推薦切換到使用Canvas進行主動渲染。 [This](http://jamesgames.org/resources/double_buffer/double_buffering_and_active_rendering.html)就是一個很好的例子。 – Calvin