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

2011/9/11

Image

1
2
3
Canning Canning

2011/9/11

#
I am having trouble drawing text and graphics in my game. Basically, I am wanting to display a score in the top left corner and a filed rectangle to be a health bar. How should I go about this? thanks
GameCode GameCode

2011/9/11

#
For writing texts use this:
[code]public class HealthBar extends Actor
{
    public HealthBar()
    {
        setImage("health.jpg"); // Draw a red image. Size doesn't matter. Name it health.jpg and put it into the image ordner
    }
    public void showHealth(int health) // health from 0 - 50
    {
        getImage().scale(health, 30);
    }
}
} For a health bar use this:
GameCode GameCode

2011/9/11

#
[code]public class TextField extends Actor  
{  
  public GreenfootImage label = new GreenfootImage(100,100);
    
    public Label()
    {
        setImage(label);
        
    }
    public void setText(String text)  
    {  
        label.drawString(  text,0,20);
    }  
}  
GameCode GameCode

2011/9/11

#
upps
public void setText(String text)    
{    
    label.clear();
    label.drawString(  text,0,20);  
}    
, I forgot to clear the image:
Canning Canning

2011/9/12

#
About the health bar, can you help me with some code to do it via drawing a filled rectangle rather than an image? thanks
danpost danpost

2011/9/12

#
No matter how you go about it, if it is to be shown within your world, it will have to use an image. You might actually want to create TWO images -- the first one is for the health bar itself (which never changes); the second one can be size-dependant on the percentage of total health; the second image can then be drawn onto the first image and this, the final image, can be set as the image of your health bar class object. The code for your HealthBar class might look something like this:
import greenfoot.*;
import java.awt.Color;
import java.awt.color.*;

public class HealthBar extends Actor
{
    private final int maxValue = 200; // whatever max value you desire
    private int barValue = 200;
    private int lastValue = 0;

    public HealthBar()
    {
    }

    public void act()
    {
        if (barValue != lastValue)
        {
            lastValue = barValue;
            int pctHealth = (int) (200 * barValue / maxValue); // 200 is maxPixelHealth
            // Create thermometer -- sorta speak
            GreenfootImage imgOne = new GreenfootImage(206,12);
            imgOne.setColor(Color.CYAN);
            imgOne.fill();
            imgOne.setColor(Color.BLUE);
            imgOne.drawRect(2,2,202,8);
            // Add mercury, if there is any temperature -- sorta speak
            if (pctHealth != 0)
            {
                GreenfootImage imgTwo = new GreenfootImage(pctHealth,6); 
                imgTwo.setColor(Color.RED);
                imgTwo.fill(); // Completes the second image
                imgOne.drawImage(imgTwo,3,3); // Puts mercury into the thermometer
            }
            // imgOne.scale(myX, myY); // Dimensions myX and myY are whatever size you wish to make it
            imgOne.setTransparency(128); // Adjust value as wanted or delete statement for no transparency
            setImage(imgOne);
        }
    }

    public void chgHealth(int chgValue)
    {
        barValue += chgValue;
        if (barValue > maxValue) barValue = maxValue;
        if (barValue < 0) barValue = 0;
    }

    public int getHealth()
    {
        return barValue;
    }
}
I throw this together right on the Post a reply screen, so no syntax was checked and there might be other minor errors. If you need help, post another reply. (Edited 23 hours after original posting) Tested and corrected and runs properly. Update might include adding text 'Health' on the left side and the numeric value on the right.
Canning Canning

2011/9/14

#
I am playing around with drawing my score at the top left of the screen. Here is my code:
public class HUD extends Actor
{    
    private GreenfootImage label = new GreenfootImage(200,200);  
        
    public void setText(String text)      
    {      
        setImage(label);
        label.clear();  
        label.drawString(text,0,20); 
        setImage(label);
    }    
}
When I call my setText method, I can't see any text being displayed. Am I doing something wrong? thanks
kiarocks kiarocks

2011/9/14

#
where and how do you call it? Also it is unnecessary to set the image the first time.
AwesomeNameGuy AwesomeNameGuy

2011/9/14

#
The code to setText should work. The only thing I can think of is maybe you should make the image smaller. If you have it in the top left corner of your screen, the image itself may extend beyond the border of your world, and the program might be trying to draw the text off of the screen.
Canning Canning

2011/9/15

#
OK, got it working basically. Here is my code:
public class HUD extends Actor
{

    private GreenfootImage imageScore = new GreenfootImage(120, 24);

    public HUD()
    {
        setImage(imageScore);
        Font font = imageScore.getFont();
        imageScore.setFont(font.deriveFont(24.0F));  // use larger font   
    }
    
    public void act() 
    {
        CarWorld w = (CarWorld) getWorld();
        Car aCar = w.getCar();
        
        imageScore.clear();
        imageScore.drawString("Score: " + aCar.getScore(), 1, 18);  
    }    
}
Am I correct in saying this creates a blank image 120 units wide and 24 units high? My question is this, how can i position this image to be in a different place in my world, but still retain its size? thanks
danpost danpost

2011/9/15

#
Just use the 'setLocation(int xValue, int yValue);' statement. This is an 'Actor' class method.
kiarocks kiarocks

2011/10/31

#
sorry to reactivate an old post, but i am making a level bar and i cant figure out how to get this code to work:
int exp = 100;
int nextlvlat = 150;
...
...
...
...
public void act()
{
    int value1 = (int) nextlvlat/exp;
GreenfootImage img1 = new GreenfootImage(206,6);
        img1.setColor(Color.RED);
        img1.fill();
        if(value1 > 0)
        {
            GreenfootImage img2 = new GreenfootImage(value1,6);
            img2.setColor(Color.green);
            img2.fill();
            img1.drawImage(img2,0,0);
        }
        setImage(img1);
}
kiarocks kiarocks

2011/10/31

#
and i know that this results in 1 for value1.
danpost danpost

2011/11/1

#
Try setting 'value1' in the 'img2' declaration statement to a specific number, say '50', and double check to see if it works.
kiarocks kiarocks

2011/11/1

#
this is what i get:
There are more replies on the next page.
1
2
3