Java 当JFrame设置为不可调整大小时,会产生奇怪的渲染结果,但当它可调整大小时则不会

Java 当JFrame设置为不可调整大小时,会产生奇怪的渲染结果,但当它可调整大小时则不会,java,rendering,Java,Rendering,我正在创建一个游戏,我有一个带有自定义画布的JFrame。奇怪的是,当我将JFrame设置为可调整大小时,渲染结果如下所示: // DRAW A TEXTURE ON THE CANVAS public void drawTexture(Texture texture, Vector2i location) { // Store the current texture coordinates int tx = 0, ty = 0; // Scroll through ea

我正在创建一个游戏,我有一个带有自定义画布的JFrame。奇怪的是,当我将JFrame设置为可调整大小时,渲染结果如下所示:

// DRAW A TEXTURE ON THE CANVAS
public void drawTexture(Texture texture, Vector2i location) {
    // Store the current texture coordinates
    int tx = 0, ty = 0;
    // Scroll through each pixel on the screen horizontally (begin at the X coordinate specified by the 'location')
    for(int x = location.x; (x < getWidth() && x < location.x + texture.getWidth()); x++) {
        // Scroll through each pixel on the screen vertically (begin at the Y coordinate specified by the 'location')
        for(int y = location.y; (y < getHeight() && y < location.y + texture.getHeight()); y++) {
            // Set each pixel to the color of the corresponding pixel on the Texture
            pixels[x + y * getWidth()] = texture.getPixels()[tx + ty * texture.getWidth()];
            // Add one to the texture Y coordinate
            ty++;
        }
        // Reset the ty variable
        ty = 0;
        // Add one to the texture X coordinate
        tx++;
    }
}
但当我将JFrame设置为不可调整大小时,结果如下所示:

// DRAW A TEXTURE ON THE CANVAS
public void drawTexture(Texture texture, Vector2i location) {
    // Store the current texture coordinates
    int tx = 0, ty = 0;
    // Scroll through each pixel on the screen horizontally (begin at the X coordinate specified by the 'location')
    for(int x = location.x; (x < getWidth() && x < location.x + texture.getWidth()); x++) {
        // Scroll through each pixel on the screen vertically (begin at the Y coordinate specified by the 'location')
        for(int y = location.y; (y < getHeight() && y < location.y + texture.getHeight()); y++) {
            // Set each pixel to the color of the corresponding pixel on the Texture
            pixels[x + y * getWidth()] = texture.getPixels()[tx + ty * texture.getWidth()];
            // Add one to the texture Y coordinate
            ty++;
        }
        // Reset the ty variable
        ty = 0;
        // Add one to the texture X coordinate
        tx++;
    }
}
将纹理的像素阵列添加到BuffereImage的像素阵列的方法如下所示:

// DRAW A TEXTURE ON THE CANVAS
public void drawTexture(Texture texture, Vector2i location) {
    // Store the current texture coordinates
    int tx = 0, ty = 0;
    // Scroll through each pixel on the screen horizontally (begin at the X coordinate specified by the 'location')
    for(int x = location.x; (x < getWidth() && x < location.x + texture.getWidth()); x++) {
        // Scroll through each pixel on the screen vertically (begin at the Y coordinate specified by the 'location')
        for(int y = location.y; (y < getHeight() && y < location.y + texture.getHeight()); y++) {
            // Set each pixel to the color of the corresponding pixel on the Texture
            pixels[x + y * getWidth()] = texture.getPixels()[tx + ty * texture.getWidth()];
            // Add one to the texture Y coordinate
            ty++;
        }
        // Reset the ty variable
        ty = 0;
        // Add one to the texture X coordinate
        tx++;
    }
}
BuffereImage是在自定义画布的run方法中的循环内部绘制的。 自定义canvas类扩展canvas offcourse实现Runnable,它还包含自己的用于渲染的线程


我想知道是否有人知道为什么会发生这种情况,以及可能如何解决它,因为我不知道……

好吧,我找到了有效的答案,因为我发现画布比我设定的要大,所以我在谷歌上搜索,这个答案解决了这两个问题

引述对以下问题的答复:

交换pack和setresizeable调用,使pack成为第二个调用

这是一个众所周知的问题

问题