Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with just number 8. The rest of the code is done.ty BARREL CLASS import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import

Need help with just number 8. The rest of the code is done.ty

image text in transcribed

BARREL CLASS

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List;

/** * Write a description of class Barrel here. * * @author (your name) * @version (a version number or a date) */ public class Barrel extends Actor { private final double SCALE_FACTOR_5 = 1.05; private final double SCALE_FACTOR_25 = 1.25; public int mushroomsStored; private int ns; public Barrel() { mushroomsStored = 0; ns = 0; }

/** * Automatically called by Greenfoot whenever a Barrel object * is placed in a world. In this assigment, we use it to remember * the initial state of this barrel - its (x,y) position and its * original image size. */ public void addedToWorld(World world) { GreenfootImage img = new GreenfootImage("barrel.png"); setImage(img); final int originalX = getX(); final int originalY = getY(); final int originalWidth = img.getWidth(); final int originalHeight = img.getHeight(); } /** * Returns how many mushrooms this barrel has stored. */ public int getMushrooms() { return mushroomsStored; } /** * Follows mouse drag-and-drop motion. Eats nearby mushrooms when * dropped and increases its current image scale by 25%. If this barrel * stores more than 10 mushrooms, this barrel has itself removed from * this world. */ public void act() { if (Greenfoot.mouseDragged(this)) { MouseInfo mouse = Greenfoot.getMouseInfo(); setLocation(mouse.getX(), mouse.getY()); } if(Greenfoot.mouseDragEnded(this)) { List nearby = getObjectsInRange(75, Mushroom.class); ns = nearby.size(); for(Mushroom m : nearby) { getWorld().removeObject(m); mushroomsStored++; } if(ns = 5) { GreenfootImage img = getImage(); int width = (int)(img.getWidth() * SCALE_FACTOR_25); int height = (int)(img.getHeight() * SCALE_FACTOR_25); img.scale(width, height); } if(mushroomsStored == 10) { getWorld().removeObject(this); } } }

/** * Returns this barrel to its original (x,y) location and its * original image scale. */ public void reset() { if (Greenfoot.mouseClicked(this)) { MouseInfo mouse = Greenfoot.getMouseInfo(); if(mouse.getButton() == 3) { this.setLocation(565, 350); setImage(new GreenfootImage("barrel.png")); mushroomsStored = 0; } } }

}

PIG CLASS

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/** * Write a description of class Pig here. * * @author (your name) * @version (a version number or a date) */ public class Pig extends Actor { private int shrooms; /** Keeps track of how many mushrooms this pig has eaten. */ /** * Constructs a Pig object and initializes it as having * eaten no mushrooms. */ public Pig() { shrooms = 0; } /** * Follows the mouse movement and eats mushrooms on mouse clicks. * Stops the scenario once this pig has eaten at least 15 mushrooms. */ public void act() { if (Greenfoot.mouseMoved(null)) { MouseInfo mouse = Greenfoot.getMouseInfo(); setLocation(mouse.getX(), mouse.getY()); } if (Greenfoot.mouseClicked(null)) { Mushroom m = (Mushroom) getOneIntersectingObject(Mushroom.class); if (m != null) { shrooms++; getWorld().removeObject(m); } } if (shrooms > 29) { Greenfoot.stop(); } } }

8. If the user right-clicks on the barrel, the pig "eats" all the mushrooms currently in the barrel and the barrel returns to its original size and original location. This will involve the [act method and fields of the Pig class, the getMushrooms method of the Barrel class, and the reset method of the Barrel class Remembering the original state of the barrel (location and image size) will involve fields of the Barrel class and the addedToworld method of the Barrel class Notes on Step8 This is the hardest part of the assignment. There are essentially three parts to step 8, and I'll discuss each below 1. "If the user right-clicks on the barrel," It seems like this would be handled in the (act method of the Barrel class since we're wanting to detect a click "on the barrel". But: (1) It's the pig that has to take action, not the barrel. (2) Since the pig follows the mouse, if the mouse is over the barrel, the pig is too. These two facts together mean that this step will actually all be handled in the act method of the Pig You'll have to use the Greenfoot.mouseClicked (null) method to detect that a click occurred. If a click has occurred, you will create a MouseInfo object using the Greenfoot.getMouseInfo() method like usual. Then, you can use the mouse.getButton() method to determine if the click was the left, center, or right button. It would be very helpful to view the documentation for the Greenfoot class and read what each of these methods do 2. "the pig eats all the mushrooms currently in the barrel" The pig will have to call the barrel's getMushroom() method to find out how many mushrooms the barrel currently stores. The pig can add this amount to its count of mushrooms eaten. Remember, the mushrooms are already gone from the world - this happened when the barrel "stored them." 3. "and the barrel returns to its original size and original location." This is the purpose of the reset) method in the Barrel class, and the pig will have to call this method. The reset method relies on two things already having happened: (1) Fields have been declared in the Barrel class to record the original location (x and y coordinates) and orignal image size (width and height). (2) These fields have been assigned in the addedToWorld( method of the Barrel class 8. If the user right-clicks on the barrel, the pig "eats" all the mushrooms currently in the barrel and the barrel returns to its original size and original location. This will involve the [act method and fields of the Pig class, the getMushrooms method of the Barrel class, and the reset method of the Barrel class Remembering the original state of the barrel (location and image size) will involve fields of the Barrel class and the addedToworld method of the Barrel class Notes on Step8 This is the hardest part of the assignment. There are essentially three parts to step 8, and I'll discuss each below 1. "If the user right-clicks on the barrel," It seems like this would be handled in the (act method of the Barrel class since we're wanting to detect a click "on the barrel". But: (1) It's the pig that has to take action, not the barrel. (2) Since the pig follows the mouse, if the mouse is over the barrel, the pig is too. These two facts together mean that this step will actually all be handled in the act method of the Pig You'll have to use the Greenfoot.mouseClicked (null) method to detect that a click occurred. If a click has occurred, you will create a MouseInfo object using the Greenfoot.getMouseInfo() method like usual. Then, you can use the mouse.getButton() method to determine if the click was the left, center, or right button. It would be very helpful to view the documentation for the Greenfoot class and read what each of these methods do 2. "the pig eats all the mushrooms currently in the barrel" The pig will have to call the barrel's getMushroom() method to find out how many mushrooms the barrel currently stores. The pig can add this amount to its count of mushrooms eaten. Remember, the mushrooms are already gone from the world - this happened when the barrel "stored them." 3. "and the barrel returns to its original size and original location." This is the purpose of the reset) method in the Barrel class, and the pig will have to call this method. The reset method relies on two things already having happened: (1) Fields have been declared in the Barrel class to record the original location (x and y coordinates) and orignal image size (width and height). (2) These fields have been assigned in the addedToWorld( method of the Barrel class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 23rd International Conference Dexa 2012 Vienna Austria September 2012 Proceedings Part 1 Lncs 7446

Authors: Stephen W. Liddle ,Klaus-Dieter Schewe ,A Min Tjoa ,Xiaofang Zhou

2012th Edition

3642325998, 978-3642325991

More Books

Students also viewed these Databases questions

Question

What could be some of the consequences of these perceptions?

Answered: 1 week ago