1

为什么这不渲染底部和右侧。我如何解决它?有一个小的白色区域,没有任何东西被绘制?我确信有一个简单的解决方法,但我太愚蠢了。

我已经摆脱了任何不必要的东西,但可能仍然有一些随机无用的代码。

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

这是输出

请。我知道这里几乎每个人都知道如何解决这个问题,所以请有人可以帮助我。

4

1 回答 1

0

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

于 2013-03-01T22:22:14.843 回答