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

2013/4/9

counter

GrimCreeper312 GrimCreeper312

2013/4/9

#
what would be the code to make a counter class that displays the amount of money the ship has without having a back ground, and the numbers are light blue, something like the counter you can import from greenfoot but its text color can be changed and there isnt a background image
danpost danpost

2013/4/9

#
If you want to be able to change the text color of the counter, you will need an instance field to hold the color of the text, a method that will allow another color to be set as a new text color and you will need to reference that field when creating the text image. For the transparent background, just use 'new Color(0, 0, 0, 0)' for the last parameter when creating the GreenfootImage object that will be set as the image of the counter object. For light blue, you could use 'Color.cyan'. The class code could be something like this:
import greenfoot.*;
import java.awt.Color;

public class Counter extends Actor
{
    private static final Color TRANSPARENT = new Color(0, 0, 0, 0);
    private int value;
    private Color textColor = Color.cyan;

    public Counter()
    {
        updateImage();
    }

    // to create an updated image for the counter object
    private void updateImage()
    {
        int fontSize = 24; // adjust to suit
        setImage(new GreenfootImage(""+value, fontSize, textColor, TRANSPARENT));
    }

    // to change the color of the text
    public void setTextColor(Color color)
    {
        textColor = color;
        updateImage();
    }

    // to add (or subtract) an amount from its value
    public void add(int amount)
    {
        value += amount;
        updateImage();
    }

    // to set a new value for the counter
    public void setValue(int newVal)
    {
        value = newVal;
        updateImage();
    }

    // to allow access to the value from other classes
    public int getValue()
    {
        return value;
    }
}
I could have shortened this a little, but wanted you to see what was happening. Such as, I did not need to add the static field and placed the Color value itself in the code; and I did not need to declare the int 'fontSize' in the code and used its value in the next line, also.
GrimCreeper312 GrimCreeper312

2013/5/10

#
What code would i add to add a prefix to the counter
danpost danpost

2013/5/10

#
You can add a secondary constructor, adjust the 'updateImage' method and, if you want (or need) to, add a method to change (or set) the prefix. You will also have to add an instance String field to hold the prefix.
// add the instance field
String prefix = "";

// add the secondary constructor
public Counter(String text)
{
    prefix = text;
    updateImage();
}

// change line 19 above to
setImage(new GreenfootImage(prefix+": "+value, fontSize, textColor, TRANSPARENT));

// possibly add this method
public void setPrefix(String text)
{
    prefix = text;
    updateImage();
}
You need to login to post a reply.