package class2_1_DuckStrategies_v8_FactoryMethodPattern.ducks; import class2_1_DuckStrategies_v8_FactoryMethodPattern.DuckPond; import class2_1_DuckStrategies_v8_FactoryMethodPattern.SwimBehavior; public class StandardSwimming implements SwimBehavior { /** * The duck that uses this behavior */ private final Duck parent; public StandardSwimming(Duck parent) { this.parent = parent; } /** * Standard bounce-off-the-walls swimming */ @Override public void swim() { if( (parent.currentPos.x > (DuckPond.PONDSIZE-parent.image.getWidth(null))) // leaving the window's right edge || (parent.currentPos.x < 0) ) { // ...or left edge parent.increment.x = -parent.increment.x; // reverse direction parent.performQuacking(); // quack each time we reverse direction } if( (parent.currentPos.y > (DuckPond.PONDSIZE-parent.image.getHeight(null))) // leaving the window's top edge || (parent.currentPos.y < 0) ) {//... or bottom edge parent.increment.y = -parent.increment.y; // reverse direction } parent.currentPos.x += parent.increment.x; // increment the position of the duck parent.currentPos.y += parent.increment.y; parent.setLocation(parent.currentPos); // ...and update its location in the container } }