为什么会出现空白画布而不是我的图像?

时间:2013-09-28 16:07:11

标签: java swing event-dispatch-thread thread-sleep

我正在编写一个游戏,我希望在进入游戏之前有大约3-5秒的公司徽标。这是我的代码:

Graphics gfx = buffer.getDrawGraphics();
    gfx.setColor(new Color(146, 17, 189));
    gfx.fillRect(0, 0, this.getWidth(), this.getHeight());
    // Draw stuffs between here...
    gfx.drawImage(icon.getImage(), 0, 0, this.getWidth(), this.getHeight(), null);
    int timer = 0;
    while (timer <= 4) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException exc) {
            exc.printStackTrace();
            System.out.println("Could not put thread to sleep! :(");
        }
        timer++;
    }
    gfx.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
    if (key.showFPS == true) {
        //Set it up so that it still works with the "per second" rule.
        key.showPerSeconds(buffer, FPS, TPS);
    }
    // and here.
    gfx.dispose();
    buffer.show();
}

我的主要问题是出现空白JFrame,然后在4秒后,游戏本身出现。我的代码出了什么问题?有什么我应该做的,我现在不是吗?

2 个答案:

答案 0 :(得分:0)

绘制完整的JFrame需要4秒钟,因为你的代码中有4秒的睡眠时间:

while (timer <= 4) {
        try {
            Thread.sleep(1000);

答案 1 :(得分:0)

此问题可能在Thread.sleep的while循环后的图像变量中。

gfx.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null); 

此图像会使您的画面留空。