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

2013/3/13

How would I add text input?

pashmi pashmi

2013/3/13

#
I want to be able to type something, have it converted to a string and shown to the user. Where would I begin? Also, how do I use "drawString()"? I've tried using it like this:
GreenfootImage img = getImage();
img.drawString("hello", x, y);
But it does not work. Any tips on using drawstring?
jagrosh jagrosh

2013/3/13

#
To use drawString() in the way you have it, you need one more line (add this after the two you already have: setImage(img);
danpost danpost

2013/3/13

#
It may be that you are drawing the string too high. The coordinates that you give are for the lower-left corner of the drawn string.
pashmi pashmi

2013/3/14

#
Thanks for your help. Any tips on detecting when a key is down, or storing a key's letter?
Gevater_Tod4711 Gevater_Tod4711

2013/3/14

#
In Greenfoot there are methods to check whether a key is down or to get the name of the key. You can find them in the Greenfoot API. The methods you need are isKeyDown and getKey. Both of this methods are static so you can call them like this: Greenfoot.isKeyDown("anyKey"); or Greenfoot.getKey();
jagrosh jagrosh

2013/3/15

#
If you would like a text box with a text field to appear on the screen (as well as pause the program until something is entered, try using JOptionPane: At the top of your code add the line import javax.swing.*; Then, to have an input box appear (and save the response as a String), use the line String inputstring = JOptionPane.showInputDialog("Your question here:");
bourne bourne

2013/3/15

#
http://www.greenfoot.org/scenarios/7578
You need to login to post a reply.