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

2014/2/26

Circular Health Bar

Greenhorn Greenhorn

2014/2/26

#
Hello, I am trying to draw a sector of a circle (something similar to Turing where you can specify the degrees of the circle/oval that should be filled). Is it possible to do the same thing on Greenfoot?
danpost danpost

2014/2/26

#
Pretty much anything is possible with the right coding. I came up with this for a circular health bar class (I kind of fancied it up a bit):
import greenfoot.*;

public class CircularHB extends Actor
{
    int maximum = 100, health = maximum;
    
    public CircularHB()
    {
        updateImage();
    }

    public void adjustHealth(int adjustment)
    {
        health += adjustment;
        if (health < 0) health = 0;
        if (health > maximum) health = maximum;
        updateImage();
    }
    
    private void updateImage()
    {
        GreenfootImage base = new GreenfootImage(150, 150);
        base.setColor(java.awt.Color.green);
        if (getWorld() == null) base.fillOval(0, 0, 149, 149);
        else
        {
            int lastX = 75, lastY = 0;
            for(int rot=0; rot<health*360/maximum; rot++)
            {
                Actor actor = new Actor(){};
                getWorld().addObject(actor, getX(), getY());
                actor.setRotation(90+rot);
                actor.move(74);
                int dx = actor.getX()-getX(), dy = actor.getY()-getY();
                getWorld().removeObject(actor);
                base.fillPolygon(new int[] { 75, lastX, 75+dx }, new int[] { 75, lastY, 75+dy }, 3);
                lastX = 75+dx;
                lastY = 75+dy;
            }
        }
        GreenfootImage text = new GreenfootImage(""+health, 24, java.awt.Color.black, null);
        base.drawImage(text, 75-text.getWidth()/2, 75-text.getHeight()/2);
        base.setColor(java.awt.Color.black);
        for (int i=0; i<3; i++) base.drawOval(i, i, 149-2*i, 149-2*i);
        base.drawLine(75, 120, 75, 150);
        base.drawLine(0, 75, 30, 75);
        base.drawLine(75, 0, 75, 30);
        base.drawLine(120, 75, 150, 75);
        setImage(base);
    }
}
danpost danpost

2014/2/26

#
I made it easier to change its size (I realized it was a bit large). The final code follows:
import greenfoot.*;

public class CircularHB extends Actor
{
    int size = 100;
    int maximum = 100, health = maximum;
    
    public CircularHB()
    {
        updateImage();
    }

    public void adjustHealth(int adjustment)
    {
        health += adjustment;
        if (health < 0) health = 0;
        if (health > maximum) health = maximum;
        updateImage();
    }
    
    private void updateImage()
    {
        GreenfootImage base = new GreenfootImage(size, size);
        base.setColor(java.awt.Color.green);
        if (getWorld() == null) base.fillOval(0, 0, size-1, size-1);
        else
        {
            int lastX = size/2, lastY = 0;
            Actor actor = new Actor(){};
            getWorld().addObject(actor, getX(), getY());
            for(int rot=0; rot<health*360/maximum; rot++)
            {
                actor.setRotation(90+rot);
                actor.move(size/2-1);
                int dx = actor.getX()-getX(), dy = actor.getY()-getY();
                actor.setLocation(getX(), getY());
                base.fillPolygon(new int[] { size/2, lastX, size/2+dx }, new int[] { size/2, lastY, size/2+dy }, 3);
                lastX = size/2+dx;
                lastY = size/2+dy;
            }
            getWorld().removeObject(actor);
        }
        GreenfootImage text = new GreenfootImage(""+health, 24, java.awt.Color.black, null);
        base.drawImage(text, size/2-text.getWidth()/2, size/2-text.getHeight()/2);
        base.setColor(java.awt.Color.black);
        for (int i=0; i<3; i++) base.drawOval(i, i, size-1-2*i, size-1-2*i);
        base.drawLine(size/2, size*4/5, size/2, size);
        base.drawLine(0, size/2, size/5, size/2);
        base.drawLine(size/2, 0, size/2, size/5);
        base.drawLine(size*4/5, size/2, size, size/2);
        setImage(base);
    }
}
Also, I only add and remove the helper actor one time (instead of multiple times) each time the image is updated.
You need to login to post a reply.