2017-09-04 9 views
0

私はJFrameを作成し、それに矩形を描画している必要があります。 それは動作しません、時にはそれは完全に黒く、時には完全に白です、ここに私の方法です。このコードは四角形を表示しませんが、それは

したがって、最初にバッファを作成し、フレームレートも無視するため、renderメソッドは2回呼び出されます。これは現在のところ非同期です。

Edit1:私は1つの問題を解決しました: 今は矩形を描いていますが、時には白い画面が表示されるだけです。私はまだその問題を解決する必要があります

Edit2:私は解決策を探しているだけでなく、私の問題が発生している理由も探しているので、コードを盲目的に書くのではありません。

public MyFrame(String title,int width,int height) 
    { 
     super(title); 
     this.setSize(width,height); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setLocationRelativeTo(null); 
     this.setUndecorated(true); 
     this.addKeyListener(new KeyInput()); 
     this.setVisible(true); 
    } 
    public void draw(Graphics2D g,int arg) 
    { 
     g.setColor(new Color(0,0,0,255)); 
     g.fillRect(0,0,SIZE,SIZE); 
     g.setColor(new Color(255,255,255)); 
     g.fillRect(0,0,50,50); 
    } 
    public void render(int arg) 
    { 
     BufferStrategy bs=this.getBufferStrategy(); 
     if(null==bs) 
     { 
      this.createBufferStrategy(3); 
     } 
     else 
     { 
      Graphics2D g=(Graphics2D)bs.getDrawGraphics(); 
      g.setColor(new Color(0,0,0,255)); 
      g.fillRect(0,0,this.getWidth(),this.getHeight()); 
      BufferedImage canvas=new BufferedImage(SIZE,SIZE,2); 
      int s=Math.min(this.getWidth(),this.getHeight()); 
      g.drawImage(canvas,(this.getWidth()-s)/2,(this.getHeight()-s)/2,s,s,this); 
      Graphics2D g2=canvas.createGraphics(); 
      this.draw(g2,arg); 
      bs.show(); 
      g.dispose(); 
      g2.dispose(); 
     } 
    } 
    public static void main(String[] args) 
    { 
     Dimension d=Toolkit.getDefaultToolkit().getScreenSize(); 
     FRAME=new MyFrame("Insert name here!",d.width,d.height,1); 
     FRAME.render(0); 
     FRAME.render(0); 
    } 

編集:これはもはやneessaryですが、私はとにかくあなたの助けをありがとう、問題を解決するために管理していません。

+2

1)* "INSERT_FRAME_NAME_HERE" *そうでなければなりません。一般的なJava命名法(名前付け規則、例えば 'EachWordUpperCaseClass'、' firstWordLowerCaseMethod() '、' firstWordLowerCaseAttribute'が 'UPPER_CASE_CONSTANT'でない限り)を学び、一貫して使用してください。サンプルコードでさえ。 2)すぐに助けを得るために、[MCVE]または[短く、自己完結型の正しい例](http://www.sscce.org/)を投稿してください。 –

+0

さて、私はそれを変更します。 –

+2

[レッスン:カスタム・ペイントの実行](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html):スイング・グラフィックスへの入門チュートリアル –

答えて

1

必要なすべての情報は、tutorialホバークラフトに記載されています。
次のコードは、フルスクリーンサイズのJFrameの黒い四角形を描画しています。詳細情報はmcveです。

import java.awt.Color; //mcve should include imports 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Toolkit; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class MyFrame extends JFrame{ 

    public MyFrame() 
    { 
     super(); 
     //this.setSize(width,height); use : 
     setExtendedState(JFrame.MAXIMIZED_BOTH); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //comment 1 : see http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html 
     //as Hovercraft suggested 
     JPanel customJPanel = new MyPanel(); 
     add(customJPanel); 

     // not needed to demonstrate the issue. Remove to make mcve 
     /* 
     this.setLocationRelativeTo(null); 
     this.setUndecorated(true); also removes frame title 
     this.addKeyListener(new KeyInput()); 
     */ 
     pack(); 
     setVisible(true); 
    } 

    //comment 1 
    class MyPanel extends JPanel { 

     public MyPanel() { super(); } 

     @Override 
     public void paintComponent(Graphics g) { 

      super.paintComponent(g); 

      int x = getRectangleXpos();   //if postion and/or size 
      int y = getRectangleYPos();   //do not change, no need to 
      int width = getRectangleWidth(); //recalculate : move from this method 
      int height = getRectangleHeight(); 
      g.drawRect(x, y , width,height); 
      g.setColor(Color.BLACK); 
      g.fillRect(x, y , width,height); 
     } 

     private int getRectangleXpos() { 
      //apply calculation logic if needed 
      return 200; 
     } 

     private int getRectangleYPos() { 
      //apply calculation logic if needed 
      return 300; 
     } 

     private int getRectangleWidth() { 
      //apply calculation logic if needed 
      return 500; 
     } 

     private int getRectangleHeight() { 
      //apply calculation logic if needed 
      return 400; 
     } 
    } 

    public static void main(String[] args) 
    { 
     //compilation error: The constructor MyFrame(String, int, int, int) is undefined 
     //new MyFrame("Insert name here!",d.width,d.height,1); 

     //comment 1 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new MyFrame(); 
      } 
     }); 
    } 
} 
+0

ありがとう、私は間違いなくいつかJPanelの方法を試してみます。私はまだそれがJFrameでなぜ起こるのかを知る必要があります。 –

関連する問題