2017-11-19 6 views
1

私はキャンバスでJFrameを作成しました。そのキャンバスに描画したいと思います。後日キャンバスが1秒間に何度も更新されるので、私はこのためのバッファー戦略を使用しています。ここでは、コードは次のとおりです。バッファストラテジで描画するグラフィックスオブジェクトを取得する

package mainPackage; 

import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.image.BufferStrategy; 

import javax.swing.JFrame; 

public class TickPainter { 
    //just some presets for a window. 
    public static JFrame makeWindow(String title, int width, int height) { 
     JFrame mainWindow = new JFrame(); 
     mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     mainWindow.setSize(width, height); 
     mainWindow.setVisible(true); 
     mainWindow.setLocationRelativeTo(null); 
     mainWindow.setTitle(title); 

     return mainWindow;  
    } 

    public static void main(String[] args) { 

     JFrame mainWindow = makeWindow("Practice", 800, 600); 
     Canvas mainCanvas = new Canvas(); 
     mainWindow.add(mainCanvas); 
     mainCanvas.setSize(mainWindow.getWidth(), mainWindow.getHeight()); 
     mainCanvas.setBackground(Color.white); 
     mainCanvas.createBufferStrategy(3); 
     BufferStrategy bufferStrat = mainCanvas.getBufferStrategy(); 

     Graphics g = bufferStrat.getDrawGraphics(); 
     g.setColor(Color.black); 
     g.fillRect(250, 250, 250, 250); 
     g.dispose(); 
     bufferStrat.show(); 
    } 
} 

意図したとおりにプログラムが黒の四角形を描画していない、私はここで本当に何かを明らかに見逃しているような気がしますし、私はそれを見ることはできません。現時点では、このプログラムは白いキャンバスを空白にしています。問題の一部のように思えるのは、バッファが、私が見るよりも速く四角形のフレームを渡しているだけですが、それ以降はロードするフレームがないため、なぜこれを行うのか分かりません。

+0

まず、[JavaDocs](https://docs.oracle.com/javase/8/docs/api/java/awt/image/BufferStrategy.html)と[チュートリアル](https:// docs。 oracle.com/javase/tutorial/extra/fullscreen/bufferstrategy.html)、これらは、あなたが 'BufferStrategy'をどのように使用しているか、 – MadProgrammer

答えて

1

BufferStrategyには、レンダリングする前に満たす必要があるいくつかの初期要件があります。また、どのように動作するのかという性質から、実際にハードウェア層が受け入れる前に、塗装段階を何度も繰り返す必要があるかもしれません。

私はJavaDocstutorialを経由をお勧めします、彼らはあなたがBufferStrategy

次の例を使用すると仮定しているどのように貴重な例を提供は、基本コンポーネントとしてCanvasを使用し、カスタムの中にレンダリングループを設定しますThread。それは非常に基本的ですが、あなたが実装する必要があると思い、基本的な概念を示しています...

import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.image.BufferStrategy; 
import java.util.concurrent.atomic.AtomicBoolean; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

public class Test { 

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

    public Test() { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       TestCanvas canvas = new TestCanvas(); 

       JFrame frame = new JFrame(); 
       frame.add(canvas); 
       frame.setTitle("Test"); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 

       canvas.start(); 
      } 
     }); 
    } 

    public class TestCanvas extends Canvas { 

     private Thread thread; 
     private AtomicBoolean keepRendering = new AtomicBoolean(true); 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     public void stop() { 
      if (thread != null) { 
       keepRendering.set(false); 
       try { 
        thread.join(); 
       } catch (InterruptedException ex) { 
        ex.printStackTrace(); 
       } 
      } 
     } 

     public void start() { 
      if (thread != null) { 
       stop(); 
      } 

      keepRendering.set(true); 
      thread = new Thread(new Runnable() { 
       @Override 
       public void run() { 

        createBufferStrategy(3); 

        do { 
         BufferStrategy bs = getBufferStrategy(); 
         while (bs == null) { 
          System.out.println("get buffer"); 
          bs = getBufferStrategy(); 
         } 
         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 
           System.out.println("draw"); 
           Graphics graphics = bs.getDrawGraphics(); 

           // Render to graphics 
           // ... 
           graphics.setColor(Color.RED); 
           graphics.fillRect(0, 0, 100, 100); 
           // Dispose the graphics 
           graphics.dispose(); 

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

          System.out.println("show"); 
          // Display the buffer 
          bs.show(); 

          // Repeat the rendering if the drawing buffer was lost 
         } while (bs.contentsLost()); 
         System.out.println("done"); 
         try { 
          Thread.sleep(100); 
         } catch (InterruptedException ex) { 
          ex.printStackTrace(); 
         } 
        } while (keepRendering.get()); 
       } 
      }); 
      thread.start(); 
     } 

    } 

} 

は、BufferStrategyのポイントを覚えて、あなたに塗装工程を完全に制御を与えることですので、それは通常の塗装工程の外に動作しますAWTとSwingで一般的に実装されています

"遅くともキャンバスが何度も更新されるので、私はこのためのバッファー戦略を使用しています" - "直接ハードウェア"ソリューションを下る前に、 dスイングTimerと通常の塗装プロセスを使用してどのようにうまく動作するかを検討してください。

関連する問題