is there a built in method for displaying an image from a file? or should i make an actor class for each type of fish? because i only know of one way to put an image onto the screen which is through putting the actor on screen
addObject(new TempImage(image), getWidth()/2, getHeight()/2);
public void getFish() { int[] fishes = new int[100]; int n = 0; //making an array for each type of fish using the percentages in the //fishCount array so that when it chooses a random number, depending on //which fish's array the number is in,it will choose that type of fish for (int t=0; t<typeCount; t++) { for (int i=0; i<fishCount[t]; i++) fishes[n+i] = t; n += fishCount[t]; } // choose a caught fish int caughtFish = Greenfoot.getRandomNumber(100); int type = fishes[caughtFish]; // type of fish caught GreenfootImage image = fishImages[type]; TempImage.setImage(fishImages); addObject(new TempImage(image), getWidth()/2, getHeight()/2); }
public class TempImage extends Actor { /** * Act - do whatever the cFish wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ GreenfootImage fishType = getImage(); GreenfootImage[] fishImages; private final int typeCount = 3; private int timer = 120; private boolean TimerStart = false; public TempImage() { fishImages = new GreenfootImage[typeCount]; fishType.scale(80, 50); for (int i = 0; i < typeCount; i++) { fishImages[i] = new GreenfootImage("Images/fishTypes/fish" + i + ".png"); } } public void act() { TimerStart = true; if (TimerStart == true) { timer--; if (timer <= 0) { ((MyWorld)getWorld()).removeObject(TempImage); } } } }
Actor tempActor = new TempImage(); tempActor.setImage(image); addObject(tempActor, getWidth()/2, getHeight()/2);
import greenfoot.*; public class TempImage extends Actor { int timer; public void act() { if (++timer == 120) getWorld().removeObject(this); } }