0

So I want "Hello World" to stay on the screen for 4 seconds, I have the thread to set to sleep for 4 seconds and then to return to the main game code. It doesn't seem to do what I want it too, I haven't done this sort of thing before so I'd be glad if someone could give me a hint. The only thing this code does is wait 4 seconds on a blank screen and then return to the game. I know that the Thread.sleep() is probably not going to do what I want, so how could I go about doing this?

public class SplashScreen {

    public static void SplashScreen() {
        Main.state = STATE.PLAY;
    }

    public void render(Graphics g) {

        g.setColor(Color.BLACK);
        g.fillRect(0, 0, (Main.WIDTH * Main.SCALE) + 50, (Main.HEIGHT * Main.SCALE) + 50);

        Font stringFont = new Font ("SansSerif", Font.PLAIN, 30);

        //Name of the Game title Screen
        g.setColor(Color.WHITE);
        g.setFont( stringFont );
        g.drawString("Hello World", 100, 100);

        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        SplashScreen();

        // No Rendering past here.
        g.dispose();
    }

Graphics initialization:

public void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();

    if (state == STATE.SPLASH) {
        splash.render(g);
    }

    int xOffset = player.x - (screen.width / 2);
    int yOffset = player.y - (screen.height / 2);

    level.renderTiles(screen, xOffset, yOffset);

    level.renderEntities(screen);

    for (int y = 0; y < screen.height; y++) {

        for (int x = 0; x < screen.width; x++) {
            int colourCode = screen.pixels[x + y * screen.width];
            if (colourCode < 255)
                pixels[x + y * WIDTH] = colours[colourCode];
        }
    }

    g.setColor(Color.BLACK);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    g.dispose();
    bs.show();
}
Rob
  • 1
  • 3
  • `"Should this work for rendering an image on the screen for 4 seconds in java?" ` -- the answer to this question is to first test it, you've got nothing to lose in the attempt and all to gain. – Hovercraft Full Of Eels Oct 19 '15 at 14:52
  • That having been said, I think that you want to use a `java.awt.SplashScreen` object and a Swing Timer, and close the SplashScreen when the Timer has "ticked". – Hovercraft Full Of Eels Oct 19 '15 at 14:52
  • All right thanks, I'll look into using swing timers – Rob Oct 19 '15 at 14:54
  • Here's a link to [similar questions on this site](http://stackoverflow.com/search?q=%5Bjava%5D+%5Bswing%5D+splashscreen+timer). – Hovercraft Full Of Eels Oct 19 '15 at 14:55
  • We can't see the code that calls this method and provides the Graphics object - that's probably where the problem lies. What's *possibly* happening is that the drawing operations are buffered, and the buffer doesn't get flushed until you return from the render method - but by that time, the Graphics object is empty. – Mike Baranczak Oct 19 '15 at 15:01
  • Oops I added the graphics code Mike – Rob Oct 19 '15 at 15:10

0 Answers0