java - JPanel 在单击鼠标之前不会绘制图像

标签 java image jframe jpanel bufferedimage

我启动了一个小而简单的程序,并且有一个奇怪的错误,我无法在网上找出或找到原因。

我的程序现在所做的就是构建一个 JFrame,构建一个 JPanel,然后从线程将图像绘制到 JPanel 上。问题是,除非我在框架中单击,左键单击,右键单击,甚至单击鼠标滚轮使图像显示,否则图像不会显示,但调整框架大小不会使图像显示。我已经在面板和框架上尝试过 validate(),并且尝试将 repaint() 放入线程调用的代码中,但是当 repaint() 位于代码中时图像会闪烁。

我在实际绘制图像的代码中放置了一条打印语句,但该语句在我单击框架之前永远不会显示。我已经使用调试器进行了调试,但代码似乎被正确调用。如果我将 print 语句放在从主线程的 update() 函数调用的代码中的任何位置,则无需单击即可正确显示图像。

这是我的代码,任何建议将不胜感激:

框架

public class GameFrame extends JFrame{

  int w = 1300;
  int h = 740;

  public GameFrame() {

    this.setSize(w, h);
    setTitle("Frame");

    GamePanel panel = new GamePanel(w, h);
    this.add(panel);
    this.setVisible(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  }

  public static void main(String[] args) {
    GameFrame gameFrame = new GameFrame();
  }
}

小组

public class GamePanel extends JPanel implements Runnable{

//core classes
GameRenderer renderer;
GameManager manager;
GameLoader loader;

//graphics stuff
private Graphics dbg;
private Image dbImage = null;
private int panelWidth;
private int panelHeight;

//game updating stuff
Thread animator;
boolean running;

public GamePanel(int w, int h) {
    this.setSize(w, h);
    this.setVisible(true);
    this.setFocusable(true);
    this.panelWidth = w;
    this.panelHeight = h;

    renderer = new GameRenderer();
    manager = new GameManager();
    loader = new GameLoader();

    startGame();
}

private void startGame() {
    animator = new Thread(this, "Animator");
    animator.start();
    running = true;
}

public void run() {
    while(running) {
        gameAction();
    }
}

//everything that needs to be updated continuously
private void gameAction() {
    //updateGame(); // Need code here to update everything
    gameRender(); // Draw to the double buffer.
    paintScreen(); // Draw double buffer to screen.
}

/**
 * Draws the game image to the buffer.
 * 
 */
private void gameRender() {
    if(dbImage == null) {
        dbImage = createImage(this.panelWidth, this.panelHeight);
        return;
    }
    dbg = dbImage.getGraphics();
    renderer.draw((Graphics2D) dbg, panelWidth, panelHeight);
}

/**
 * Draws the game image to the screen by drawing the buffer.
 */
private void paintScreen() {
    Graphics g;
    try {
        g = this.getGraphics();
        if ((g != null) && (dbImage != null))  {
            g.drawImage(dbImage, 0, 0, null);
            g.dispose();
        }
    } catch (Exception e) { System.out.println("Graphics context error: " + e); }
}

}

图像渲染器

public class GameRenderer {

ImageManipulator im = new ImageManipulator();

/**
 * Actually draw everything that needs to be drawn
 * Layering also happens here 
 */
public void draw(Graphics2D g,int screenWidth, int screenHeight) {
    BufferedImage tileImg = im.loadImage("Images/tile.png");
    Tile tile = new Tile(tileImg, 30, 30);
    tile.draw(g);
}

}

带有我试图绘制的图像的类

public class Tile {

BufferedImage tileImg;
//x and y values based on tile grid - z based on height up
int tileX;
int tileY;
int tileZ;
//actual coordinate of top left corner of tile *image*
int pixelX;
int pixelY;

public Tile(BufferedImage img, int pixelX, int pixelY) {
    this.pixelX = pixelX;
    this.pixelY = pixelY;
    this.tileImg = img;
}

public void draw(Graphics g) {
    g.drawImage(tileImg, pixelX, pixelY, null);
}

}

同样,任何建议或想法都会有帮助。我不知道可能是什么原因造成的。 另外,有人知道为什么这可能适用于包含打印语句但不包含打印语句吗?

最佳答案

您误解了如何在 Swing 组件中进行自定义绘制。调用 this.getGraphics() 是不正确的。绘制 Swing 组件的正确方法是重写其 paintComponent 方法。请注意,该方法中的第一行必须调用super.paintComponent:

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (dbImage != null) {
        g.drawImage(dbImage, 0, 0, this);
    }
}

另请注意,您不得尝试处置 Graphics 对象,因为它不是您创建的,而且它处于 Swing 的控制之下,而不是您的控制之下。

有关完整详细信息,请参阅 Painting in AWT and Swing教程。

当您想要重新绘制游戏时,只需调用面板的 repaint方法。因此,您的 gameAction 方法应该用 repaint() 替换paintScreen(),它将自动调用面板的paintComponent 方法(以及一些其他方法;正如该教程所解释的,绘画不是一件简单的事情)。

您需要输入一个简短的sleeprun() 方法的循环内部调用。如果您尝试不断更新和重新绘制游戏,程序将没有时间实际绘制任何内容。例如,如果您希望游戏每秒更新五次(即每 200 毫秒):

try {
    while (running) {
        gameAction();
        Thread.sleep(200);
    }
} catch (InterruptedException e) {
    e.printStackTrace();
}

关于java - JPanel 在单击鼠标之前不会绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27594289/

相关文章:

java - 如何在 iPhone 真实设备上检查和编写 xpath

java - 耶拿/Fuseki : loading a custom implementation of com. hp.hpl.jena.graph.impl.GraphBase?

c# - 如何在不降低质量的情况下调整图像大小

Python:标准化图像曝光

java - 在背景图像 JFrame 上绘制形状

Java png 出来是黑色的

java - 带重音符号的 Spring RestTemplate 文件名

image - AddThis Pinterest 小部件提供我不想提供的图像,例如导航按钮、广告。

java - JFrame:简单的按键绑定(bind)?

java - 不使用 JFrame 创建屏幕(窗口)