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

2013/1/8

Newish Greenfooter HELP! :) - Turning String name into Greenfoot Image

TehDoctor TehDoctor

2013/1/8

#
public class PrintKey extends Actor { GreenfootImage keyLetter; GreenfootImage b = new GreenfootImage("b.png"); GreenfootImage j = new GreenfootImage("j.png"); GreenfootImage t = new GreenfootImage("t.png"); public PrintKey(String letter) { keyLetter = letter; } public void act() { setImage(keyLetter); } } I want keyLetter to be identified as a GreenfootImage with the value of the String letter. Thanks in advance! :)
danpost danpost

2013/1/8

#
One thing that catches my eye is that fact that you are bringing in a String (letter) in the PrintKey constructor and trying to assign it to a GreenfootImage object (keyLetter) which you cannot do. What you can do is:
public PrintKey(String letter)
{
    setImage(new GreenfootImage(letter+".png"));
}
You do not need any other code within the class (as far as I know at the moment).
TehDoctor TehDoctor

2013/1/8

#
Thanks, the only problem is that I plan to use those three base letter (b, j and t) to build other letters, since the idea of this program is to translate into a symbol like language that works off those three letters. So there is plan for a lot more code in this class but they depend on whether its possible to turn the value of a string into a GreenfootImage?
TehDoctor TehDoctor

2013/1/8

#
So is there a way to use the value of a String to call onto a GreenfootImage object?
danpost danpost

2013/1/8

#
If you are referring to make an image of a character (or a String), the GreenfooImage API has a constructor and a method that could be used to do just that. Click the 'Documentation' link at the top of the page and select 'online' in the 'Reference' section of the new page.
TehDoctor TehDoctor

2013/1/8

#
Its not that. I may be confusing both myself and you :) Ill explain what my code is meant to do. In another class, when a key is pressed the String letter in the constructor of Printkey is changed to that the key Pressed. So the g key would cause a PrintKey Object to be added to the world with the String letter set to "g". Now I want that new PrintKey Object to understand what image to be set to, according to the key that was pressed. So if the key pressed is g, and letter is changed to "g," the GreenfootImage object that the PrintKey image is set to is g. The problem is that letter is a String, ("g" in this case) and I want it to be converted to a GreenfootImage, so PrintKey could be set to that image.
vonmeth vonmeth

2013/1/8

#
    public PrintKey(String letter)
    {
        try {
        GreenfootImage instance = (GreenfootImage) getClass().getDeclaredField(letter).get(this);
        keyLetter = instance;
        } catch (Exception e) {
        e.printStackTrace();    
        }    
    }
TehDoctor TehDoctor

2013/1/8

#
Could you explain a bit what that code is meant to do so i could implement it over again. New to java :)
danpost danpost

2013/1/8

#
If you had looked at the GreenfootImage documentation, you would have seen a GreenfootImage constructor that has a String as its first parameter. That string is used in creating the new GreenfootImage object which is a representation of the String. The constructor is: GreenfootImage(String, int, Color, Color) where the int is the font size, the first Color is the text color and the second Color is the background color.
TehDoctor TehDoctor

2013/1/8

#
I understand what that does. Its not what I am trying to do though. I have an Alien symbol based language from a TV show that I am trying to make a word processor for. Its not going to spit out English Letters, nor will it spit out any known language. The letters have to be created from separate images. That where the "b.png" (which sort of looks like a half circle with a line through it) and the others come into play. My question of converting the String into a GreenfootImage object is so when letter is defined as some String, let it be "b" in this case, the GreenfootImage that is used to set the Image of this Class is will also be b. (not "b") I want, lets say,
if(letter == "b")
{
    keyLetter = b
}
setImage(keyLetter);
But without having to go through all that for each letter, I want to just be able to have the GreenfootImage keyLetter defined by whatever the String letter is. So if letter is "b" then keyLetter is b;
danpost danpost

2013/1/8

#
Oh, you want the GreenfootImage variable name to be that of the letter! I do not think that that is possible (but I could be wrong).
TehDoctor TehDoctor

2013/1/8

#
Yes! thank you for clarifying my question, that was the main reason i came here! now I can properly ask my teacher without giving him an brain aneurysm :) ... But yes, if anyone else could figure this out, much appreciated. or if you, dan, have an alternative way of going by this? Or do i just have to define each keyLetter with a bunch of ifs?
danpost danpost

2013/1/8

#
I would just create the images of each character and assign them in order like: char0.png, char1.png, char2.png, etc. to char999.png or however many characters in the language minus one. Then, use a conversion table to determine the image for each letter: int i="0123456789abcdefghijklmnopqrstuvwxyz".indexOf(letter); setImage("char"+i+".png");.
TehDoctor TehDoctor

2013/1/8

#
That would work, but I want to make it even simpler because all 24 letters build off the 3 base letters. So although B is that strange half circle with a line, an M would be the same letter with just a line sticking out of it. So i just created the 3 base letters and a line. Then ill attach them together to make characters in the code. I may just set up 24 separate if statements for each letter. Unless there's some possible way to perform the toString() method backwards :D
You need to login to post a reply.