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

[618 byte] By [born2snipea] at [2007-11-27 3:46:29]
# 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.

khangharotha at 2007-7-12 8:50:14 > top of Java-index,Security,Cryptography...
# 2
Use the Java profiler to see where the CPU usage is being spent.If all you are doing is drawing simple shapes, then there is probably something wrong with your code. E.g. calling repaint from the paint(Graphics g) method etc.
rkippena at 2007-7-12 8:50:14 > top of Java-index,Security,Cryptography...
# 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

MarcusWoletza at 2007-7-12 8:50:14 > top of Java-index,Security,Cryptography...
# 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

born2snipea at 2007-7-12 8:50:14 > top of Java-index,Security,Cryptography...