框架和帆布增长大于规定 [英] Frame and Canvas grow larger than specified

查看:235
本文介绍了框架和帆布增长大于规定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我收到一个超大的窗户,这让我跑箍适合我的精灵在游戏窗口。构造函数应该让这样所有的子组件安装到海誓山盟,但它好像有在画布或框架一些额外的填充。我有一个艰难的时间搞清楚的罪魁祸首是什么。我帧的大小应不大于800×600大(不包括OS装饰,谈论容器)。

I have no idea why I am getting an extra-large window, this is making me run hoops to fit my sprites in the game-window. The constructor should make it so all the subcomponents fit into eachother, but it seems like there is some extra padding in the canvas or frame. I am having a tough time figuring out what the culprit is. The size of my frame should NOT be larger than 800x600 (OS decoration not included, talking about the containers).

Panel: java.awt.Rectangle[x=0,y=0,width=810,height=610]
Frame: java.awt.Rectangle[x=0,y=0,width=816,height=638]
Canvas:java.awt.Rectangle[x=0,y=0,width=816,height=638]

正如你所看到的范围比我大specifed

As you can see the bounds are larger than I specifed.

类:

public class GameFrame {
private JFrame frame;
private JPanel panel;
private Canvas canvas;
private BufferStrategy bufferStrategy;
private Graphics2D g;
private int width;
private int height;

public GameFrame(Color bgColor) {
    this.width = GameEngine.GAMEDIMENSION[0]; // 800
    this.height = GameEngine.GAMEDIMENSION[1]; // 600
    this.frame = new JFrame();
    this.panel = (JPanel) frame.getContentPane();
    this.panel.setPreferredSize(new Dimension(width, height));
    this.panel.setLayout(null);
    this.panel.setBackground(bgColor);
    this.canvas = new Canvas();
    this.canvas.setBounds(0, 0, width, height);
    this.canvas.setIgnoreRepaint(true);
    this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.frame.pack();
    this.frame.setResizable(false);
    this.frame.setVisible(true);
    this.panel.add(canvas);
    this.canvas.createBufferStrategy(2);
    this.bufferStrategy = canvas.getBufferStrategy();
    this.g = (Graphics2D) bufferStrategy.getDrawGraphics();
    this.canvas.requestFocus();

}

主要的:

public static void main(String[] args) {
    GameFrame gf = new GameFrame(Color.black);
    System.out.println("Panel: " + gf.panel.getBounds());
    System.out.println("Frame: " + gf.frame.getBounds());
    System.out.println("Canvas:" + gf.frame.getBounds());

    for (int R = 0; R < 255; R++) {
        for (int B = 0; B < 255; B++) {
            for (int G = 0; G < 255; G++) {
                gf.g.setColor(Color.black);
                gf.g.fillRect(0, 0, gf.width, gf.height);
                gf.g.setColor(new Color(R, B, G));
                gf.g.fillRect(0, 0, gf.width, gf.height);
                gf.bufferStrategy.show();
            }
        }
    }

}

在主我尝试循环正方形刚才看到的效果。

In the main I try to loop squares just to see the effect.

推荐答案

更​​换为的setResizable 电话,让是第二个电话。

Swap the pack and setResizable calls, so that pack is the second call.

在下面的例子中,它会写 200×200 在面板上,如果换成将会写入呼叫 210x210

In the following example, it will write 200x200 on the panel, if you swap the calls it will write 210x210

这是一个已知的问题

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestResizeFrame {

    public static void main(String[] args) {
        new TestResizeFrame();
    }

    public TestResizeFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                // Will appear as 200x200, swap them and it will appear as 210x210
                frame.setResizable(false);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); 
            FontMetrics fm = g.getFontMetrics();
            g.drawString(getWidth() + "x" + getHeight(), 0, fm.getAscent());
        }

    }

}

这篇关于框架和帆布增长大于规定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆