This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How optimize paintComponent? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How optimize paintComponent?" Watch "How optimize paintComponent?" New topic
Author

How optimize paintComponent?

joan wo
Greenhorn

Joined: Mar 12, 2007
Posts: 9
Hello,

I'm developing an application that draw a shape over an image and then after clic a button, the application calculate all the points that are into the shape and paint it with the same color as the shape.
I paint the shape in the paintComponent method. My problem is that performance of the application descends dramatically in the moment that have to repaint all this points. How can I increase the performance?

I have the rectangle that includes the shape and I use a byte[] to store the points (1 are into the shape, 0 are out).

Thank's a lot.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Try using the shape to set the clip for the graphics context and then draw the image.

The image pixels that fall inside the clipping shape should be the only ones rendered.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
And for painting the image in a different color, take a look at the class RGBImageFilter.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
joan wo
Greenhorn

Joined: Mar 12, 2007
Posts: 9
Hello,

Thank's a lot for your replies.

I have two doubts:
- In the shape I only have the points of the border of the shape. I have to find all the points of the interior of the shape, is in this process when the process is too slow. How can I use the setClip method to obtain the points of the interior?
- Is possible to call paintComponent (with reapint) only when there are a change on the image. I've tried to do it, but then the shape is not ever repaint it, and disappears.

Thank's.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How optimize paintComponent?
 
Similar Threads
class who implement shape, paint, draw
PolyLine or Polygon?
previously drawn shapes disappear when new one is created
Draw rectangle by dragging mouse
Need Help with Multiple Classes