Hey! I want to know how I can display a gif to the screen when a timer I made is over.
This is what is in the World
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * * <p> * <p> * */ /** * Declare Instance Variables */ public class Timer extends Actor { //Maximum time the Timer can run. private int maxTime; // private int currTime = 0; //The rate of which the bar how fast the bar needs to drain private double currPercentTime; //The size of the blue bar private int blueBarSize; //The bar image private GreenfootImage bar; //The black bar image private GreenfootImage blackBar; //Sets the bar width, must be greater than or equal to 100 private final int TIMER_BAR_WIDTH = 255; //Sets bar height, must be greater than or equal to 20 private final int TIMER_BAR_HEIGHT = 20; //Initalize colours red and blue private int red; private int blue; //Value can be changed from 0 to 255 private final int GREEN_COLOUR = 255; // the first colours private Color myBlue = new Color (0, 255, 255); private Color myBlack = new Color (51, 64, 69); //Initializing the text colour private Color foreground; //Ititializing the String text private String text; private final int NUM_ZEROS = 5; /**Constructor creates the bar image according to size and height. * Fills bar so it can start full and start to drain as the timer starts. * Also displays "Time" text on screen. */ public Timer() { blueBarSize = TIMER_BAR_WIDTH; red = 0; //Creates the black bar that is to be under/behind the coloured bar as reference to how much time has passed. bar = new GreenfootImage(TIMER_BAR_WIDTH, TIMER_BAR_HEIGHT); blackBar = new GreenfootImage(100,100); //Sets the colour, fills the bar, sets the overall image. bar.setColor(myBlack); bar.fill(); this.setImage(bar); //Sets and displays text foreground = new Color (255, 255, 255); text = "Time:"; this.update(text); } /** * Overload constructor that sets the current time to be the maximum amount of time as well as * the full bar(maximum time) to be blue * <p> * @param theMaxTime The maximum amount of time in the timer */ public Timer(int theMaxTime) { //Retrieves the values for maxTime and currTime this(); maxTime = theMaxTime; currTime = theMaxTime; blue = maxTime; } public Timer(int theMaxTime, int theCurrTime) /** * Overload constructor that retrieves/sets the maxTime to theMaxtime and currTime to theCurrTime * <p> * @param theMaxTime The maximum amount of time in the timer * @param theCurrTime The current time in the timer */ { //Retrieves the values for maxTime and currTime this(); maxTime = theMaxTime; currTime = theCurrTime; blue = maxTime; } /** * Displays white text * <p> * @param output Updates the String in the Timer bar */ public void update (String output) { bar.setColor(foreground); int centeredY = (TIMER_BAR_HEIGHT/2+5); bar.drawString(output, 20, centeredY); } /** * Overload method update that takes in two booleans, startTime and endTime * Checks to see if the timer is to start/is starting or has ended. * If the Timer is starting the bar will decrease and the "Time" text is shown with how much time is left. * When the timer is over it displays text "Time Over!" * <p> * @param startTime If Timer has started * @param endTime If the timer has ended */ public void update(boolean startTime, boolean endTime) { if(startTime) { //If the blue bar is not empty keeps decreasing the timer if(blue > 0) { decreaseTimer(); } //Initializes text to be set String setTimeString; if(currTime > 0) { //Keeps the "Time" text present the whole time the program is run, even if the timer reaches zero setTimeString = zeroAdder(currTime, NUM_ZEROS); text = "Time: " + setTimeString; this.update(text); //Decreases the current time from the timer currTime--; } } //If the currentTime is 0 and endTime is true display new text "Timer over!" if(endTime && currTime == 0) { text = "TIMER OVER!"; this.update(text); } } /** * Overload method update that takes in two booleans, startTime and endTime * Checks to see if the timer is to start/is starting or has ended. * If the Timer is starting the bar will decrease and the "Time" text is shown with how much time is left. * When the timer is over it displays text "Time Over!" * * @param startTime Checks to see if Timer has started * @param endTime Checks to see if Timer has ended * @param endPhrase Message that is displayed once timer is over */ public void update(boolean startTime, boolean endTime, String endPhrase) { if(startTime) { //If the blue bar is not empty keeps decreasing the timer if(blue > 0) { decreaseTimer(); } //Initializes text to be set String setTimeString; if(currTime > 0) { //Keeps the "Time" text present the whole time the program is run, even if the timer reaches zero setTimeString = zeroAdder(currTime, NUM_ZEROS); text = "Time: " + setTimeString; this.update(text); //Decreases the current time from the timer currTime--; } } //If the currentTime is 0 and endTime is true display new end text message if(endTime && currTime == 0) { text = endPhrase; this.update(text); } } /** * Displays a set number of zeroes to display on the screen for the Timer. * * @param value The amount of zeroes needed to be displayed on the screen * @param digits The number of digits in the amount of time */ public static String zeroAdder (int value, int digits) { // Figure out how many digits the number is int numDigits = digitCounter(value); // If not extra digits are needed if (numDigits >= digits) { return Integer.toString(value); } else // Build the number with zeroes for extra place values: { String zeroes = ""; for (int i = 0; i < (digits - numDigits); i++) { zeroes += "0"; } return (zeroes + value); } } /** * Counts the number of digits in the starting amount of time * <p> * @param number Number of digits in starting time */ private static int digitCounter (int number) { if (number < 10) { return 1; } int count = 0; while (number > 0) { number /= 10; count++; } return count; } /** * Decreases the timer and changes the colour simultaneously. */ public void decreaseTimer() { //If the current time is not the max time, the bar is to decrease/change colour. if(currTime != maxTime) { currPercentTime = (double) currTime / maxTime; blueBarSize = (int) (currPercentTime * TIMER_BAR_WIDTH); blue -= 1; //Blue cannot be greater than 255 or else the colour does not exist if (blue > 255) { myBlue = new Color(0, 255, 255); } //Decreases the bar and changes the bar to red and "disappears" else if(red < 256 && blue > 0) { red += 1; myBlue = new Color(red, blue,GREEN_COLOUR); } //When the Bar is empty, the blue bar size is 0 else { blueBarSize = 0; } bar.setColor(myBlue); bar.fillRect(0, 0, blueBarSize, TIMER_BAR_HEIGHT); bar.setColor(myBlack); bar.fillRect(blueBarSize,0, TIMER_BAR_WIDTH - blueBarSize, TIMER_BAR_HEIGHT); this.setImage(bar); } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MainWorld here. * * @author (your name) * @version (a version number or a date) */ public class MainWorld extends World { private Timer timer; /** * Constructor for objects of class MainWorld. * */ public MainWorld() { // Create a new world with 940x640 cells with a cell size of 1x1 pixels. super(1000, 640, 1); GreenfootImage bg = new GreenfootImage("Background.jpg"); bg.scale(getWidth(), getHeight()); setBackground(bg); timer = new Timer(10000,10000); //Adds object to the world while centering it on screen. addObject(timer, 800, 50); // adding grid for(int i = 140; i < 640; i += 105){ for(int j = 285; j < 1000; j += 82){ addObject(new Grass(), j, i); } } } public void act() { timer.update(true, true, "THE BIRDS WORK FOR THE BOURGEOISIE" ); } }