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

2018/3/7

How do i add a .gif as my world background?

Lavenger Lavenger

2018/3/7

#
I want to make it so that my world image is a gif but I have no idea how to do so, I have tried adding in this
1
2
3
4
5
6
7
8
9
10
GifImage gifImg = new GifImage("giphy.gif");
public void gifAnimation()
{
    setBackground(gifImg.getCurrentImage());
}
 
public void act()
{
    gifAnimation();
}
but it says "cannot find symbol- class GifImage" on both "GifImage"s on this line in particular
1
GifImage gifImg = new GifImage("giphy.gif");
What am I doing wrong here?
danpost danpost

2018/3/7

#
Did you import the GifImage class into your project?
Lavenger Lavenger

2018/3/7

#
danpost wrote...
Did you import the GifImage class into your project?
oh how do I do that?
Lavenger Lavenger

2018/3/7

#
Lavenger wrote...
danpost wrote...
Did you import the GifImage class into your project?
oh how do I do that?
nevermind, I found it, its under Edit>Import Class> GifImage ^^ thank you danpost Edit: how do you change the gif size? it seems to be repeating the image and not implementing just one big gif...
danpost danpost

2018/3/7

#
There is a method to get a List or array of all the images. Iterate through the list and scale each one once -- then you will be good to go. Use the 'getCurrrentImage' method in the act method to run the animation.
Lavenger Lavenger

2018/3/8

#
danpost wrote...
There is a method to get a List or array of all the images. Iterate through the list and scale each one once -- then you will be good to go. Use the 'getCurrrentImage' method in the act method to run the animation.
Since I'm using a world class background image, is this the one?
1
setBackground(gifImg.getCurrentImage());
danpost danpost

2018/3/8

#
Lavenger wrote...
Since I'm using a world class background image, is this the one?
1
setBackground(gifImg.getCurrentImage());
That should do it.
Lavenger Lavenger

2018/3/8

#
danpost wrote...
Lavenger wrote...
Since I'm using a world class background image, is this the one?
1
setBackground(gifImg.getCurrentImage());
That should do it.
but it's not fitting the entire world size with only one gif, its getting the original size and filling it until it fits the entire screen
danpost danpost

2018/3/8

#
In your world constructor, after creating the GifImage object, add the following:
1
for (Object obj : gifImg.getImages()) ((GreenfootImage)obj).scale(getWidth(), getHeight());
Lavenger Lavenger

2018/3/8

#
danpost wrote...
In your world constructor, after creating the GifImage object, add the following:
1
for (Object obj : gifImg.getImages()) ((GreenfootImage)obj).scale(getWidth(), getHeight());
I don't quite understand what does "Object" do, can you explain to me how do I use it??
danpost danpost

2018/3/8

#
I did not look to see exactly how the list of images were passed as (the type). So, I took each element from the list as an Object type and cast each as a GreenfootImage in the scaling code. I probably could just as well have had this:
1
for (GreenfootImage img : gifImg.getImages()) img.scale(getWidth(), getHeight());
now that I see it returns as type 'List<GreenfootImage>'.
Lavenger Lavenger

2018/3/8

#
danpost wrote...
I did not look to see exactly how the list of images were passed as (the type). So, I took each element from the list as an Object type and cast each as a GreenfootImage in the scaling code. I probably could just as well have had this:
1
for (GreenfootImage img : gifImg.getImages()) img.scale(getWidth(), getHeight());
now that I see it returns as type 'List<GreenfootImage>'.
oh thanks ^^ I now understand how the gif scaling and animation works, couldn't have done it without you :D
You need to login to post a reply.