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).
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 | 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 ); } } } |
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 | 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); } } } |
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 | 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 ); } } |