Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with repaint()  RSS feed

 
Randall Twede
Ranch Hand
Posts: 4585
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried to find an answer myself but my patience is limited

repaint() must start a new thread and this is causing me a problem.



the output should be:
entering mouseDragged()
1
entering paintComponent()
0
entering mouseDragged()

instead i get:
entering mouseDragged()
1
0
entering paintComponent()
entering mouseDragged()

how do i fix this?
should i call paintComponent() directly?
everything i read says not to do that.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:how do i fix this?

Wrapping the call to shapes.remove(...) in a SwingUtilities#invokeLater(...) should do it. The call to repaint() schedules a painting event, which is honored after any code still executing on the EDT -- which is the rest of your method.

Randall Twede wrote:should i call paintComponent() directly?
everything i read says not to do that.

Yup. Don't even think about it.

Is this just a theoretical exercise or is there something you're trying to achieve by adding the Shape to a Vector and removing it a few lines later? Also, do you have a good reason for not using ArrayList, which doesn't have the baggage of synchronization?
 
Randall Twede
Ranch Hand
Posts: 4585
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is a program i wrote in 2004 in college. i think we were told to use a vector. the original program drew the shape, and all previous ones stored in the vector on mouseReleased. i wanted it to draw the shape as it goes. i do add the shape back in the vector on mouseReleased. dont want to store all the intermediate shapes.

how do i fix this?
 
Randall Twede
Ranch Hand
Posts: 4585
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didnt notice the first part of your post

Wrapping the call to shapes.remove(...) in a SwingUtilities#invokeLater(...) should do it.
[/qutoe]
ill look that up
 
Randall Twede
Ranch Hand
Posts: 4585
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that doesnt work i get a compiler error that says the argument needs to be a runnable not an object.

maybe i could call sleep()
if i do, how long should i tell it to sleep?
any ideas?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:maybe i could call sleep()

Never on the EDT, unless your intention is to freeze the GUI.

Recommended reading: Concurrency in Swing
 
Randall Twede
Ranch Hand
Posts: 4585
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks!
that fixed it!
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of adding/removing the temporary Shape object to your Vector you could just create a "temporaryShape" variable. You update this variable on mousePressed/dragged. When the mouseRelease event fires you would set this variable to null. Now in your painting code you paint everthing in the Vector and the temporaryShape when it is not null.

This way you don't have to worry about the invokeLater() and the ordering of events on the EDT.
 
Randall Twede
Ranch Hand
Posts: 4585
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i see what you mean. on mouseReleased() i would add it to the vector then set it to null(actually i dont think i have to set it to null). i think i will try that as well. i need the practice
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!