gpt4 book ai didi

java - 框架和 Canvas 变得比指定的大

转载 作者:行者123 更新时间:2023-12-01 23:18:08 25 4
gpt4 key购买 nike

我不知道为什么我会得到一个超大的窗口,这让我跑圈以适应游戏窗口中的 Sprite 。构造函数应该使所有子组件都能相互适应,但 Canvas 或框架中似乎有一些额外的填充。我很难弄清楚罪魁祸首是什么。我的框架的大小不应大于 800x600(不包括操作系统装饰,谈论的是容器)。

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]

如您所见,边界比我指定的要大。

类(class):

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

}

主要是我尝试循环方 block 只是为了看看效果。 enter image description here

最佳答案

交换 packsetResizes 调用,以便 pack 是第二个调用。

在下面的示例中,它将在面板上写入 200x200,如果交换调用,它将写入 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());
}

}

}

关于java - 框架和 Canvas 变得比指定的大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15560527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com