2017-11-18 57 views
0

遵循2015年11月發佈的指南,我已經逐字複製了他的代碼,但它仍然不適用於我。有東西被棄用?如何在java中使用BufferStrategy時避免黑線

我有3個緩衝區(稱它們1,2和3)。當2和3畫在屏幕上時,屏幕的頂部和左側會有黑線。這個相同的代碼適用於兩個緩衝區。

錯誤鏡頭:https://gfycat.com/gifs/detail/GraveCompetentArmyworm

package field; 

import javax.swing.JFrame; 

import java.awt.*; 
import java.awt.image.BufferStrategy; 

public class Main extends JFrame{ 

    private Canvas canvas=new Canvas(); 

    public Main() { 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     setBounds(0,0,1000,1000); 

     setLocationRelativeTo(null); 

     add(canvas); 
     setVisible(true); 

     canvas.createBufferStrategy(3); 
     BufferStrategy buffert = canvas.getBufferStrategy(); 

     int p=0; 
     int ap=0; 
     while(p<1000) { 
      if (ap==100){ 
       p++; 
       ap=0; 
      } 
      ap++; 
      buffert=canvas.getBufferStrategy(); 
      Graphics g = buffert.getDrawGraphics(); 
      super.paint(g); 
      g.setColor(Color.GREEN); 

      g.fillOval(p+100, 200, 50, 50); 

      buffert.show(); 
     } 
    } 

// public void paint(Graphics graphics) { 
//  super.paint(graphics); 
//  graphics.setColor(Color.RED); 
//  graphics.fillOval(100, 100, 100, 100); 
//  
// } 



    public static void main(String[] args){ 


     new Main(); 




    } 

} 
+1

'super.paint(g);'將是第一個錯誤。你應該自己清除緩衝區 – MadProgrammer

+0

讓我試着澄清一下。畫布是框架的一個孩子,它的位置被框架邊框所抵消,通過調用super.paint,您要求框架將自己繪製到圖形上,黑色條實際上是通常由窗口裝飾覆蓋的區域 – MadProgrammer

+0

太棒了!我將super.paint(g)更改爲canvas.paint(g),它現在按預期工作。 –

回答

1

你需要去閱讀the JavaDocs for BufferStrategyFull-Screen Exclusive Mode API,這對BufferStrategy

一個BufferStrategy一些重要的教程和例子是執行「翻頁」的手段,這與常規噴漆系統無關。這爲您提供對繪畫過程的「主動」控制。每個緩衝區在屏幕上都會更新,並在準備就緒時推送到屏幕上。

這通常不涉及組件自己的繪畫系統,其目的是避免它。

這意味着您不應該在JFramecanvas.paint上致電super.paint(g)。實際上,一般來說,您絕不應手動撥打paint

每次你想更新一個緩衝區時,你都需要「準備」它。這通常意味着有一些基本的顏色填充

因此,基於從JavaDoc中的例子,你可以這樣做......

// Check the capabilities of the GraphicsConfiguration 
... 

// Create our component 
Window w = new Window(gc); 

// Show our window 
w.setVisible(true); 

// Create a general double-buffering strategy 
w.createBufferStrategy(2); 
BufferStrategy strategy = w.getBufferStrategy(); 

// Main loop 
while (!done) { 
    // Prepare for rendering the next frame 
    // ... 

    // Render single frame 
    do { 
     // The following loop ensures that the contents of the drawing buffer 
     // are consistent in case the underlying surface was recreated 
     do { 
      // Get a new graphics context every time through the loop 

      // Determine the current width and height of the 
      // output 
      int width = ...; 
      int height = ...l 
      // to make sure the strategy is validated 
      Graphics graphics = strategy.getDrawGraphics(); 
      graphics.setColor(Color.WHITE); 
      graphics.fillRect(0, 0, width, height);  
      // Render to graphics 
      // ... 

      // Dispose the graphics 
      graphics.dispose(); 

      // Repeat the rendering if the drawing buffer contents 
      // were restored 
     } while (strategy.contentsRestored()); 

     // Display the buffer 
     strategy.show(); 

     // Repeat the rendering if the drawing buffer was lost 
    } while (strategy.contentsLost()); 
} 

// Dispose the window 
w.setVisible(false); 
w.dispose(); 

現在,就個人而言,我寧願使用Canvas作爲因爲它提供了更多可重用的解決方案,並且更容易確定尺寸從

相關問題