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

2017/11/28

How to fill java.awt.geom shape in GreenfootImage?

CubeGamer6 CubeGamer6

2017/11/28

#
I've been trying to make rounded rectangles with the RoundRectange2D.Float class, but is there any way to fill this shape on a GreenfootImage, just like how you do image.fillRect(...) ? I know there is a drawShape(java.awt.Shape) method, but i thought it would be too messy to use a for loop to draw progressively smaller shapes in order to make it solid. Here is how i currently draw my RoundRectangle2D.Float:
1
2
RoundRectangle2D.Float rectangle=new RoundRectangle2D.Float(0, 0, 50, 20, 5, 5);
image.drawShape(rectangle);
danpost danpost

2017/11/28

#
Check out the 'fillPolygon' method of the GreenfootImage class.
CubeGamer6 CubeGamer6

2017/11/28

#
danpost wrote...
Check out the 'fillPolygon' method of the GreenfootImage class.
Thanks for the reply, but i think in order for that to work, i need to be able to turn RoundRectangle2d.Float into arrays of x and y coördinates. The RoundRectangle2D.Float class doesn't have this. I have however found a way to fill it in though. All i needed to do was add ovals to the 4 corners and add two squares in a cross pattern.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public GreenfootImage fillRoundRectangle(RoundRectangle2D.Float rect, Color color)
    {
        GreenfootImage image=new GreenfootImage((int)(rect.width+rect.x+0.5f), (int)(rect.height+rect.y+0.5f));
        int arcX=(int)rect.arcwidth;
        int arcY=(int)rect.archeight;
        int width=(int)rect.width;
        int height=(int)rect.height;
        int x=(int)rect.x;
        int y=(int)rect.y;
        image.setColor(color);
        image.fillOval(x, y, arcX, arcY);
        image.fillOval(x+width-arcX, y, arcX, arcY);
        image.fillOval(x, y+height-arcY, arcX, arcY);
        image.fillOval(x+width-arcX, y+height-arcY, arcX, arcY);
        image.fillRect(x+arcX/2, y, x+width-arcX, y+height);
        image.fillRect(x, y+arcY/2, x+width, y+height-arcY);
        return image;
    }
You need to login to post a reply.