This site requires JavaScript, please enable it in your browser!
Greenfoot back
mattrayner
mattrayner wrote ...

2013/2/24

Loading an image within a circle

mattrayner mattrayner

2013/2/24

#
Hi guys, I'm working on a project whereby I need to load an image inside of a circle with a stoke, I was just wondering if anyone could help me out? This is what I mean: Thanks, Matt
Gevater_Tod4711 Gevater_Tod4711

2013/2/24

#
The only way I can imagine is that you create a new image with the same size as the image you want to load, fill this image with white, draw a black circle into it and then you check for every point of the image whether there is black or white. If there is white the color is set to transparency (new Color(0, 0, 0 ,0)) and if it's black the color at the point is set to the color the other image has at this point. But this would take many resources. It would be eayser just to cut the image using photoshop or some other programms.
danpost danpost

2013/2/24

#
As an alternative: after creating the new transparent image, instead of adding and checking for color at each point, just check the distance each point is from the center. If in the specified range (the radius of the circle), set the point to the color of the image at that point. Once this is done, draw the circle with the stoke onto this image (assuming that that image is transparent within the circle, except for the stoke) and set this image as the image of the actor.
robg robg

2013/2/25

#
For anyone that comes across this looking for the same thing, take a look at our solution here -> Image within a circle Solution Outline - Created a new BufferedImage & got the Graphics2D object (The same size as the orignal image - Drew the Elipse that we wished to crop by. (In white) - Changed the Composite of the image to AlphaComposite.SrcAtop to allow us to clip any further drawing to the elipse. - Drew the original BufferedImage onto the new one Make sure to use the correct type when creating the BufferedImage, if you don't use TYPE_INT_ARGB to allow alpha it won't mask the image! Regards, Rob
You need to login to post a reply.