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:
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!
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());"
}
}

