/*
* @(#)BalanceBSTAnimation.java
*
* Last Modified: 9/15/01
*/
import java.util.*;
import java.awt.*;
/** *
* The Animation object that defines the Balancing of a node in a BSTTree. Two constructors exist,
* one setting the starting string command and step time (preferred use). The other uses defaults.
* The animation builds PartitionBSTAnimation
s from the head node as it goes, keeping
* only one currently animating and allowing rewinding only to the previous
* rotation. The color schemes are set by the head call. The call is
* makePartitionAnimation
.
* * @author Corey Sanders * @version 1.4 9/15/01 */ public class BalanceBSTAnimation extends AbstractAnimation implements AnimationListener { /** * Refers to the current PartitionBSTAnimation being drawn. */ PartitionBSTAnimation currentPartition; /** * Holds the node that is replacing. */ private BSTTree node; /** * Holds the node that is replacing. */ private BSTTree replacingNode; /** * Private doubles used to hold the current location steps. */ private double currentLocation = 0.0; /** * The constructor which initiates the status. The node * which is being balanced must be passed. * * @param node the BSTTree from which the balancing takes place. * @param startingCmd the Animation command that this should start. * @param stepTime the time for each step of the Animation. Sets the initial value. */ public BalanceBSTAnimation(BSTTree node, String startingCmd, int stepTime) { super(); setNode(node); setStartingCommand(startingCmd); setStepTime(stepTime); } /** * The constructor which initiates the status and sets the starting command and step time * * @param node the BSTTree which is balanced. */ public BalanceBSTAnimation(BSTTree node) { this(node, Animation.PLAY, DEFAULT_STEP); } /************************/ /* Accessor methods */ /************************/ /** * Gets the node from which the balancing takes place. * * @return BSTTree of the node currently being balanced. */ public BSTTree getNode() { return node; } /** * Gets the node currently being replaced by the node being balanced (not set until after partition occurs). * * @return BSTTree of the node currently being replaced and animated. */ public BSTTree getReplacingNode() { return replacingNode; } /************************/ /* Mutator methods */ /************************/ /** * Sets the node from which the balancing takes place. * * @param node BSTTree of the node currently being balanced. */ public void setNode(BSTTree node) { this.node = node; } /** * Sets the node that will replace the balanced node. * * @param BSTTree of the node replacing the balancing node. */ public void setReplacingNode(BSTTree node) { replacingNode = node; } /*********************/ /* Animation methods */ /*********************/ /** * Draws the animation of the next step, using the status of the animation (Animation.PLAY, Animation.PAUSE and so forth). * After completing the drawing, the Animation sends an AnimationEvent to all its listeners, indicating * any information that the listerners may wish to use.
* BSTTreeHead calls: *
selectTreeType
- to locate the replacing node without moving itbalance
- to make recursive calls once the animation has completedanimationEventPerformed
method is called.
*
* @param cmd String Animation command passed instead of the current Status.
* @param description String description for messages.
*/
protected void animationAction(String cmd, String description) {
super.animationAction(AnimationEvent.BALANCE_BST_ANIMATION, cmd, description, currentLocation);
}
/**
* Implements AnimationListener
which requires the following method.
* The only status of animation it listens for is Animation.ANIMATION_MESSAGE
, to pass
* the message on.
*
* @param e AnimationEvent that represents the information of the Animation.
*/
public void animationEventPerformed(AnimationEvent e) {
// set location each progress call.
currentLocation = e.getProgress();
if (e.getStatus().equals(Animation.ANIMATION_MESSAGE)) {
messageAction(e.getAnimationDescription());
}
if (e.getStatus().equals(Animation.STEP)) {
animationAction(Animation.STEP, null);
}
}
}