Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Repaint and Mouse Motion

 
n romaai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On my debian laptop, Moving the mouse over the JPanel in the JFrame seems to smoothen the animation in the JPanel. Can anybody give insight as to why this might happen?
Also, is there a way to smoothen the animation programmatically? Should I try double buffering or is there other things I can try too?
I want the animation to happen faster, but I still want all intermediate values, so it wont look choppy. Increasing the frame rate makes it smoother not faster, but like I said I dont want to make each step bigger.

Any ideas?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I want the animation to happen faster..

faster than what?

 
n romaai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I want the animation to go faster without making it step more, so it stays smooth. Im basically drawing a circle that grows in size, so instead of making the radius increase by a greater amount, which makes it choppily grow in size, I want it to be a smooth trnansition, but I want the whole thing to happen faster. I want it to go faster than it is but I dont want to sacrifice smoothness
 
n romaai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, Its looking better on my Win Vista box that has much more mem and two better processors. But I'd still be very interested in some tips to the original question. Shirley, theres a software approach to the question
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Shirley, theres a software approach to the question

definitely, but, as is, we'd need a crystal ball to help you - you're the only one with the 'slow' code.
 
n romaai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For all you non-clairvoyant

 
n romaai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, lets make sizestep = .0005.
So the question is, without changing the sizestep, is there a way to make the animation go faster?
 
n romaai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, why would moving the mouse over the panel smoothen the animation?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Honestly, the code is terrible. Read the link for the correct way of painting in Swing.

http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html


the indicated line needs to be updated via the EDT


> why would moving the mouse over the panel smoothen the animation?

I noticed no change when I ran the code. platform/version issue? I'm Windows 7, java 1.6_31

 
n romaai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:Honestly, the code is terrible. Read the link for the correct way of painting in Swing.


Can you be more specific or can you just paste a link to a tutorial? I mean, thanks for saving me the trouble of searching and clicking on a link.

Also, whether the code is terrible or not is not the original question. The original question pertains to the code in the paint method of the Visual.
Is there a way to make it go faster without just changing sizestep?
Also, on Debian 6, moving the mouse over the window smoothens the animation. Why?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> The original question pertains to the code in the paint method of the Visual.
> Is there a way to make it go faster without just changing sizestep?

if you do the painting correctly, the problem might not exist
if you call repaint on the EDT, again the problem might not exist

there's just no point trying to tinker with something that is broken in so may areas.

> Also, on Debian 6, moving the mouse over the window smoothens the animation. Why?

maybe a Debian user can answer for you.
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am running Arch Linux. No slow down here. Please listen to what Michael said! Your code does things which are highly discouraged and the api says clearly that unpredictable errors might occur if calling those methods from threads != edt.
 
n romaai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, You guys are saying to do this:

Now what?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Okay, You guys are saying to do this:

that's the first part, but change it a bit

this is main() - it should do no more than start the program (and assign args[], if set)



this is the constructor for Main() (poor choice of a name, but not really important)


> Now what?

1) as I mentioned earlier, this line needs to be on the EDT (SwingUtilities...)
panel.repaint();

2) "public void paint(java.awt.Graphics g){"

never override paint(..) in Swing, use paintComponent(..),
and the first line generally is super.paintComponent(..);

 
n romaai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So using a SwingWorker to call repaint

Thanks for the tip about the name 'Main'

never override paint(..) in Swing, use paintComponent(..),
and the first line generally is super.paintComponent(..);

Are you sure?

From JavaDocs
Subclasses can just override this method, as always. A subclass that just wants to specialize the UI (look and feel) delegate's paint method should just override paintComponent.

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from your link, first line

"Applications should not invoke paint directly,"

[edit]
actually wrong - refers to repaint.
I'll find another doc and post that
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic