/*------------------------------------------------------------------- Sorter class -----------------------------------------------------------------*/ import java.lang.*; import java.applet.*; import java.awt.*; import java.util.*; public class Sorter extends Applet { public static final int MAX = 10; Random random = new Random(); Vector blocks = new Vector(); Button randomizeButton = new Button( "randomize" ); Button selectionButton = new Button( "selection sort" ); Button insertionButton = new Button( "insertion sort" ); Button bubbleButton = new Button( "bubble sort" ); BlocksCanvas blocksCanvas = null; SelectionCanvas selectionCanvas = null; InsertionCanvas insertionCanvas = null; BubbleCanvas bubbleCanvas = null; /*--------------------------------------------------------------- init() This method overrides the standard init() method in the Applet class. It constructs the following components: (1) a Vector of 10 Blocks, each with a different color and a random numeric value (2) a Canvas for displaying the blocks, in unsorted order (3) three Canvases for displaying each of three sorting algorithms as the blocks are sorted It then creates a layout manager and lays out all the graphical components in the Applet. -------------------------------------------------------------*/ public void init() { blocks.addElement( new Block( random,Color.blue )); blocks.addElement( new Block( random,Color.cyan )); blocks.addElement( new Block( random,Color.lightGray )); blocks.addElement( new Block( random,Color.green )); blocks.addElement( new Block( random,Color.magenta )); blocks.addElement( new Block( random,Color.orange )); blocks.addElement( new Block( random,Color.pink )); blocks.addElement( new Block( random,Color.red )); blocks.addElement( new Block( random,Color.white )); blocks.addElement( new Block( random,Color.yellow )); blocksCanvas = new BlocksCanvas( this ); selectionCanvas = new SelectionCanvas( this ); insertionCanvas = new InsertionCanvas( this ); bubbleCanvas = new BubbleCanvas( this ); selectionButton.disable(); insertionButton.disable(); bubbleButton.disable(); this.setFont( new Font( "Helvetica",Font.PLAIN,10 )); GridBagLayout layout = new GridBagLayout(); setLayout( layout ); addComponent( randomizeButton,this,layout, 0,0,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,0,0 ); addComponent( blocksCanvas,this,layout, 0,1,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,0,0 ); addComponent( selectionButton,this,layout, 1,0,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,0,0 ); addComponent( selectionCanvas,this,layout, 1,1,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,0,0 ); Label spacer2 = new Label( " " ); addComponent( spacer2,this,layout, 2,1,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,0,0 ); addComponent( insertionButton,this,layout, 3,0,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,0,0 ); addComponent( insertionCanvas,this,layout, 3,1,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,0,0 ); Label spacer4 = new Label( " " ); addComponent( spacer4,this,layout, 4,1,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,0,0 ); addComponent( bubbleButton,this,layout, 5,0,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,0,0 ); addComponent( bubbleCanvas,this,layout, 5,1,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,0,0 ); validate(); randomize(); } // end of init() method /*--------------------------------------------------------------- randomize() -------------------------------------------------------------*/ public void randomize() { Block block; for ( int i=0; i