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

2015/3/25

drawImage and differnt Images in one method

xWanna xWanna

2015/3/25

#
Hey how do I draw an Image through a method. My current one doesn't work
W1 w1 = (W1) getWorld();
        GreenfootImage im = new GreenfootImage(100, 500);
        im.drawImage(new GreenfootImage("Anzeigen/Lebenspunkte2.png"), 100, 500);
And is it possible to create different Images in one method? I want to create 5 rects that should be displayed at the same time. Thanks fo helpig me out.
Super_Hippo Super_Hippo

2015/3/25

#
'im' is 100x500 pixels large. You are trying to draw the other image on that one with the top left pixel at 100|500. Since 'im' ends at 99|499, it isn't visible. So the 100 and 500 in the last line should be smaller values than the values in the second line. If you put in "0, 0", then the image will be drawn in the top left corner.
xWanna xWanna

2015/3/25

#
Super_Hippo wrote...
'im' is 100x500 pixels large. You are trying to draw the other image on that one with the top left pixel at 100|500. Since 'im' ends at 99|499, it isn't visible. So the 100 and 500 in the last line should be smaller values than the values in the second line. If you put in "0, 0", then the image will be drawn in the top left corner.
Thanks for the response. The image isn't drawn even if I change the values in the last line.
Super_Hippo Super_Hippo

2015/3/25

#
Do you use 'setImage' or 'setBackground'?
xWanna xWanna

2015/3/25

#
I use 'setBackground' in the world class. And I don't use 'setImage' because if I use it it would change the picture of the object wouldn't it? And I want to display more than one image at the same time.
Super_Hippo Super_Hippo

2015/3/25

#
I mean, you have the image 'im' and draw the "Anzeigen/Lebenspunkte2.png" on it. Do you set this 'im' to the background of the world? So is the next line 'w1.setBackground(im);' (though I am not 100% sure why this code is in an Actor subclass)?
xWanna xWanna

2015/3/25

#
No, I use a different image as background and I don't have a problem with it. The image Lebenspunkte should be drawn on top of the background so it's just displayed. The code is finished with 'im.drawImage(...)' do I need another line to finally display it?
Super_Hippo Super_Hippo

2015/3/25

#
So you want to draw it on the background. You create a new image and call it 'im'. How should be the background changed if you draw an image on a new image?
xWanna xWanna

2015/3/25

#
Sorry, I explained it a bit bad. I want to draw the image seperatly and not on the background. But later on I want to add Images in the same class, so they are displayed at the same time.
Super_Hippo Super_Hippo

2015/3/25

#
As long as you don't set it as an image, you can't see the result. So how can you be sure that changing the numbers doesn't do anything?
xWanna xWanna

2015/3/25

#
I thougt it would display it even without saying 'setImage()'. And if I want to add images wouldn't the first one be replaced?
Super_Hippo Super_Hippo

2015/3/25

#
You could create thousands of images. There aren't automatically displayed.
xWanna xWanna

2015/3/25

#
Ok, thanks. Do I have to use a specific line, such as . 'Image.setImage(...)' or just 'setImage'?
Super_Hippo Super_Hippo

2015/3/25

#
Displayed images are always bounded to an object - actor or world. Every object can only have one image, but you can draw images on another image to display "more".
danpost danpost

2015/3/25

#
xWanna wrote...
Ok, thanks. Do I have to use a specific line, such as . 'Image.setImage(...)' or just 'setImage'?
'setImage' is an Actor class method that assigns an image to an actor instance:
/** Actor object */.setImage(/* GreenfootImage or String object */);
The actor must be added to the world using the World object method 'addObject' before the image of the actor is visible. If the changes to the world background image are to be permanent, then you can get the background of the world and use the GreenfootImage object method 'drawImage' to draw the image directly onto the background image. If they are only temporary, using actors is the easiest way to go; otherwise, you would need to save the part of the background image that will be drawn on before drawing on it so you can re-draw the original part of the image back in place (unless you can just reset the entire background image of the world).
You need to login to post a reply.