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

2013/10/31

Referencing a Non-Static Method from a Static Context

Ed.Betteridge Ed.Betteridge

2013/10/31

#
Hi. I have the code:
(Display.getImage()).clear();          
(Display.getImage()).drawString(numOne+op+numTwo,2,20);
inside act() in my World class. (Is it obvious what I want to do?) I also have this code in my Display class inside my World class:
public class Display extends Actor{
        public Display(String text){
            GreenfootImage Textbox= new GreenfootImage(24*text.length(),20);
            Textbox.setFont(new Font("Consolas", Font.PLAIN, 22));
            Textbox.drawString(text,2,20);
            setImage(Textbox);  
        }
    }
I was wondering if there was a nice way around this error. This is for my Calculator Project I have a feeling that this video contains the answer I'm searching for but I can't seem to apply it to me scenario: Any comments welcome. Thanks :)
danpost danpost

2013/10/31

#
'Display' is the name of the class, not the name of a Display object. You cannot getImage from a class, you can basically only getImage from an Actor object (which a Display object is). Another thing is that you have no code within the Display class to alter the image (all you have is a constructor). This means that once you create one, it is immutable (all you can do is remove it from the world and create a new one).
Ed.Betteridge Ed.Betteridge

2013/10/31

#
Thanks Danpost. Would you be able to suggest the code I would need to add to make this work? (and currently I am just removing and re-creating the object, but I thought that that would be less efficient, no?)
danpost danpost

2013/10/31

#
You would need to add a method to the Display class to alter the image and have a reference to the Display object to call the method on.
You need to login to post a reply.