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

2013/10/6

Shaped image drawing

Zamoht Zamoht

2013/10/6

#
Is there a way to draw an image shaped as a circle? For instance if I wanted to the userinfo image in a circle frame. I could use the setColorAt method from GreenfootImage to change all the pixels out of the circle to a transparent color, but I was just wondering if there was an easier way.
You can use GreenfootImage's drawOval & fillOval methods.
GreenfootImage img = new GreenfootImage(height, width);

img.setColor(color);

img.drawOval(0,0,size,size); //Will just draw an outline

img.fillOval(0,0,size,size); //Will fill the whole thing.
You can find all these methods and more at the Greenfoot JavaDocs
Gevater_Tod4711 Gevater_Tod4711

2013/10/6

#
It might be a bit easier if you create a new Greenfoot image with the same size as the image you want to have as a circle, draw a circle onto the transparent image and then just copy the single pixel colors from one image to another when the copied image is not transparent at the current checked pixel. That would be easier than setting the pixels outside the circle to transparent color. But I think a realy simple way for this doesn't exist. You only can draw the whole images. Not only a circle of it.
Zamoht Zamoht

2013/10/6

#
FlyingRabidUnicornPig wrote...
You can use GreenfootImage's drawOval & fillOval methods.
I know that. My question was how to draw an image but only draw a bit of the square image so it would end up being shaped as a circle. For instance if I wanted to draw the userinfo image as a circle shape.
Zamoht Zamoht

2013/10/6

#
Oh okay so I could write something like this for every pixel:
GreenfootImage image = UserInfo.getMyInfo().getUserImage();
GreenfootImage frame = new GreenfootImage(image);
frame.clear;
frame.setColor(anyColor);
frame.fillOval(x, y, width, height);//Where I want the circle
//Then create a loop for the code below to check all pixels
Color imageColor = image.getColorAt(x, y);
Color color = new Color(imageColor.getRed(), imageColor.getGreen(), imageColor.getBlue(), frame.getColorAt(x, y).getAlpha());
image.setColorAt(x, y, color);
I may have made some mistakes above since I wrote it here on the site, but I just want to know if the method will work.
Gevater_Tod4711 Gevater_Tod4711

2013/10/6

#
I think in line 2 you could use: ... = new GreenfootImage(image.getWidth(), image.getHeight()); And I think you don't need to create a new Color object. That would make a better runtime but your version should also work.
Zamoht Zamoht

2013/10/6

#
Okay thank you. I was just wandering if there was an easier way, but since Greenfoot is pretty basic I guess I would have to work with the BufferedImage (or Graphics2D) to find a simpler yet less basic way to go around it.
Zamoht Zamoht

2013/10/6

#
I found this great answer on stackoverflow and rewrote it a little for Greenfoot.
    public DrawingWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        
        GreenfootImage image = new GreenfootImage("brick.jpg");
        BufferedImage awtImage = (new GreenfootImage(image)).getAwtImage();
        int w = image.getWidth();
        int h = image.getHeight();
        Graphics2D graphics = image.getAwtImage().createGraphics();
        image.clear();
        graphics.setComposite(AlphaComposite.Src);
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setColor(Color.WHITE);
        graphics.fill(new Ellipse2D.Float(0, 0, w, h));
        graphics.setComposite(AlphaComposite.SrcAtop);
        graphics.drawImage(awtImage, 0, 0, null);
        graphics.dispose();
        setBackground(image);
    }
http://i.imgur.com/LakwlO9.png?1 Not pretty but maybe it can be used for other stuff.-
You need to login to post a reply.