我的 GUI 中的原点偏离了大约 25/26 像素
我的 GUI 上的原点 (0,0) 向上移动了大约 25/26 像素.所以我的 GUI 的左上角似乎代表 0,25 或 0,26.我正在使用双缓冲区来绘制东西.基本上,每当我尝试在缓冲区中的 0,0 处绘制一些东西时,它就会出现在屏幕外.
My point of origin (0,0) on my GUI is moved up by about 25/26 pixels. So the very top-left corner of my GUI seems to represent 0,25 or 0,26. I am using a double buffer to draw in things. Basically, whenever I try to draw something in my buffer at 0,0, it appears off-screen.
例如,下面是我尝试生成的棋盘图案的屏幕截图,从 0,0 开始.虽然水平看起来不错,但请注意顶部的切碎棋盘.高度是 480,记住它可以被 32 整除,所以棋盘格图案应该完美填充.
For example, below is a screenshot of a checkerboard pattern I attempted to generate, starting from 0,0. Although it seems fine horizontally, notice the chopped checkerboard at the top. The height is 480, which is divisible by 32 keep in mind, so the checkerboard pattern should fill in perfectly.
这是我的代码(剪断无关部分):
And here is my code (Snipped in irrelevant parts):
public class Dekari_gameGUI extends JFrame implements KeyListener {
// Resources
private final String imagePath = "Resources/Images/";
private final String spriteSheetPath = imagePath + "Sprite Sheets/";
private final String soundPath = "Resources/Sounds/";
private final String dataPath = "Resources/Data/";
private BufferedImage[][] sprites;
private Timer playerMovementTimer = new Timer();
//Game variables
private int pX = 0, pY = 0;
private short pDir = -1, pLastDir = 0, pAniStage = 0, counter = 0;
//Graphics and paint variables
private Graphics buffer;
private Image offscreen;
private Dimension dim;
//Internal variables, used for loops and placeholders
private int a, b, c, d;
/*
____ _ _
/ ___|___ _ __ ___| |_ _ __ _ _ ___| |_ ___ _ __
| | / _ | '_ / __| __| '__| | | |/ __| __/ _ | '__|
| |__| (_) | | | \__ |_| | | |_| | (__| || (_) | |
\____\___/|_| |_|___/\__|_| \__,_|\___|\__\___/|_|
*/
public Dekari_gameGUI() {
// Declaring GUI setup
setResizable(false);
setTitle("Dekari RPG Indev v1.0");
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setSize(640, 480);
setBackground(Color.GREEN);
setLocationRelativeTo(null); //Centers the window
splitSpriteSheets(); //Sets up resources
dim = getSize();
offscreen = createImage(dim.width, dim.height);
buffer = offscreen.getGraphics();
*snip*
setVisible(true);
}
*snip*
/*
____ _ _
| _ __ _(_)_ __ | |_
| |_) / _` | | '_ | __|
| __/ (_| | | | | | |_
|_| \__,_|_|_| |_|\__|
*/
public void paint(Graphics g) {
//Clears the buffer
buffer.clearRect(0, 0, dim.width, dim.width);
//Drawing images to the buffer
buffer.setColor(Color.BLACK);
boolean paint = true;
//This is my checkerboard drawing function
for (a = 0; a < getWidth() / 32; a++) {
for (b = 0; b < getHeight() / 32; b++) {
if (paint) {
paint = false;
buffer.fillRect(a * 32, b * 32, 32, 32);
} else {
paint = true;
}
}
}
if (pDir == -1) //If the player is not moving
//Draws player in an idle stance facing last moved direction
buffer.drawImage(sprites[0][4 * pLastDir], pX - 24, pY - 32, this);
else //If the player is moving
//Draws player in a movement state facing the current direction
buffer.drawImage(sprites[0][pAniStage + (4 * pLastDir)], pX - 24, pY - 32, this);
//Drawing the buffer to the frame
g.drawImage(offscreen, 0, 0, this);
}
public void update(Graphics g) {
paint(g);
}
}
推荐答案
不,不是.您直接在框架上绘画,它的边框装饰画在框架边界内.欢迎来到为什么不应该直接在顶级容器上绘画的奇妙世界.
No it's not. You painting directly to the frame, which has it's border decorations painted WITHIN the frames boundaries. Welcome to the wonderful world of why you shouldn't paint directly to top level containers.
相反,将您的自定义绘画移至另一个组件,例如 JPanel
并将其直接添加到框架中.这将被布置在如此远的地方,以便定位在框架边框装饰内
Instead, move you custom painting to another component, such as a JPanel
and add this directly to the frame. This will be laid out in such away so as to positioned within the frames border decorations
通过覆盖面板的 getPreferredSize
方法并返回适当的值,您可以利用 JFrame#pack
来打包"框架,使其满足首选大小要求内容.这将确保内容处于其首选大小并且围绕它的窗口装饰.
By overriding the panel's getPreferredSize
method and returning an appropriate value, you can utilise JFrame#pack
to "pack" the frame so that it meets the preferred size requirements of the content. This will ensure that the content is at it's preferred size and the window decorations surround it.
还请记住,Swing 组件默认情况下是双缓冲的,因此您不必重新发明轮子,除非您有特殊需要...
Also remember, Swing components are double buffered by default, so you won't have to reinvent the wheel, unless you have a particular need to do so...
有关更多信息,请查看 我该怎么做设置在中间? 和 如何获得屏幕的精确中间,即使重新调整大小和为什么在顶级容器中覆盖绘画如此糟糕?和为什么不直接在 JFrame 中绘制了解更多详情
For more information, take a look at How can I set in the midst? and How to get the EXACT middle of a screen, even when re-sized and Why is overriding paint in a top-level container so bad? and Why not to draw directly inside JFrame for more details
相关文章