How to get the clip rectangle of a Graphics object

I hope more people start to post here soon as I feel a litte bit embarrased by posting three questions in a row… :-/

Is there any way of getting the clipping rectangle of the Graphics object passed to my method? And I don’t mean the user specified clipping rectangle (which would be null since I haven’t set any). I need to know this because I’m drawing a complete tile map to the screen and since it will be much larger than the window most of it will be clipped any way and thus result in unnecesary drawing.

Thanks,

Johan Tibell

interesting question…

I didn’t know the answer either, so I had a quick look at the api.

I belive what your after is this :-


public Rectangle getBounds(Graphics2D g2d)
{
   return g2d.getDeviceConfiguration().getBounds();
}

Something like this?

public void render(Graphics g)
{
Rectangle oldClip = g.getClipBounds();
g.setClip(left, top, width,height);
. . . draw stuff . . .
g.setClip(oldClip);
}

  • Craig

So if the Graphics object was sent from say a Canvas the getBounds would give the Canvas clipping rectangle?

Thanks,

Johan Tibell

[quote]So if the Graphics object was sent from say a Canvas the getBounds would give the Canvas clipping rectangle?

Thanks,

Johan Tibell
[/quote]
Have you tried either method?

Mattocks solution requires you to know the bounds of the object from which the Graphics context was derived anyway, so it doesn’t realy ‘solve’ your problem.

I don’t know whether what I suggested will work either… I havn’t tested it.