Crop a triangle out of an image

Hi, I am trying to crop a triangle out of an image. I have seen methods to crop rectangles, but that is not what I want, I want to be able to crop a triangle. Is there a method I can use for this? I am using JAI, and all they seem to have are methods such as crop(rectangle r), or something like getSubImage(rectangle r). Thanks for your help!

[351 byte] By [jamapiro] at [2007-9-30 18:02:33]
# 1
the Graphics2D class has a clip(Shape) method. The Polygon class implements Shape, so you could set up a triangle with Polygon and then clip it through Graphics2D.the downside to this is, you won't get an image object out of this, it'll just be drawn on the screen
Woogley at 2007-7-6 18:32:36 > top of Java-index,Other Topics,Java Game Development...
# 2
It sounds like you want crop, not clip. In that case, I don't think it's possible. Never seen a triangular image in my life. Doesn't exist. You'd have to have a rectangular image and then map it to a polygon.
wbracken at 2007-7-6 18:32:36 > top of Java-index,Other Topics,Java Game Development...
# 3

Woogley was correct. The cropped piece has to go somewhere, so if you create a BufferedImage of a type that has an alpha component, set the Clip as Woogley described, then draw your image that you wish to crop on top of that, the result will be a cropped image. To re-use it later, you can draw that image onto other sources, and all space that isn't part of your triangle will be completely transparent as it should be.

Malohkan at 2007-7-6 18:32:36 > top of Java-index,Other Topics,Java Game Development...
# 4
Thanks Malohkan, but the answer is how can I do this. Once I have the triangle, i need to manipulate it(duplicate it, rotate it, translate it,...) on the same canvas.
jamapiro at 2007-7-6 18:32:36 > top of Java-index,Other Topics,Java Game Development...
# 5

Area a = new Area(myTriangle);

AffineTransform at = new AffineTransform();

at.translate(x, y);

at.rotate(Math.PI/2, -width/2, -height/2); //to rotate about the center, I think that's how you do it

a.transform(at);

Malohkan at 2007-7-6 18:32:36 > top of Java-index,Other Topics,Java Game Development...
# 6

ahh wait, that'll manipulate the shape, but not the image, sorry!

take your AffineTransform object, and when you go to draw your cropped triangle, which is now a BufferedImage, call:

graphics2d.drawImage(croppedImage, at, null);

and it'll be drawn transformed in any way that the AffineTransform has been set to transform

Malohkan at 2007-7-6 18:32:36 > top of Java-index,Other Topics,Java Game Development...
# 7
Thanks Malohkan, but now the problem is that the shape of the image that isn't part of the triangle covers any image drawn in the canvas. I need a lot of triangles together
jamapiro at 2007-7-6 18:32:36 > top of Java-index,Other Topics,Java Game Development...
# 8

Create a translucent BufferedImage, set the Clip to your desired shape, draw the origional image on top of it. If you want multiple triangles as your clip, use the Area class. Create a new Area(new Polygon(stuff for triangle)), and if you want to add shapes, use the Area.add(Area a) method. Area implements Shape. The Graphics2D.setClip(Shape shape) method should handle that. If not, you'll have to apply some real thinking to come up with a smarter method to handle what you want to do.

If you meant by your problem that you're getting white space outside the trangle drawn, that means your BufferedImage isn't translucent like I said it should be.

Malohkan at 2007-7-6 18:32:36 > top of Java-index,Other Topics,Java Game Development...
# 9

Another possible problem you may be facing, as described in the 1.4.2 Javadoc for Graphics2D.setClip(Shape shape):

"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."

Malohkan at 2007-7-6 18:32:36 > top of Java-index,Other Topics,Java Game Development...
# 10
Thanks a lot Malohkan, my BufferedImage wasn't translucent. Now it works.
jamapiro at 2007-7-6 18:32:36 > top of Java-index,Other Topics,Java Game Development...