I would like to put in an animated gif file to my world class background. What is the code that will do that? Thank you -


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Space is the setting for Spaceship scenario. * This is a place where Meteors, Satelites, and FuelCells float around. * * @author Michael V. * @version 1.0 */ public class Space extends World { private int fuel; private int score; /** * Constructor: Set up the staring objects. */ public Space() { super ( 780 , 360 , 1 ); prepare(); setPaintOrder(Border. class ); score = 0 ; fuel = 2000 ; showFuel(); //getCurrentImage(); } /** * Create new floating objects at random intervals. */ public void act() { if (Greenfoot.getRandomNumber( 100 ) < 3 ) { addObject( new FuelCell(), 779 , Greenfoot.getRandomNumber( 360 )); } if (Greenfoot.getRandomNumber( 100 ) < 3 ) { addObject( new Surface(), 779 , 0 ); } if (Greenfoot.getRandomNumber( 100 ) < 3 ) { addObject( new Surface(), 779 , 359 ); } if (Greenfoot.getRandomNumber( 100 ) < 1 ) { addObject( new Meteor(), 779 , Greenfoot.getRandomNumber( 360 )); } if (Greenfoot.getRandomNumber( 100 ) < . 05 ) { addObject( new Satelite(), 779 , Greenfoot.getRandomNumber( 360 )); } countFuel(); } public void addScore( int points) { score = score + points; showText( "Score: " + score, 80 , 25 ); } public void addFuel( int bonus) { fuel = fuel + bonus; showText( "Fuel: " + fuel, 700 , 25 ); } private void countFuel() { fuel--; showFuel(); if (fuel == 0 ) { showEndMessage(); Greenfoot.stop(); } } private void showFuel() { showText( "Fuel: " + fuel, 700 , 25 ); } /** * Prepare the world for the start of the program. In this case: Spawn * a Spaceship and the moons surface at the edge of screen. */ private void prepare() { Spaceship spaceship = new Spaceship(); addObject(spaceship, 103 , 179 ); Surface surface = new Surface(); addObject(surface, 126 , 1 ); Surface surface2 = new Surface(); addObject(surface2, 342 , 5 ); Surface surface3 = new Surface(); addObject(surface3, 589 , 2 ); Surface surface4 = new Surface(); addObject(surface4, 695 , 5 ); Surface surface5 = new Surface(); addObject(surface5, 114 , 359 ); Surface surface6 = new Surface(); Surface surface7 = new Surface(); addObject(surface7, 295 , 353 ); Surface surface8 = new Surface(); Surface surface9 = new Surface(); Surface surface10 = new Surface(); addObject(surface10, 480 , 358 ); Surface surface11 = new Surface(); addObject(surface11, 596 , 359 ); Surface surface12 = new Surface(); addObject(surface12, 740 , 354 ); Border border = new Border(); addObject(border, 0 , 180 ); Border border2 = new Border(); addObject(border2, 770 , 180 ); setPaintOrder(Border. class ); } private void showEndMessage() { showText( "Times up - you win!" , 390 , 150 ); showText( "Your final score was: " + score + "points!" , 390 , 170 ); } } |
1 | private GifImage animation = new GifImage( "<< gif filename >>" ); |
1 | setBackground(animation.getCurrentImage()); |