954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Stop repaint()

I'm creating a silly game, and the first thing I want to make is to tell you "You Lose" if the all that you control with the mouse is not inside the frame. I created a JComponent and every 20 milliseconds it is repainted (A thread does that). Inside it, I detect if the ball is inside the the frame, an if it's not, I just call a method that puts a JOptionPane that says "You Lose", and then exits the program. the problem is, since the JComponent is repainted every 20 milliseconds, loads of JOptionPane messages star to appear. I've tried making the repainting stop, but I was unsuccessful. I tried using the interrupt() method of the thread, telling it to sleep(), removing it from the ScheduledThreadPoolExecutor it was being executed by, shutting down the executor, but nothing worked. How can I make the repainting stop?

Thanks in advance.

yancouto
Light Poster
27 posts since Apr 2011
Reputation Points: 10
Solved Threads: 3
 

don't do that

I created a JComponent and every 20 milliseconds it is repainted


because latency from Native OS are always more than 63ms, yes is possible to paint every 50ms but you have to implements DirtyRegion to avoids GUI's freeze or RepaintManagerError, start with 100ms, better would be 250ms

you have to stop ScheduledThreadPoolExecutor, same as you start ....,

if you want to play with Executor, then look for Monitor

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

perhaps you can add some boolean variable and an if statement that check for truth value of the variable and call the repaint method only inside the if statement, which means that repaint will be called only while the boolean variable evaluates to true. hope that helps.

bibiki
Posting Whiz
327 posts since Nov 2009
Reputation Points: 28
Solved Threads: 27
 
perhaps you can add some boolean variable and an if statement that check for truth value of the variable and call the repaint method only inside the if statement, which means that repaint will be called only while the boolean variable evaluates to true. hope that helps.

Thank you, this worked! But the problem is, since it repaints, but nothing happens because the boolean changed, after you lose the screen becomes empty, because nothing is being painted. I wanted a way to stop the thread that repainted so that the last image before you lose remained. But thanks.

yancouto
Light Poster
27 posts since Apr 2011
Reputation Points: 10
Solved Threads: 3
 

I guess if your if statement is the last inside your paintComponent method, the last screen should persist unless you have some other place you invoke the repaint method in... like say if you have an interactive application, like some button or some method linked to some key pressed, and it calls repaint... then you of course are invoking the repaint outside the paintcomponent method too, and in that case your boolean variable may be out of reach and useless. glad you made it work.

bibiki
Posting Whiz
327 posts since Nov 2009
Reputation Points: 28
Solved Threads: 27
 
I guess if your if statement is the last inside your paintComponent method, the last screen should persist unless you have some other place you invoke the repaint method in... like say if you have an interactive application, like some button or some method linked to some key pressed, and it calls repaint... then you of course are invoking the repaint outside the paintcomponent method too, and in that case your boolean variable may be out of reach and useless. glad you made it work.

The repaint is called in a Thread, what should work is to stop the Thread, but my attempts didn't work.

yancouto
Light Poster
27 posts since Apr 2011
Reputation Points: 10
Solved Threads: 3
 

You really should try to keep your logic and your painting as separate as possible...

hanvyj
Posting Whiz in Training
225 posts since Aug 2010
Reputation Points: 17
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: