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

2013/7/8

I need help with strings

Alby Alby

2013/7/8

#
So, I'm still a pretty new programmer and I need help getting the drawstring method to draw a string that says a variable from another class. In this case a seal class defines a variable as points(shown below) and I need to bring the variable to the background class and and use it in a string. Here's what I have so far... Variable declaration code...
    public static int points;
String drawing code...
    public void showPoints()
    {
        GreenfootImage bg = getBackground();
        bg.setColor(Color.BLACK);
        bg.drawstring("Points: " Seal.points, 78, 37);
    }
The only import statements are for color and the default greenfoot one. The creation statement doesn't import any variables(or at least not the points variable). So, just to sum things up, that's my code and I want it to say "Points: (Whatever the points variable will be equal to)" Thank you and good luck :)
If you want to add a number onto a string, you have to do this:
int number = 10;
String str = "String" + number; //Will return "String10"
Make sure you have that "+" symbol
Also, the method is drawString(), not drawstring()
Alby Alby

2013/7/9

#
yeah thanx ill test it!!!
Alby Alby

2013/7/9

#
Ok,so I have that done now the problem is that it doesn't erase the old string, so please help with that :P
Alby Alby

2013/7/9

#
ok...
danpost danpost

2013/7/9

#
If you insist on having the string drawn on the background image, then your options are limited to what your background image is. If it is a flat color, then you can redraw the string after setting the drawing color to that of the background image before drawing the new string; if not, then you will need to redraw the background image before drawing the new string. Better, however, is to use the image of an actor to draw the string on. A basic Text actor class or a basic Counter actor class can be used for this purpose. The difference between the two is where the int score field is located. The Counter class will have the counter (or score) located within itself, where the Text class will have the counter (or score) value passed to it within the string to display. A basic Text class follows:
import greenfoot.*;
import java.awt.Color;

public class Text extends Actor
{
    public Text(String text)
    {
        setText(text);
    }

    public void setText(String text)
    {
        setImage(new GreenfootImage(text, 20, Color.black, new Color(0, 0, 0, 0)));
    }
}
// created from outside the Text class with the following:
// (remove 'getWorld().' if in world class and x and y should be int values)
Text scoreText = new Text("Score: "+score);
getWorld().addObject(scoreText, x, y);
// updated with
score += addAmount; // addAmount should be some int value
scoreText.setText("Score: "+score);
For a Counter class (an int 'score' field should not be used anywhere)
import greenfoot.*;
import java.awt.Color;

public class Counter extends Actor
{
    private String prefix;
    private int value;

    public Counter(String text)
    {
        prefix = text;
        updateImage();
    }

    private void updateImage()
    {
        setImage(new GreenfootImage(prefix+value, 20, Color.black, new Color(0, 0, 0, 0)));
    }

    public void add(int addAmount)
    {
        value += addAmount;
        updateImage();
    }

    public void setValue(int newValue)
    {
        value = newValue;
        updaeImage();
    }

    public int getValue()
    {
        return value;
    }
}
// created with
Counter scoreCounter = new Counter("Score: ");
getWorld().addObject(scoreCounter, x, y);
// updated with
scoreCounter.add(addAmount);
// or
scoreCounter.setValue(scoreCounter.getValue()+addAmount);
You need to login to post a reply.