Skip to main content

Rectangle and background clipping issue

7 replies [Last post]
qu0ll
Offline
Joined: 2006-12-09

In simplified terms, I have a situation where I am drawing a large filled Rectangle and then drawing other smaller filled rectangles in front of or on top of the large one. The rectangles at the front do not completely cover the large rectangle at the back so there is a region of the back rectangle around the smaller rectangles that "shows through" so to speak.

The question is, is it possible (perhaps with some form of "negative" clipping) to repaint the background colour of the large rectangle without having to repaint the smaller rectangles as well? So, just the area that shows through from the large rectangle is repainted. At the moment, I have to repaint the large rectangle and then all the smaller ones as well which is clearly too expensive.

Any ideas?

--
And loving it,

-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
flar
Offline
Joined: 2003-06-11

If the clip would be constructed once and then reused a lot, then the Area operations that Dmitri posted are the most general and safest way (but not the fastest) to construct it.

On the other hand, if all of the foreground rectangles are *known* to be non-overlapping, and if the background rectangle is *known* to encompass them all, and you need to reconstruct it pretty quickly fairly often, then a faster, though not as general, way to construct it would be to append all of the rectangles into a GeneralPath with an EvenOdd winding rule which will contain only those areas that are inside the big outer rectangle, but not inside the smaller inner rectangles (which must not overlap).

With either method, you might still find that clipped rendering is slower than painting a lot of rectangles as it involves less optimized loops (for one thing complex clips aren't as easily accelerated as simple rectangles drawn inside of rectangular clips).

How many smaller inner rectangles are we talking about?
Are some of them off the screen?
Have you tried skipping the rendering calls for the ones that are known to be off the visible part of the window?
Which calls are you using to render the rectangles? drawRect() will be *much, much* faster than draw(rectangleObject) for example.

qu0ll
Offline
Joined: 2006-12-09

Hi flar,

> If the clip would be constructed once and then reused
> a lot, then the Area operations that Dmitri posted
> are the most general and safest way (but not the
> fastest) to construct it.

It seems to be fast enough at this stage.

> On the other hand, if all of the foreground
> rectangles are *known* to be non-overlapping, and if
> the background rectangle is *known* to encompass them
> all, and you need to reconstruct it pretty quickly
> fairly often, then a faster, though not as general,
> way to construct it would be to append all of the
> rectangles into a GeneralPath with an EvenOdd winding
> rule which will contain only those areas that are
> inside the big outer rectangle, but not inside the
> smaller inner rectangles (which must not overlap).

I will look at this if I find I need the extra speed and also as a learning exercise.

> How many smaller inner rectangles are we talking
> about?

Potentially hundreds.

> Are some of them off the screen?

Potentially yes.

> Have you tried skipping the rendering calls for the
> ones that are known to be off the visible part of the
> window?

Yes.

> Which calls are you using to render the rectangles?
> drawRect() will be *much, much* faster than
> draw(rectangleObject) for example.

They are not simple Rectangles per se but rather rectangular shapes whose outline is being rendered with drawLine() calls.

--
And loving it,

-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)

qu0ll
Offline
Joined: 2006-12-09

Marking issue as resolved...

--
And loving it,

-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)

Dmitri Trembovetski

Hi,

java2d@JAVADESKTOP.ORG wrote:
> In simplified terms, I have a situation where I am drawing a large filled Rectangle and then drawing other smaller filled rectangles in front of or on top of the large one. The rectangles at the front do not completely cover the large rectangle at the back so there is a region of the back rectangle around the smaller rectangles that "shows through" so to speak.
>
> The question is, is it possible (perhaps with some form of "negative" clipping) to repaint the background colour of the large rectangle without having to repaint the smaller rectangles as well? So, just the area that shows through from the large rectangle is repainted. At the moment, I have to repaint the large rectangle and then all the smaller ones as well which is clearly too expensive.

First of all, did you determine (by profiling and such) that it
is expensive or is this your gut feeling? It is important
to work with correct data, and sometimes our
assumptions about performance implications may be
incorrect (I get this stuff wrong all the time).

Anyway, assuming that it is super-expensive to repaint
the whole thing, you can construct a clip shape
(which would be your bg rectangle minus your small
rectangles) and set it on the graphics context.
That way the area of the bg rectangle which isn't
covered by smaller rectangles only will be repainted.

Shape clip may be an expensive operation, though
(depending on the pipeline), so I would still
suggest to profile (or just time) both
approaches.

Thanks,
Dmitri

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

qu0ll
Offline
Joined: 2006-12-09

Hi Dmitri,

> First of all, did you determine (by profiling and
> such) that it is expensive or is this your gut feeling? It is
> important to work with correct data, and sometimes our
> assumptions about performance implications may be
> incorrect (I get this stuff wrong all the time).

I have not officially profiled it but it is visibly slower than I would like.

> Anyway, assuming that it is super-expensive to
> repaint the whole thing, you can construct a clip shape
> (which would be your bg rectangle minus your small
> rectangles) and set it on the graphics context.
> That way the area of the bg rectangle which isn't
> covered by smaller rectangles only will be
> repainted.
> Shape clip may be an expensive operation, though
> (depending on the pipeline), so I would still
> suggest to profile (or just time) both
> approaches.

Yes, but how do I remove areas from a clip region? I can only see how to set a clip region to a Shape and the area around the smaller rectangles is not a Shape.

--
And loving it,

-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)

Dmitri Trembovetski

java2d@JAVADESKTOP.ORG wrote:
> Hi Dmitri,
>
>> First of all, did you determine (by profiling and
>> such) that it is expensive or is this your gut feeling? It is
>> important to work with correct data, and sometimes our
>> assumptions about performance implications may be
>> incorrect (I get this stuff wrong all the time).
>
> I have not officially profiled it but it is visibly slower than I would like.

I'd still suggest to profile - you may be solving
a symptom instead of the cause.

>
>> Anyway, assuming that it is super-expensive to
>> repaint the whole thing, you can construct a clip shape
>> (which would be your bg rectangle minus your small
>> rectangles) and set it on the graphics context.
>> That way the area of the bg rectangle which isn't
>> covered by smaller rectangles only will be
>> repainted.
>> Shape clip may be an expensive operation, though
>> (depending on the pipeline), so I would still
>> suggest to profile (or just time) both
>> approaches.
>
> Yes, but how do I remove areas from a clip region? I can only see how to set a clip region to a Shape and the area around the smaller rectangles is not a Shape.
>

Something along these lines
Area clip = new Area(new Rectangle(0,0,largeRectW,largeRectH));
clip.subtract(new Area(new Rectangle(,,,)));
g2d.clip(clip);

Thanks,
Dmitri

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

qu0ll
Offline
Joined: 2006-12-09

Thanks Dmitri - that works a treat.

--
And loving it,

-Q
_________________________________________________
Qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email me)