is there more efficient painting out there?...
I'm working on an application that allows the user to draw shapes (rectangle, oval, lines, etc) and they are able to resize the shapes as needed. When you drag a corner to resize the shape the cpu usage shoots up pretty fast! The way I have this setup is everytime a point changes the repaint is called.
I have looked at the method "paintImmediately" on JComponent which allows you to paint a certain region instead of the whole component, but this didn't seem to help out in performance.
Before anyone asks, yes, I am double buffering. :0)
Any ideas are appreciated.
Thanks,
b2s
# 1
Well just out of curiosity,Are you using Java2D or something like opengl rendering(JOGL).Because in Java2D this should not happen .
I have few questions about your method:
1) Are you putting your Logic or drawing algo in Swing Event thread.If yes than this could be a problem.
2) Is the problem happen when you are dynamically drawing shapes or even when you just resize the window?
If you can list out the flow or can post a test case than we might be able to help you.
# 3
Hi,
[...]
> shoots up pretty fast! The way I have this setup is
> everytime a point changes the repaint is called.
What do you do in your paintComponent Method. If you have to draw a complex drawing in your paint Method an when for example this Method is called many times, this increases the CPU usage.
[...]
> Before anyone asks, yes, I am double buffering. :0)
Double buffering would not help you if you draw your whole image many times. With "double buffering" do you mean the implicit buffering that swing does or do you have your own buffering?
>
> Any ideas are appreciated.
>
> Thanks,
> b2s
bye
Marcus
# 4
Sorry for such the late reply, if you would like to see my code checkout my project on sourceforge. Just look in the subversion repository for the project.
http://sourceforge.net/projects/planofattack
To give you an idea of the classes roles:
PlanPanel - the swing component that actually is viewed by the user
PlanRendererImpl - renders the current plan to an image to be painted
AbstractDrawable - the class is the base shape
How do these fit together you ask?
Well, as the user drags the drawable around i'm updating properties on the drawable. As the properties are being changed they are firing PropertyChangeEvents which get caught by the PlanPanel. The panel asks the render to render the current plan to an image.
If you have any questions about the code, please ask, all comments/suggestions are welcome.
Thanks,
b2s