java frame类 canvas_java – Frame和Canvas比指定的大

我不知道为什么我会得到一个超大的窗口,这让我在游戏窗口中运行箍以适应我的精灵.构造函数应该使它所有子组件都适合彼此,但似乎画布或框架中有一些额外的填充.我很难搞清楚罪魁祸首是什么.我的框架尺寸不应大于800×600(不包括OS装饰,谈论容器).

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]

正如您所看到的,边界大于我指定的范围.

班上:

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

}

}

}

}

在主要我尝试循环方块只是为了看到效果.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值