package class1_3_DuckStrategies_v2; import java.awt.Point; /** * This class represents a specialized type of Duck * @author hornick */ public class RubberDuck extends Duck { /** * RubberDuck-specific constructor */ public RubberDuck(String name) { super(name, "rubber.jpg"); // this value defines how far the duck moves each update period - s random value between 1 and 3. increment = new Point(random.nextInt(3)+1, random.nextInt(3)+1 ); } /** * This method OVERRIDES the Duck's default quacking behavior, * because RubberDucks don't make any sound. */ @Override public void quack() { // do nothing - Note same (duplicate) behavior implemented by Decoy } /** * This method OVERRIDES the Duck's default swimming behavior, * because RubberDucks only bob around. */ @Override public void swim() { if( (currentPos.x > (DuckPond.PONDSIZE-image.getWidth(null))) // leaving the window's right edge || (currentPos.x < 0) ) { // ...or left edge increment.x = -increment.x; // reverse direction } if( (currentPos.y > (DuckPond.PONDSIZE-image.getHeight(null))) // leaving the window's top edge || (currentPos.y < 0) ) {//... or bottom edge increment.y = -increment.y; // reverse direction } currentPos.x += increment.x; // increment the position of the duck currentPos.y += increment.y; // Create a new random increment for the next movement (the next time this method is called) // Note: increments between -2 and +2 will be generated. increment = new Point(random.nextInt(5)-2, random.nextInt(5)-2 ); setLocation(currentPos); // ...and update its location in the container } }