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

2017/4/17

I need a little (maybe a lot) of help

Wasupmacuz Wasupmacuz

2017/4/17

#
I've been trying to make a rectangle appear as an actor's image but all I can figure out is putting a rectangle on the background...what I'm trying to do is probably way passed what I am capable of doing, however I still want it to work. the code that has no errors and creates the rectangle on the background is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.Color;
/**
 * Write a description of class StatBar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class StatBar extends Actor
{
    private String[] vars={"energy","health",""};
    private String var;
    private int barLength;
    /**
     * makes a bar for a specific statistic
     */
    public StatBar(int id)
    {
        var=vars[id];
    }
    
    /**
     * Act - do whatever the StatBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        BufferedImage img = getWorld().getBackground().getAwtImage();
        Graphics2D g=img.createGraphics();
        g.clearRect(150,200,barLength*3,4);
        if(var=="energy")
        {
            barLength=(int)Planet.energy;
        }else if(var=="health")
        {
            barLength=(int)Planet.health;
        }else if(var=="")
        {
            
        }
        g.setColor(Color.green);
        g.fillRect(150,200,barLength*3,4);
        // I also tried "setImage(g.toString());"
    }    
}
However, I would like the image set as the actor's image so I can use the world's setPaintOrder() method to paint it in front of other actors. Any help is welcome!
danpost danpost

2017/4/17

#
Wasupmacuz wrote...
I've been trying to make a rectangle appear as an actor's image
One thing that would make things a lot easier is using the GreenfootImage class instead of using the 'BufferedImage' and 'Graphics2D' classes. As such, the code above can be re-written as follows:
import greenfoot.*;
import java.awt.Color;

public class StatBar extends Actor
{
    private String[] vars={ "energy", "health" };
    private String var;
    private int barLength;

    public StatBar(int id)
    {
        var=vars[id];
    }
     
    public void act() 
    {
        GreenfootImage img = getWorld().getBackground();
        img.setColor(Color.white);
        img.fillRect(150,200, barLength*3 ,4);
        if ("energy".equals(var)) barLength=(int)Planet.energy;
        else if ("health".equals(var)) barLength=(int)Planet.health;
        img.setColor(Color.green);
        img.fillRect(150,200, barLength*3, 4);
    }    
}
Of course, all the StatBar objects will still compete for the same area of the background image with this; and, like you said, it would be nice to be able to paint these bars in a specific order with respect to other actors. The only real difference between using the world background and the image of an actor is that the background of the world already exists and is set to the world, where you will have to create the image for the actor and set it to the actor. Use:
GreenfootImage img = new GreenfootImage((int)Planet.maxHealth*3, 4);
to create the image. Fill the same green rectangle at (0, 0) instead of (150, 200) and set the image to the actor using:
setImage(img);
I used '(int)Planet.maxHealth' as an example and for what might go there. You will have to correct the code for what you actually have as well as for the energy bar.
Wasupmacuz Wasupmacuz

2017/4/18

#
danpost wrote...
Wasupmacuz wrote...
I've been trying to make a rectangle appear as an actor's image
One thing that would make things a lot easier is using the GreenfootImage class instead of using the 'BufferedImage' and 'Graphics2D' classes. As such, the code above can be re-written as follows:
import greenfoot.*;
import java.awt.Color;

public class StatBar extends Actor
{
    private String[] vars={ "energy", "health" };
    private String var;
    private int barLength;

    public StatBar(int id)
    {
        var=vars[id];
    }
     
    public void act() 
    {
        GreenfootImage img = getWorld().getBackground();
        img.setColor(Color.white);
        img.fillRect(150,200, barLength*3 ,4);
        if ("energy".equals(var)) barLength=(int)Planet.energy;
        else if ("health".equals(var)) barLength=(int)Planet.health;
        img.setColor(Color.green);
        img.fillRect(150,200, barLength*3, 4);
    }    
}
Of course, all the StatBar objects will still compete for the same area of the background image with this; and, like you said, it would be nice to be able to paint these bars in a specific order with respect to other actors. The only real difference between using the world background and the image of an actor is that the background of the world already exists and is set to the world, where you will have to create the image for the actor and set it to the actor. Use:
GreenfootImage img = new GreenfootImage((int)Planet.maxHealth*3, 4);
to create the image. Fill the same green rectangle at (0, 0) instead of (150, 200) and set the image to the actor using:
setImage(img);
I used '(int)Planet.maxHealth' as an example and for what might go there. You will have to correct the code for what you actually have as well as for the energy bar.
I'll try thanks Dan.
You need to login to post a reply.