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