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

2013/9/3

Insert image in world

savanibharat savanibharat

2013/9/3

#
I have a class ImageLabel. The constructor is as follows: public ImageLabel(String str) { GreenfootImage g = new GreenfootImage(str, 20, null, null); setImage(g); } Now I want to overload this constructor bt passing an object of another class and want to display another image(NOt of ImageLabel class). means like public ImageLabel(Actor object) { display image at specific location } I am not able to display an image.. How to do it???
Gevater_Tod4711 Gevater_Tod4711

2013/9/3

#
To display an image at a specific location you can either create an Actor and place it where you want to image to be displayed or draw the image you want to display onto the world's background using the method drawImage(GreenfootImage, int, int). The image will be printed at the location you choose with the int parameters.
savanibharat savanibharat

2013/9/3

#
Can you please tell me the syntax of drawImage
Gevater_Tod4711 Gevater_Tod4711

2013/9/3

#
If you want to use it in an actor subclass you can use it like this:
1
2
3
4
5
GreenfootImage drawedImage = new GreenfootImage("imageName.png");//the image that is drawed;
 
getWorld().getBackground().drawImage(drawedImage, 100, 100);
//this will draw the image at the position (100|100) of the world;
//The image's left top corner will be at the coordinate poin (100|100) not the center of the image;
(If you use this in a World subclass just leave out the getWorld().) The only problem with this code could be that you can't use it in your constructor because in your constructor you can't use getWorld() because it will return null (which will cause a NullPointerException). Instead of the constructor you can put it in the method addedToWorld(World world) which is called when an actor is added to the world.
Th3j4 Th3j4

2013/9/11

#
Gevater, drawImage method is able to display text in the world but not over the image that is already on the world. And more over i want the text to disappear after 2 seconds. What methods i have to use to do that?
Gevater_Tod4711 Gevater_Tod4711

2013/9/13

#
If you want your text to appear over the actors you need to create a new Actor subclass that prints your text. Try it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class TextImage {
     
    private int counter = 0;
     
    public TextImage(String text, int size) {
        setImage(new GreenfootImage(text, size, null, null);   
    }
 
    public void act() {
        counter++;
        if (counter > 1000) { //not shure if 1000 is about 2 seconds; you'll have to try
            getWorld().removeObject(this);
        }
    }
}
Th3j4 Th3j4

2013/9/13

#
Gevatar, how can i add objects to the world? I used getWorld().addObject(String,x,y). But i am getting a null pointer exception and init error... But the same piece of code works good in only one actor... Having problem with all other actors.. Help me with some code.. Thank you
danpost danpost

2013/9/14

#
The 'addObject' method has the signature 'addObject(Actor, int, int)', not 'addObject(String, int, int)'. You need to substitute 'Actor' with either 'new ActorName()' or get a reference to a new actor with 'Actor actor = new ActorName();' and then use 'addObject(actor, x, y);'. Of course, 'x' and 'y' need to refer to or be replaced with values.
You need to login to post a reply.