I am so confused . can anybody help me
need to supply code in method checkMouseClick(), so that whenever you click a single leaf, this single leaf will change color(change image), and all other leaves does not change colors (images).
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; /** * A block that bounces back and forth across the screen. * * @author Michael Kölling * @version 1.0 */ public class Block extends Actor { private int delta = 2; /** * Move across the screen, bounce off edges. Turn leaves, if we touch any. */ public void act() { move(); checkEdge(); checkLeaf(); checkMouseClick(); } /** * Move sideways, either left or right. */ private void move() { setLocation(getX()+delta, getY()); } /** * Check whether we are at the edge of the screen. If we are, turn around. */ private void checkEdge() { if (isAtEdge()) { delta = -delta; } } /** * Check whether the mouse button was clicked. If it was, change all leaves. */ private void checkMouseClick() { if (Greenfoot.mouseClicked(null)) { World world = getWorld(); List<Leaf> leaves = world.getObjects(Leaf.class); for (Leaf leaf : leaves) { leaf.changeImage(); } } } /** * Check whether we're touching a leaf. If we are, turn it a bit. */ private void checkLeaf() { Leaf leaf = (Leaf) getOneIntersectingObject(Leaf.class); if (leaf != null) { leaf.turn(9); } } }
import greenfoot.*; /** * A floating leaf that blows across the screen. * * @author Michael Kölling * @version 1.0 */ public class Leaf extends Actor { private int speed; GreenfootImage img1 = new GreenfootImage("leaf-green.png"); GreenfootImage img2 = new GreenfootImage("leaf-brown.png"); /** * Create the leaf. */ public Leaf() { setImage(img1); speed = Greenfoot.getRandomNumber(3) + 1; // random speed: 1 to 3 setRotation(Greenfoot.getRandomNumber(360)); } /** * Move around. */ public void act() { if (isAtEdge()) { turn(180); } move(speed); if (Greenfoot.getRandomNumber(100) < 50) { turn(Greenfoot.getRandomNumber(5) - 2); // -2 to 2 } } /** * Change the image to another leaf image. This toggles back and * forth between two images. */ public void changeImage() { if (getImage() == img1) { setImage(img2); } else { setImage(img1); } } }
import greenfoot.*; /** * Autumn. A world with some leaves. * * @author Michael Kölling * @version 1.0 */ public class MyWorld extends World { /** * Create the world and objects in it. */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); setUp(); } /** * Create the initial objects in the world. */ private void setUp() { int i = 0; while (i<18) { int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); addObject( new Leaf(), x, y ); i++; } addObject(new Block(), 300, 200); } }