2017-09-04 98 views
0

我正在製作一個JFrame並在其上繪製一個矩形。 它不起作用,有時它是完全黑色的,有時是完全白色的,這裏是我的方法。此代碼不會顯示矩形,但它應該

所以渲染方法被調用兩次,因爲它第一次創建緩衝區,也忽略幀率,它現在是不重要的。

EDIT1:我解決了一個問題: 它繪製一個矩形了,但有時它只是顯示一個白色的屏幕。我仍然需要解決這個問題

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); 
    } 

編輯:這已不再是問題,我設法解決了這個問題,無論如何感謝您的幫助。

+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):Swing圖形入門教程 –

回答

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中。 –