2013-03-01 70 views
1

爲什麼這不會渲染底部和右側。我如何解決它? 有一個小的白色區域,沒有任何東西被繪製?我確信有一個簡單的解決方法,但我太笨了。Java圖形不在底部和右側繪製

我已經擺脫了沒有必要的任何事情,但可能仍然有一些隨機無用的代碼。

package test; 

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Test extends JPanel implements Runnable 
{ 
    /** 
    * Default serial version 
    */ 
    private static final long serialVersionUID = 1L; 


    private static final int WIN_WIDTH = 760; 
    private static final int WIN_HEIGHT = 480; 

    private boolean running; 
    private Thread thread; 
    public Graphics g; 
    public Graphics2D g2d; 
    static final int UPDATE_RATE = 60; 
    static final long UPDATE_PERIOD = 1000000000L/UPDATE_RATE; 

    static public final int EONWIDTH = 24; 
    static public final int EONHEIGHT = 24; 

    /** 
    * Contains a list of Eon instances by their instance ID. 
    */ 

    ///////////////////////////////////CONSTRUCTER///////////////////////////// 
    public Test() 
    { 
     Dimension size = new Dimension(WIN_WIDTH, WIN_HEIGHT); 
     setPreferredSize(size); 
     setMinimumSize(size); 
     setMaximumSize(size);  
    } 

    /** 
    * Main java runnable, required 
    * @param args Unused, required by default 
    */ 
    public static void main(String[] args) 
    { 
     Test game = new Test(); 

     JFrame frame = new JFrame("See guys, It's off centered... - 00.00.10"); 
     JPanel panel = new JPanel(new BorderLayout()); 
     panel.add(game, BorderLayout.CENTER); 
     frame.setContentPane(panel); 
     frame.pack(); 
     frame.isFocusable(); 
     frame.setFocusable(true); 
     frame.setResizable(false); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 

     game.g = game.getGraphics(); 

     game.start(game); 
    } 


    private void loopUpdate(Test game) 
    { 
      game.repaint(); 
    } 

    boolean done = true; 
    public void paint(Graphics g) 
    { 
     System.out.print(g.getClipBounds().width + ", " + g.getClipBounds().height + "\n"); 
     super.paint(g); 
     g.setClip(0,0, WIN_WIDTH, WIN_HEIGHT); 
     final Graphics2D g2d = (Graphics2D) g; 
     if (done) 
     { 
      done=false; 

      try 
      { 

     g2d.fillRect(0, 0, WIN_WIDTH + 100, WIN_HEIGHT + 100); 


     } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      g.dispose(); 
      done = true; 
    } 
    } 

    @Override 
    public void run() 
    { 
     loopUpdate(this); 
    } 
} 

This is the output

請。我知道幾乎每個人都知道如何解決這個問題,所以請有人幫助我。

+2

請修改您的問題以包含展示您描述的問題的[sscce](http://sscce.org/)。 – trashgod 2013-03-01 17:53:59

+0

@trashgod對不起,你會做 – 2013-03-01 18:35:24

+0

你不必大喊; +1(近)[sscce](http://sscce.org/)。 – trashgod 2013-03-02 01:14:03

回答

0

你假設JPanel的大小正好是WIN_WIDTH和WIN_HEIGHT的大小。使用getBounds()而不是常量來獲得JPanel的實際實現大小。實際尺寸取決於容器的LayoutManager的自由裁量權,所以如果您絕對需要您指定的確切尺寸,您可以調查不同的佈局管理器或絕對定位。

相關問題