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

2012/8/27

Simple Character Box

Barador Barador

2012/8/27

#
Hi! Has anyone an idea for a simple box to input numbers? It just has to set an integer so the Program can check if a correct answer was put in.
danpost danpost

2012/8/28

#
You need to create a new sub-class of Actor for the simple box (name it 'NumberInputBox'). Next, declare a 'String' field to hold the text of the number and set it to an empty string. Now, you need to create the image of the object. This should be done in a seperate method and called from the constructor to initialize the image, and from the act() method, when it picks up any changes to the text. The act method should check for valid keystrokes and perform the neccessary processes for each one. When the 'enter' key is pressed, the value needs to be available for your world to use. That would be a good job for a 'static int' variable (class field). The value is set and the object is removed. A boolean in the world can track if you are in the process of getting numeric input from the class. Set it to 'true' when you create the object. In the world act method, check if getting input and the object in no longer in the world, the class field can be accessed. My (not so simple -- the image is dressed up a little) code for the class follows:
import greenfoot.*;
import java.awt.Color;

public class NumberInputBox extends Actor
{
    static int value = 0;
    String text = "";
    
    public NumberInputBox()
    {
        updateImage();
    }
    
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(150, 30);
        image.setColor(new Color(128, 0, 0));
        image.fill();
        image.setColor(Color.lightGray);
        image.fillRect(3, 3, 144, 24);
        GreenfootImage numImage = new GreenfootImage(" " + text + " ", 24, Color.black, Color.lightGray);
        image.drawImage(numImage, 75 - numImage.getWidth() / 2, 15 - numImage.getHeight() / 2);
        setImage(image);
    }

    public void act()
    {
        String key = Greenfoot.getKey();
        if (key == null) return;
        if ("-".equals(key) && text.length() == 0)
        {
            text = "-";
            updateImage();
        }
        if ("0123456789".indexOf(key) > -1 && text.length() < 10)
        {
            text += key;
            updateImage();
        }
        if ("backspace".equals(key) && text.length() > 0)
        {
            text = text.substring(0, text.length() - 1);
            updateImage();
        }
        if ("enter".equals(key) && (text.length() > 1 || (text.length() > 0 && text.charAt(0) != '-')))
        {
            value = Integer.valueOf(text);
            getWorld().removeObject(this);
        }
    }
}
The simple world I created shows the checking if ready and retrieving of the number.
import greenfoot.*;

public class Pad extends World
{
    boolean gettingNumber = false;
    
    public Pad()
    {    
        super(400, 40, 1); 
        addObject(new NumberInputBox(), 200, 20);
        gettingNumber = true;
    }
    
    public void act()
    {
        if (gettingNumber && getObjects(NumberInputBox.class).isEmpty())
        {
            gettingNumber = false;
            System.out.println("" + NumberInputBox.value);
            Greenfoot.stop();
        }
    }
}
Barador Barador

2012/8/28

#
Thanks a lot. It works! Let's see if I get my variables ready and then finish my program :)
You need to login to post a reply.