Results 1 to 16 of 16
  1. #1
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Repaint suddenly stops drawing

    Problem: I have a game loop running in my project and sometimes it just stops rendering.

    Code:
    Java Code:
    while (running) {
    	if (timerfps.hasTimeGone()) {
    		repaint();
    		System.out.println("Rendering");
    	}
    	System.out.println("Running");
    }
    Everything works fine, but after some time my Overrided paintComponent method just stops being called. "Rendering" and "Running" keep printing out in the console but nothing in the paintComponent method is getting printed or drawn.

    the timerfps is my own timers set to 60 ticks / seconds.


    Is there a reason why it just stops calling the method?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Repaint suddenly stops drawing

    If the timerfps.hasTimeGone() method stops returning true, the repaint() method won't be called. If "Rendering" is printed, then the repaint() method would have been called.
    Do you have a println() call in the paintComponent() method that is called unconditionally? Does it stop printing?

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Repaint suddenly stops drawing

    It keeps printing "Rendering" every 60 / second -> timerfps.hasTimerGone() returns true every 60 / second.

    I have a println() in the paintComponent method that stops printing after a while (when the drawing to the panel stops).

    The problem with creating a SSCCE is that it has never happened before and seems to be totally random. So I can't really create one.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Repaint suddenly stops drawing

    Sorry, I have no suggestions. Non-repeatable bugs are really hard to find and fix.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Repaint suddenly stops drawing

    How large is your application? Can you show the paintComponent method? Are you catching any exceptions but not printing them?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Repaint suddenly stops drawing

    The project is rather large. I've been working on it for like a month now. Coding maybe 3-6 hours a day.

    Java Code:
    public void paintComponent(Graphics g) {
    	System.out.println("PaintComponent is drawing");
    	fps++;
    	super.paintComponent(g);
    	render();
    	imgh.draw(g);
    	imgh.resetImages();
    }
    the render() method goes through everything that should be drawn and adds them to a image array (Just to make me able to draw on different layers). and the imgh.draw(g) just draws all the images to Graphics g. and the imgh.resetImages() removes all the images from the array.

    I'm not catching any errors in the render methods or getting any of that sort.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Repaint suddenly stops drawing

    Are you certain that your program is not doing something differently later on that might not be changing the way you expect. Also, why are you automatically rendering every 60th of a second instead of when something needs to really be repainted?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Repaint suddenly stops drawing

    Now that I've looked into my code, I do have a while loop that might the the problem.

    Java Code:
    while(useLayer);
    This is to make sure that the layer array isn't in use right now. But I do reset it to false every time I'm done using it (double checked). But could it be that it still doesn't go out of the loop even though it is set to false?

  9. #9
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Repaint suddenly stops drawing

    Quote Originally Posted by jim829 View Post
    why are you automatically rendering every 60th of a second instead of when something needs to really be repainted?
    Because I'm making a game and it needs to render 60/second otherwise it's really hard to keep track if something on screen is moving or not.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Repaint suddenly stops drawing

    If you are using multiple threads, it could be a problem. Is useLayer marked as volatile (still may not solve the problem)? Some compilers will change some
    code like this:
    Java Code:
    while (useLayer);
    to this
    Java Code:
    if (useLayer) {
        while(true);
    }
    as an optimization technique. To ensure this doesn't happen in a multi-threaded environment, you need to make it volatile or have it part of a synchronization
    block (the details of which cannot be explained here).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Repaint suddenly stops drawing

    No, it's not volatile or any of that sort, but changing it to that seemed to fix it or at least made it less frequent. Can't say for sure yet but hopefully it worked.

    Thanks a lot!

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Repaint suddenly stops drawing

    How does the app handle all the printlns? If "Rendering", "Running" and"PaintComponent is drawing" are being printed 60 times a second that creates a lot of output.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Repaint suddenly stops drawing

    You're welcome! I hope it works. BTW, you should refrain from using tight loops to enter wait states. It would be better to to use a sleep within a loop. Or if possible, use the wait/notify capability of the Java thread architecture. That's what it's there for. You might want to investigate the java.util.concurrent package to see if anything there can support your application.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Repaint suddenly stops drawing

    Quote Originally Posted by Norm View Post
    How does the app handle all the printlns? If "Rendering", "Running" and"PaintComponent is drawing" are being printed 60 times a second that creates a lot of output.
    That was for debugging. I don't have that showing all the time.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Repaint suddenly stops drawing

    Yes, I understood that. What I was wondering about is what happens to all the lines that are printed when you are debugging. Does your console hold all the lines so you can look back through all of them to see when anything could have changed when the code was executing?
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Repaint suddenly stops drawing

    sorry for the delay, I'm was just using eclipses console to print out the debugging stuff. I think it can only contain like 1000 lines (not sure at all how many). Then I can look through them when/if I close the program.

Similar Threads

  1. please help me my myeclipse is closing suddenly
    By javastuden in forum Eclipse
    Replies: 0
    Last Post: 01-21-2010, 04:43 AM
  2. Java suddenly not working. Red X
    By kd1516 in forum New To Java
    Replies: 3
    Last Post: 10-23-2008, 05:23 AM
  3. [SOLVED] Problem in Drawing(Repaint)
    By Preethi in forum New To Java
    Replies: 2
    Last Post: 07-16-2008, 07:28 AM
  4. Bouncing Ball Just Suddenly Stops Mid Bounce
    By adlb1300 in forum Java 2D
    Replies: 1
    Last Post: 12-03-2007, 02:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •