-2

I was just wondering if there was a way to have an Graphics object that is a String (g.drawString(...)) or an Image (g.drawImage(...)) disappear after a set time once it has been rendered to the screen. I am writing a game in Java and would like a welcome message to be displayed once the game is started for a short amount of time as in 10 - 20 seconds. I am using the Slick2D gaming library which is a wrapper for the lwjgl library for a little background. I can't seem to find a solution on this site or other forums and such as well as the Java documentation. If I missed a solution to this that has already been documented then I apologize for the time waisted, but would appreciate a link to said solution. I've tried implementing a while loop and a counter to see if that would work but with how the render method works it runs through it either too quickly or causes the game to hang or runs through the loop before the play state is entered from the main menu. I'm guessing this solution wouldn't work or be efficient since the render method is ran hundreds of times a second to make a flawless appearance of images on the screen.

tl;dr

I would like to know how to have a Graphics object be rendered to the screen, stay for a few seconds, then disappear from the screen. Any feedback is appreciated, thank you for reading!

  • In essence, yes. When the time frame has passed, simply stop painting what ever it is you painted originally. I'm assuming that the paint routine is painted on a regular (active) bases, so you simply need to monitor some kind of flag and when it's raised, change what is been painted... – MadProgrammer May 13 '14 at 05:05
  • @MadProgrammer I'm not sure I understand the logic, or really how to implement. I've been coding for most of today at this point so maybe some rest will help me see it clearer. I noticed some threads talking about using the System.currentTimeMillis() method to compute time, maybe this would make things simpler? – user2134011 May 13 '14 at 05:59
  • First show us your code for painting graphics (if you don't know how to, there are *many* questions here about it). Then show us (or learn) the code for counting time. Finally, combine the two. – user1803551 May 13 '14 at 06:43

1 Answers1

0

Use a counter. delta, passed into the update method, represents the time between frames in milliseconds. Add that to your counter every frame. When rendering, check if the timer is less than a certain value before rendering.

int time = 0;
int duration = 1000;

// in update():
time += delta;


// in render():
if (time<duration) {
    g.drawImage(/* image here */);
}

This will draw an immage for one second.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75