File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes FRC Cons? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "FRC Cons?" Watch "FRC Cons?" New topic
Author

FRC Cons?

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

I have been wondering about the cool effects one can achieve by tweaking the paintComponent method. Does this not increase the overhead? I mean I got some UI running with some mindblowing effects and stuff. Now if I move my mouse around the screen, wont the JVM call the paint everytime (OK. probably it will figure out the rect to be repainted and optimise it that way) But even still, does it not mean more overheads?

Or is this a silly question?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Romain Guy
author
Ranch Hand

Joined: Sep 18, 2007
Posts: 47
Except when using animations, using richer effects does not entail more calls to repaint. Each repaint, however, is more expensive. Our book makes sure to teach the reader all of the performance tips and tricks we used over the years to ensure our effects were fast. If you want to see what performance can be, take a look at the following video of Aerith: http://progx.org/users/Gfx/aerith_hd.zip
Chet Haase
author
Ranch Hand

Joined: Aug 16, 2007
Posts: 32
Your mail seems to imply (or ask) whether simple mouse movements will entail repaints (calls to paintComponent()). This is not the case unless you have some kind of display change that's triggered on mouse motion (not a common case unless you have animated effects triggered on mouse-hover events or something). Swing is optimized for only repainting things that have actually changed appearance, so if moving the mouse around the screen doesn't cause anything in your app to change, then there will be no calls to paintComponent().

Chet.


Book: <a href="http://filthyrichclients.org" target="_blank" rel="nofollow">http://filthyrichclients.org</a> <br />Java: <a href="http://weblogs.java.net/blog/chet" target="_blank" rel="nofollow">http://weblogs.java.net/blog/chet</a> <br />Humor: <a href="http://chetchat.blogspot.com" target="_blank" rel="nofollow">http://chetchat.blogspot.com</a>
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

Originally posted by Chet Haase:
Your mail seems to imply (or ask) whether simple mouse movements will entail repaints (calls to paintComponent()).............

Swing is optimized for only repainting things that have actually changed appearance, so if moving the mouse around the screen doesn't cause anything in your app to change, then there will be no calls to paintComponent().

Chet.


Yes. That was one point of confusion in my mind. The mouse scenario I had cited was an example which I thought would trigger paint/repaint as the UI needs to be repainted. Maybe instead of the mouse I should have given the scenario of some swing app running and the user toggling to some other app.

Let me see if I got this thing correct. If I have a JTable being rendered on the UI, unless the table model is changed, the paingComponent() wont be called even if I say minimized and maximised the window? Is this applicable also if I have a panel whose paintComponent I have tweaked to say display the olympic rings as a background? In a nutshell, is swing painting is optimized to figure out model changes and only then call repaint?
Chet Haase
author
Ranch Hand

Joined: Aug 16, 2007
Posts: 32
Yes. That was one point of confusion in my mind. The mouse scenario I had cited was an example which I thought would trigger paint/repaint as the UI needs to be repainted. Maybe instead of the mouse I should have given the scenario of some swing app running and the user toggling to some other app.


Even that case is optimized. As of Java SE 6, Swing uses a back buffer whose contents map exactly to the pixels you see on the screen. So if you do anything to destroy the screen contents (toggling to another app, dragging a window on top of the Swing app, etc.), then Swing merely re-copies the pixels from the back buffer and doesn't trigger a repaint (so no calls to paintComponent()).

Let me see if I got this thing correct. If I have a JTable being rendered on the UI, unless the table model is changed, the paingComponent() wont be called even if I say minimized and maximised the window? Is this applicable also if I have a panel whose paintComponent I have tweaked to say display the olympic rings as a background? In a nutshell, is swing painting is optimized to figure out model changes and only then call repaint?[/QB]


Yes. There are other ways that repainting can get triggered (including changing your screen resolution, manually triggering it in the app with a call to repaint(), or resizing the window), but in general Swing only bothers to repaint when something has changed in the component (or its model) that changes appearance. Mousing over a JLabel will not cause a repaint, calling its setText() method will.

Of course, running animations in your UI (either independently or trigger by UI interactions like mouse hovering) changes the equation. But I think your question was more about what default repainting was like.

Chet.
Chet Haase
author
Ranch Hand

Joined: Aug 16, 2007
Posts: 32
Yes. That was one point of confusion in my mind. The mouse scenario I had cited was an example which I thought would trigger paint/repaint as the UI needs to be repainted. Maybe instead of the mouse I should have given the scenario of some swing app running and the user toggling to some other app.


Even that case is optimized. As of Java SE 6, Swing uses a back buffer whose contents map exactly to the pixels you see on the screen. So if you do anything to destroy the screen contents (toggling to another app, dragging a window on top of the Swing app, etc.), then Swing merely re-copies the pixels from the back buffer and doesn't trigger a repaint (so no calls to paintComponent()).

Let me see if I got this thing correct. If I have a JTable being rendered on the UI, unless the table model is changed, the paingComponent() wont be called even if I say minimized and maximised the window? Is this applicable also if I have a panel whose paintComponent I have tweaked to say display the olympic rings as a background? In a nutshell, is swing painting is optimized to figure out model changes and only then call repaint?[/QB]


Yes. There are other ways that repainting can get triggered (including changing your screen resolution, manually triggering it in the app with a call to repaint(), or resizing the window), but in general Swing only bothers to repaint when something has changed in the component (or its model) that changes appearance. Mousing over a JLabel will not cause a repaint, calling its setText() method will.

Of course, running animations in your UI (either independently or trigger by UI interactions like mouse hovering) changes the equation. But I think your question was more about what default repainting was like.

Chet.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

Originally posted by Chet Haase:

But I think your question was more about what default repainting was like.

Chet.


Yup and you just made my day. A big thank you!
I am currently desining a process flow kind of app where I need to show all kinds of vectors(lines) which will convey information on how the data is flowing and what all things are happening in general. Initally I had considering tweaking the paintComponent(). However I was a bit reluctant to follow this approach as I had (wrongly) thought this would cause an overload as the drawing of the vectors involves quiet some amount of computation. Now I can safely go back to that approach.

Thanks a bunch once again.
Chet Haase
author
Ranch Hand

Joined: Aug 16, 2007
Posts: 32
It's worth noting that if you have custom content in a component, such as the vectors you describe, then a change to that content may be something that Swing doesn't know about, so you may have to cause a repaint() to occur manually. But as long as you're not changing that content, then Swing should have no reason to repaint that region.

Chet.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: FRC Cons?