Question
can you do the instruction public class Button extends Actor { private GreenfootImage img1, img2; private GreenfootImage defaultImg; public Button() { img1 = new GreenfootImage(button-red.png);
can you do the instruction
public class Button extends Actor { private GreenfootImage img1, img2; private GreenfootImage defaultImg; public Button() { img1 = new GreenfootImage("button-red.png"); img2 = new GreenfootImage("button-green.png"); defaultImg = img1;
}
/** * Changes the location of this button approximately * 2% of the time. */ public void act() { if (Greenfoot.getRandomNumber(100)
}
/** * Sets the location of this Button object to a new (x, y) * coordinate selected at random. The x coordinate wil be in * the range 100 to 500 inclusive, and the y coordinate will * be the range 100 to 300 inclusive. */ public void changeLocation() { setLocation(Greenfoot.getRandomNumber(401) + 100, Greenfoot.getRandomNumber(201) + 100); } /** * Switches the default image source from current image to the other image */ public void changeImage() { if(defaultImg == img1) {
defaultImg = img2;
} else {
defaultImg= img1;
} } }
public class Ball extends Actor { private GreenfootImage img1, img2; private GreenfootImage defaultImage; private int removedButtonCounts; public Ball() { removedButtonCounts = 0; img1 = new GreenfootImage("steel-ball.png"); img2 = new GreenfootImage("gold-ball.png"); defaultImage = img1; } /** * Responds to motion control from the arrow keys and removes * any buttons from the world when it intersects them. */ public void act() { handleKeyPress(); lookForButton(); checkToStop(); }
/** * Checks to see if the ball is "touching" an object of the Button class. * If it is, then the ball removes from the World a Button that it touches. * If the ball isn't touching an object of the Button class, then this * method does nothing. */ public void lookForButton() { if (isTouching(Button.class)) { removeTouching(Button.class); removedButtonCounts ++; if(removedButtonCounts >= 5) { defaultImage = img2; } } }
/** * Handles input from the arrow keys. */ public void handleKeyPress() { checkLeftArrow(); checkRightArrow(); checkUpArrow(); checkDownArrow(); }
/** * Checks to see if the left arrow key is being pressed. * If it is, then the ball sets its rotation angle to zero degrees * and moves backward 5 units. If it is not being pressed, * this method does nothing. */ public void checkLeftArrow() { if (Greenfoot.isKeyDown("left")) { setRotation(0); move(-5); } }
/** * Checks to see if the right arrow key is being pressed. * If it is, then the ball sets its rotation angle to zero degrees * and moves forward 5 units. If it is not being pressed, * this method does nothing. */ public void checkRightArrow() { if (Greenfoot.isKeyDown("right")) { setRotation(0); move(5); } }
/** * Checks to see if the up arrow key is being pressed. * If it is, then the ball sets its rotation angle to 270 degrees * and moves forward 5 units. If it is not being pressed, * this method does nothing. */ public void checkUpArrow() { if (Greenfoot.isKeyDown("up")) { setRotation(270); move(5); } }
/** * Checks to see if the down arrow key is being pressed. * If it is, then the ball sets its rotation angle to 90 degrees * and moves forward 5 units. If it is not being pressed, * this method does nothing. */ public void checkDownArrow() { if (Greenfoot.isKeyDown("down")) { setRotation(90); move(5); } } public void checkToStop() { if(removedButtonCounts == 10) { } }
}
1. Modify the constructor of the Buttonworld class so that a single Ball object is placed at location (500, 400) and ten Button objects are placed at random locations throughout the world. w World (Greenfoot API X Button World Button Ball Comple Undo Cut Copy Paste Find... Close import greenfoot.*; 1 (World, Actor. GreenfootImage, Greenfoot and Mouse Info) public class Buttonworld extends World * Constructor for objects of class Buttonworld. public Buttonworld 1/ Create a new world with 680x480 cells with a cell size of 1x1 pixels. super (680, 400, 1); 1. Modify the constructor of the Buttonworld class so that a single Ball object is placed at location (500, 400) and ten Button objects are placed at random locations throughout the world. w World (Greenfoot API X Button World Button Ball Comple Undo Cut Copy Paste Find... Close import greenfoot.*; 1 (World, Actor. GreenfootImage, Greenfoot and Mouse Info) public class Buttonworld extends World * Constructor for objects of class Buttonworld. public Buttonworld 1/ Create a new world with 680x480 cells with a cell size of 1x1 pixels. super (680, 400, 1)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started