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

2013/11/14

Importing an Image

mpw7488 mpw7488

2013/11/14

#
How do I import my own image (saved on my desktop) into the Greenfoot application?
Gevater_Tod4711 Gevater_Tod4711

2013/11/14

#
If you copy your image from your desctop into the images folder in your greenfoot scenario you can easily use it. To load the image you just need to do this:
GreenfootImage image = new GreenfootImage("YourImagesName.type");
Then you have a new GreenfootImage object called image which is your image you wanted to import.
mpw7488 mpw7488

2013/11/14

#
I'm kind of a dummy with this stuff so far (just started this week for a college class) and I couldn't figure out where to put that code, but I noticed the "import from file" button when I went into the new subclass window... so I did it that way. But for future reference, where would I put that in my code?
Gevater_Tod4711 Gevater_Tod4711

2013/11/14

#
That depends on what you want to do with this image. If you want this image only to be the image for your actor you can put the code in your constrcuctor like this:
public class YourClass extends Actor {
    
    public YourClass() {
        setImage(new GreenfootImage("YourImage.type"));
    }
}
If you want to use this image again or just have the reference to this image in the whole class you should declare the variable as a global one like this:
public class YourClass extends Actor {
    
    private GreenfootImage image = new GreenfootImage("YourImage.type");
}
If you do it this way you can use the variable image in your whole class.
mpw7488 mpw7488

2013/11/14

#
thanks :) ...do you know about resizing an object too? lol I put a code in, and it says there are no errors, but it doesn't resize the object? this is the code i put:
{
    GreenfootImage image = getImage();
    image.scale(image.getWidth() - 1, image.getHeight() - 1);
    setImage(image);
}
Gevater_Tod4711 Gevater_Tod4711

2013/11/14

#
To resize an image you can use the method scale(int width, int height) which scales your image to the new size. You can also find all the methods of the GreenfootImage class in the API.
danpost danpost

2013/11/14

#
The code you gave above for scaling should work. Where is this code placed within your class? BTW, the slight change in the size of the image would not be very apparent unless the code is executed multiple times. However, re-scaling images is not recommended. Better is to create a new original image and scale it to the new dimensions.
mpw7488 mpw7488

2013/11/14

#
under this: public class ball extends Actor so, how do I know how much to scale it? I want to make it smaller than it is, I've tried 10, 5 and 1 as the numbers, but they all look the same?
danpost danpost

2013/11/14

#
To make the image half-sized, you could use:
GreenfootImage image = getImage();
image.scale(image.getWidth()/2, image.getHeight()/2);
Multiply by any fractional ratio (*2/3, *3/4, *3/5) to proportionally change the width and height.
mpw7488 mpw7488

2013/11/14

#
ohh, ok :) I ended up just creating a new image and uploading it, but I'm going to try this to see if I can figure it out for myself
You need to login to post a reply.