wait() in paint()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hi19hi19
    lol happy
    FFR Simfile Author
    • Oct 2005
    • 12194

    #1

    wait() in paint()

    Code:
    public void paint(Graphics gfx)
        {
        if (this.gameState == GAME_STATE_RUNNING)
            {
                this.drawMyStuff();
    
                try
                {
                    wait(WAIT_TIME);
                }
                catch(InterruptedException e)
                {
                    System.out.println("Something screwed up: " + e);
                }
                catch(IllegalMonitorStateException e)
                {
                    System.out.println("Something screwed up: " + e);
                }
                
                this.repaint();
            }
        }
    This is paint() method from my program. The class extends JFrame FYI.

    I want it to wait long WAIT_TIME milliseconds before the repaint().
    To make pause before looping my painting.
    When I run this, it generates an exception every single time I repaint()
    Specifically, it throws a java.lang.IllegalMonitorStateException every time.

    How to fix?
    Last edited by hi19hi19; 04-23-2007, 12:45 PM.


  • evilbutterfly
    FFR Player
    • Apr 2003
    • 5784

    #2
    Re: wait() in paint()

    I'm guessing you're making some sort of game or something and you don't want some computers to blaze through the whole thing. If you're getting started making a game, this tutorial I used to start off is really helpful: http://www.planetalia.com/cursos/

    Somewhere in there it tells how to do what I believe you're looking for. Instead of putting it in the paint method for things, they do it like this:

    Code:
    while (!gameEnded)
    {
    	long startTime = System.currentTimeMillis();
    
    	checkCollisions();
    	updateWorld();
    	paintWorld();
    	usedTime = System.currentTimeMillis()-startTime;
    	do
    	{
    		Thread.yield();
    	}while (System.currentTimeMillis()-startTime< 17);
    }
    Basically it stops it for 17 milliseconds every frame. You can change that and the game will speed up or down. Maybe I'm mistaken and you're not making a game, but the thread yielding thing should still work.
    So I've gone completely slack-ass and haven't done any work on creating games. =(

    In less-depressing news, I got a job for an online business (which sells non-electronic games, of all things!) which has taught me a lot about marketing online and all that jazz.

    So now I'm on Twitter @NoahWright.
    And I write the blog for their website.

    Plus I do cool programming in-house that you'll never see. =O

    Comment

    • hi19hi19
      lol happy
      FFR Simfile Author
      • Oct 2005
      • 12194

      #3
      Re: wait() in paint()

      Elegant. Thanks.

      Yes, I was making a simulation for the Game of Life for a school project. It took 10 minutes to code the actual stuff so I was using the remaining time to make a graphical interface just for fun.

      EDIT- If I make the grid really big, like 70 x 70, the lag due to my sloppy-ish code alone slows it down enough. But for smaller grids that works nicely.


      Comment

      • unknown_soldier
        FFR Player
        • Apr 2007
        • 2

        #4
        Re: wait() in paint()

        im new

        Comment

        • seththelezzy
          FFR Player
          • Feb 2007
          • 437

          #5
          Re: wait() in paint()

          Originally posted by unknown_soldier
          im new
          ...Go to the newbie forum. Read the stickies.

          This is the wrong place to put this.


          I support using the Edit Button. You should too.

          Comment

          • midnghtraver
            Icarus Moth
            FFR Music Producer
            • Jan 2006
            • 2064

            #6
            Re: wait() in paint()

            April 23rd. and only 4 posts, and 3 that only really matter. omg. And it was made by the step w1zz3r3d.

            Comment

            Working...