1

This is my code so far:

// Imported Classes
public class Timer extends Applet
{
    public void paint (Graphics page) throws InterruptedException
    {
        Thread.sleep(1000);
    }
}

I just want to know how I can get this to work. I've used the Thread.sleep() method in other code before, but never with Graphics. I don't have much experience with Exceptions either, I usually try my best to avoid or correct them.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    Doing that freezes the applet. What are you trying to do? Why do you want to do this? – Radiodef May 12 '15 at 14:35
  • Makin' a timer just 'cause. It's meant to pause the thread, clear the screen by a call to page.fillRect() or something, and then print the time left. But, this is just about the Exception. – xInMourning May 12 '15 at 14:37
  • 1
    *"But, this is just about the Exception"* Not anymore! Because you shouldn't be doing this. Use e.g. a `javax.swing.Timer` instead. – Radiodef May 12 '15 at 14:40
  • If you are not expecting to interrupt the thread yourself, you can just `try {} catch { log.error(e); }` or `try {} catch { throw new RuntimeException(e); }` depending on your error policy. No matter the policy you should do then in other (model/controller) thread though. – Mateusz Kubuszok May 12 '15 at 14:44
  • Also see [*Java game loop (painting) freezes my window*](http://stackoverflow.com/questions/29745778/java-game-loop-painting-freezes-my-window/29837148#29837148) – Radiodef May 12 '15 at 14:49
  • 1
    1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson May 14 '15 at 05:42

1 Answers1

4

You should never call methods such as Thread.sleep on the event dispatch thread (i.e. in paint methods). This will render the whole GUI unresponsive.

You should instead use timers such as SwingTimer to perform animations etc. See the following related questions:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Thanks. Sorry, I'm more used to using Lua than I am Java. Least I know more than our AP Computer Science teacher. – xInMourning May 12 '15 at 14:43
  • Heh. I see. I remember that this whole event dispatch thread thing took a long while for me to grasp. What's important to understand is that the thread that calls the paint method isn't "yours", it's the GUI systems thread that asks you to paint, and you shouldn't hijack that thread by pausing it. You should do your painting and return promptly. If you want to repaint over and over again, you should arrange so that the system asks you to paint over and over again. – aioobe May 12 '15 at 14:45
  • Having difficulty understanding how to use this Timer. Is there a way that I can keep track of the amount of time passed? This is originally how I intended it to function. while true do { page.drawString("" + count, 50, 100); Thread.sleep(1000); count--; if (count < 0) break; } – xInMourning May 13 '15 at 13:43
  • Yes. Keep `long startTime = System.currentTimeMillis()` and use `System.currentTimeMillis() - startTime`. – aioobe May 13 '15 at 13:44