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

2012/5/20

drawString

thefutureisnow thefutureisnow

2012/5/20

#
Hello again, I was wondering how to display a string. I've been trying to use the java.awt.Graphics.drawString, but it doesn't seem to be working, maybe I'm doing something wrong? Here's the code I put in the world class:
    public Instructions_Menu()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);
        
        Graphics.drawString(String Hello World, 400, 300);
    }
I also tried putting "" around the Hello World part of it, but that didn't work either, it gave me the following error: ')' is expected and it was referring to the point right after drawString(String in the code I showed you before. I'm wondering if I'm doing this correctly, or if someone has another way of doing this that could be less confusing. Also, here's the API for the java.awt.Graphics : link Thanks!
danpost danpost

2012/5/20

#
I am sure that if you drop the word 'String' from the drawString call, and put the double quotes around 'Hello World', you would have better luck. However, Greenfoot has a drawString method of its own that works on GreenfootImage objects. There is also another way to create an image with text drawn on it; it is one of the GreenfootImage constructors. Check out the Greenfoot API. BTW, the GUIs put the types of the parameters in the method calls statements to let you know what type of object needs to be inserted there. You do not actually put the type in the call statements when you use them.
davmac davmac

2012/5/20

#
Graphics.drawString(String Hello World, 400, 300);
You have a few misunderstandings here, but one of the critical ones is that if a method requires a String parameter, it means that you must supply a String when you call the method. You do not need to include the word "String"; that is part of the method signature - just like you don't need to put 'int' in front of numbers. It sounds like you tried this: Graphics.drawString(String "Hello World", 400, 300); However, a better try would have been this: Graphics.drawString("Hello World", 400, 300); It still wouldn't have worked, because you're using a method from an interface without any target object. You can't call any method just by putting the "ClassName.methodName()" - you need to have an object reference, and then you use "object.methodName()" (the exception is 'static' methods, such as Greenfoot.playSound()). What this boils down to is that you need a Graphics instance before you can call the drawing methods, otherwise it's not clear what you're actually trying to draw on. Getting a Graphics instance isn't impossible but it's an advanced technique; it's better to use the Greenfoot API as danpost suggested.
thefutureisnow thefutureisnow

2012/5/20

#
I tried using the "GreenfootImage.drawString" and it told me it cannot be used in a static method, so I'm wondering how I can put it into a "instance" method? which I believe is the opposite of a static method. Thanks!
danpost danpost

2012/5/20

#
thefutureisnow wrote...
I tried using the "GreenfootImage.drawString" and it told me it cannot be used in a static method, so I'm wondering how I can put it into a "instance" method? which I believe is the opposite of a static method. Thanks!
First:
davmac wrote...
It still wouldn't have worked, because you're using a method from an interface without any target object. You can't call any method just by putting the "ClassName.methodName()" - you need to have an object reference, and then you use "object.methodName()" (the exception is 'static' methods, such as Greenfoot.playSound()).
What he is saying, is that the methods are already designated as static (or not). What this means is: if the method is static, you can call it with "ClassName.methodName()"; if it is not, then you need an object for the method to work on (an instance of that class), with "Object.methodName()". Since drawString() is a non-static method in the GreenfootImage class, you need an instance of a GreenfootImage object to call the method on.
GreenfootImage image = new GreenfootImage(100, 20);
image.drawString("Hello World", 10, 16);
If you look at almost any non-amateurish code, you will probably find examples of code that you could learn from and re-apply. Plus the Greenfoot tutorials and the Java trails have an abundance of educational information included in them.
thefutureisnow thefutureisnow

2012/5/20

#
Oh, now I get it. Thanks!!!
You need to login to post a reply.