简体   繁体   English

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

[英]Why does an empty canvas appear instead of my image?

I'm coding a game, and I want there to be the company logo for about 3-5 seconds before entering the game itself. 我正在编写游戏,在进入游戏本身之前,我希望公司徽标在3-5秒钟左右。 Here's my code: 这是我的代码:

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

My main problem is that a blank JFrame appears, then after 4 seconds, the game itself appears. 我的主要问题是出现空白的JFrame,然后在4秒钟后出现游戏本身。 What's wrong with my code? 我的代码有什么问题? Is there something I should be doing that I'm not right now? 有什么我应该做的事吗?

It is taking 4 seconds to paint the complete JFrame because you have a sleep of 4 seconds in your code: 绘制完整的JFrame需要4秒钟,因为您在代码中的睡眠时间为4秒钟:

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

This problem might be in image variable after while loop of Thread.sleep . Thread.sleep while循环之后,图像变量中可能存在此问题。

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

This image make your frame blank. 此图像使您的框架空白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM