2013-10-16 48 views
0

我一直在四處搜尋這個問題的答案,但幾乎沒有提供有關如何解決問題的信息。我期望做的是能夠使用Graphics2D在窗口內完成我需要的所有圖形。我對Graphics2D和BufferStrategy的使用並不是很寬容,因爲我有大量的現有代碼使用這些來使用計算機GraphicsDevice製作全屏窗口。這是我做的測試,但有些東西我缺少。在窗口中使用帶有BufferStrategy的Graphics2D

public static void main(String[] args) { 
    //Creates a frame and sets properties 
    JFrame frame = new JFrame("FrameDemo"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(500, 500); 
    frame.setResizable(true); 
    frame.setVisible(true); 
    frame.createBufferStrategy(2); 

    //Gets Graphics2D from the bufferstrategy 
    BufferStrategy s = frame.getBufferStrategy(); 
    Graphics2D g = (Graphics2D)s.getDrawGraphics(); 

    //Draws a background and a line for testing 
    g.setColor(Color.GRAY); 
    g.drawRect(0, 0, 500, 500); 
    g.setColor(Color.BLACK); 
    g.drawLine(50, 50, 200, 50); 

    //Displays the graphics to the frame 
    frame.update(g); 
    g.dispose(); 
    s.show(); 
} 

當運行此僅創建一個被設置爲正確的大小的空幀,併產生沒有錯誤,但線和背景不顯示。

我的猜測是問題源於幀更新的最後三行代碼。我的困惑是如何在使用BufferStategy時顯示Graphics2D組件......您是否仍然需要更新框架,或者只需要顯示BufferStategy?任何幫助將不勝感激,並提前感謝您。

+1

在http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferStrategy.html –

回答

0

使用http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferStrategy.html上的示例,我編寫了這個框架示例。

public class BufferedStrategyTest extends JFrame implements Runnable, WindowListener { 

    private Thread graphicsThread; 
    private boolean running = false; 
    private BufferStrategy strategy; 

    public BufferedStrategyTest() { 
     super("FrameDemo"); 
     addWindowListener(this); 
     setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
     setSize(500, 500); 
     setResizable(true); 
     setVisible(true); 

     createBufferStrategy(2); 
     strategy = getBufferStrategy(); 

     running = true; 
     graphicsThread = new Thread(this); 
     graphicsThread.start(); 
    } 

    public static void main(String[] args) { 
     new BufferedStrategyTest(); 
    } 

    public void addNotify() { 
     super.addNotify(); 
     createBufferStrategy(2); 
     strategy = getBufferStrategy(); 
    } 

    @Override 
    public void run() { 
      // Main loop 
     while (running) { 
      // 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 
        // to make sure the strategy is validated 
        Graphics g = strategy.getDrawGraphics(); 

        g.setColor(Color.GRAY); 
        g.drawRect(0, 0, 500, 500); 
        g.setColor(Color.BLACK); 
        g.drawLine(50, 50, 200, 50); 

        // Dispose the graphics 
        g.dispose(); 

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

       // Display the buffer 
       strategy.show(); 

       // Repeat the rendering if the drawing buffer was lost 
      } while (running && strategy.contentsLost()); 
     } 
     setVisible(false); 
     dispose(); 
    } 

    @Override 
    public void windowActivated(WindowEvent e) {} 
    @Override 
    public void windowClosed(WindowEvent e) {} 
    @Override 
    public void windowClosing(WindowEvent e) { 
     running = false; 
    } 
    @Override 
    public void windowDeactivated(WindowEvent e) {} 
    @Override 
    public void windowDeiconified(WindowEvent e) {} 
    @Override 
    public void windowIconified(WindowEvent e) {} 
    @Override 
    public void windowOpened(WindowEvent e) {} 
} 
+0

什麼是用於重寫信息addNotify()方法的推理很好的例子?謝謝你,在你的例子之後,更深入地閱讀緩衝區策略,我開始更好地理解正在發生的事情。 – user2887000

+0

@ user2887000當我調整窗口大小時,出現「java.lang.IllegalStateException:緩衝區尚未創建」的錯誤,所以我查看了它,並且有人說要使用那一段代碼。我的猜測:窗口大小更改緩衝的圖像用於雙緩衝區獲取無效,需要重新創建。如果你願意,你可以更深入地觀察它。 –