Java: Would using setClip in Graphics be more efficient?

Go To StackoverFlow.com

4

I have an off screen BufferedImage that too large to be displayed all at once. So I'm using getSubimage to draw part of the entire image. I'm wondering if using setClip on the Graphics would use less resources. The docs say that "Rendering operations have no effect outside of the clipping area". How much of a difference would there be if I used a clip verses not using a clip?

2012-04-05 17:57
by Croolsby


3

When you set a clip on a Graphics object – for example a Rectangle(0, 0, 100, 100) – and then draw an image with that same Graphics object – for example an image sized 300x300 – the part of the image that lies outside of the clip is omitted. Only the top left 100x100 corner of the example would be drawn.

The main advantage to this is, that it speeds up the drawing process a lot. I'm not quite sure about how it makes it faster though; as a matter of fact I was looking this up right now.

In any case, I have found this to be very handy in designing full screen games. The more efficient you make your clips that draw to the screen, the smoother your game runs. When nothing is updating the screen components for a while, the clip stays empty, drawing nothing and leaving resources for game logic.

2012-08-03 22:51
by Pino
One more tip: an Area object can also be used as a clip. Areas can be added, subtracted, intersected, etc. This way you can build your clip any way you like - Pino 2012-08-03 22:55
Does anyone know if the speed-up is a function of the area of the clip or the area of its bounding box? (Thinking about drawImage(..). - Evgeni Sergeev 2013-05-13 08:23
Ads