aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Repaint and Mouse Motion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Repaint and Mouse Motion" Watch "Repaint and Mouse Motion" New topic
Author

Repaint and Mouse Motion

n romaai
Greenhorn

Joined: Mar 18, 2011
Posts: 13
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

Joined: Jun 09, 2003
Posts: 4632
> I want the animation to happen faster..

faster than what?

n romaai
Greenhorn

Joined: Mar 18, 2011
Posts: 13
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

Joined: Mar 18, 2011
Posts: 13
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Mar 18, 2011
Posts: 13
For all you non-clairvoyant

n romaai
Greenhorn

Joined: Mar 18, 2011
Posts: 13
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

Joined: Mar 18, 2011
Posts: 13
Also, why would moving the mouse over the panel smoothen the animation?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Mar 18, 2011
Posts: 13
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Jul 19, 2011
Posts: 175

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.


Please correct my English.
n romaai
Greenhorn

Joined: Mar 18, 2011
Posts: 13
Okay, You guys are saying to do this:

Now what?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Mar 18, 2011
Posts: 13
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

Joined: Jun 09, 2003
Posts: 4632
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Repaint and Mouse Motion
 
Similar Threads
paintComponent JPanel
Using Custom Cursors
Pausing the Mouse Listener
Movable JPanel - How?
Using Custom Cursors