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.


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