Java图形不在底部和右侧绘制

Java图形不在底部和右侧绘制

问题描述:

为什么这不会渲染底部和右侧。我如何解决它? 有一个小的白色区域,没有任何东西被绘制?我确信有一个简单的解决方法,但我太笨了。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

你假设JPanel的大小正好是WIN_WIDTH和WIN_HEIGHT的大小。使用getBounds()而不是常量来获得JPanel的实际实现大小。实际尺寸取决于容器的LayoutManager的自由裁量权,所以如果您绝对需要您指定的确切尺寸,您可以调查不同的布局管理器或绝对定位。