Hi again.
With Java2D there is the possibility to just draw a part of the screen, f.e. an oval by using


What I need now is something that does clipping to the rest of the screen.
Something like an setContraClip(shape);
which leaves the shape out and only draws to other parts of the screen.

Is this possible with Java2D or does anyone know a simple solution to implement
this myself?

I hope this help you

                     [url=][url removed][/url]

      is all about 2d graphics  ;) clipping, grandients, tiles, textures, fonts, graphics, shapes, all 2d graphics examples

Don’t link copyrighted material, please. / Markus

Big THANKS for this one.
I hope I find the answer in there, if not it might not exist.
This is an awesome pdf about Graphics2D.
I post again when I find the answer.

So here´s the answer:

there is something called Area in java which
can add or subtract Shapes.

So it would look like this:

Graphics2D g2;
Rectangle rec = new Rectangle(0,0,1280,1024);
Ellipse2D.Double ellipse = new Ellipse2D.Double(x1-240,y1-240,560,560;
Area area = new Area();

…something to do within the bounds of my new Shape…

It´s from the great Java_2D_Graphics -pdf
I read most of what I need right now…the rest will follow :wink:
Thanks again :slight_smile:

The problem with this approach is that it’s not incredibly fast and you can’t do something like make an Area masked by an image. There’s little leeway.

If you want image manipulation, you should have a go at Raster and BufferedImage. Then you can just erase the pixels as you feel.

Right, I got something wrong:

Graphics2D g2;
Rectangle rec = new Rectangle(0,0,1280,1024);
Ellipse2D.Double ellipse = new Ellipse2D.Double(x1-240,y1-240,560,560;
Area area = new Area(rec);
area.subtract(new Area(ellipse));

This would draw an Image on the screen but only in the part thats not in ellipse.
It worked fine for now, my FPS didn´t decrease measureable.

I stand corrected. It was on this very forum people told me you can’t do that. :stuck_out_tongue:

But in any case, Area operations are costly. If you did around 100 operations, you would most definitely notice slowdown on any computer. You can always just do this to be absolutely clear how much time you’re using:
long temp = System.currentTimeMillis();
//insertt Area stuff here
System.out.println(System.currentTimeMillis() - temp);

It will help you diagnose the speed capabilities. Obviously just one operation won’t hit performance too much, I’m just saying that it’s pretty costly.

Well with more Area actions it really is expensive concerning time…you were right :-[
But what else should I do?
GeneralPath doesn´t help with this Problem either and I just want it done in Java2D…

Well I’m not sure the exact nature of your project, so I can’t give the best recommendation. How often are you doing this clipping? Would it be possible to do the clipping once and then save an instantiation of the Area object or the clipping Graphics context?

One more thing to add - taken from the javadoc…

public abstract void setClip(Shape clip)

Sets the current clipping area to an arbitrary clip shape. Not all objects that implement the Shape interface can be used to set the clip. The only Shape objects that are guaranteed to be supported are Shape objects that are obtained via the getClip method and via Rectangle objects. This method sets the user clip, which is independent of the clipping associated with device bounds and window visibility.

Maybe it’s just me, but that really is a fuckwitted bit of software engineering isn’t it? Building a function that is expressly going to fail when given certain arguments. I bet it doesn’t even throw an exception.

Cas :slight_smile:


I agree. I typically stay away from Java2D now, because a lot of stuff seems to be that way.