/** * Class DelayThread extends the Thread object. Its only purpose is to cause * a delay to allow the GUI to be repainted before the next part of the sort * occurs. * * @author David Thibodeau * @version 7-Aug-2002 */ public class DelayThread extends Thread { // instance variables - replace the example below with your own private int delay; private Thread delayer; /** * Constructor for objects of class DelayThread * @param int delay - determines length of delay */ public DelayThread(int delay) { this.delay = delay; // determines length of delay } /** * run method - this method creates the delay * * @param void - no data required * @return void - no data returned */ public void run() { long tm = System.currentTimeMillis(); // get current time try { tm += delay; Thread.sleep(Math.max(0, tm - System.currentTimeMillis() ) ); } catch (InterruptedException e) { } return; } // end run method } // end Class DelayThread /////////////////////////////////////////////////////////// /** * Class SortGUI creates a front end where the user creates an array * of 10 random integers. Then, the user can choose 1 of 3 different * sort types to sort the data. While the sort is in progress, the * array elements being sorted flash red. Once the elements have * been sorted, the background is turned green. At the end of the * sort, all elements will be green. Clicking on the New Array button * resets all of the elements to a white background and creates a new * array of random numbers. * * @author David Thibodeau * @version 5-Aug-2002 */ import java.awt.* ; import java.awt.event.*; import javax.swing.*; import java.util.Random; public class SortGUI extends JFrame implements ActionListener { // data - Data array display textfields JLabel dataLabel; JButton dataButton; JTextField[] dataText; JPanel dataPanel; JPanel dataLabelPanel; JPanel dataTextPanel; JPanel dataButtonPanel; // sort - Holds sort-type radio buttons JLabel sortLabel; JButton sortButton; JRadioButton sortBubbleRadioButton; JRadioButton sortSelectionRadioButton; JRadioButton sortInsertionRadioButton; ButtonGroup sortType; // Groups the radio buttons JPanel sortPanel; JPanel sortLabelPanel; JPanel sortRadioButtonPanel; JPanel sortButtonPanel; private DelayThread delay; private final int DELAYLENGTH = 1000; // set length of DelayThread private final int ARRAYSIZE = 10; // Sets size of the sort array /** * Constructor for objects of class SortGUI */ public SortGUI() { setTitle( "Sorting Example" ); setDefaultCloseOperation( EXIT_ON_CLOSE ); // data group dataLabel = new JLabel( "Unsorted Array:" ); dataButton = new JButton( "New Array" ); dataButton.addActionListener(this); dataButton.setActionCommand( "NewArray" ); // The following code creates the panel to hold the data GUI // elements. The dataText array is initialized and placed into // the dataTextPanel at the same time by using a for loop. dataTextPanel = new JPanel(); dataTextPanel.setLayout( new BoxLayout( dataTextPanel, BoxLayout.X_AXIS ) ); dataText = new JTextField[ARRAYSIZE]; for(int i = 0; i < ARRAYSIZE; i++) { dataText[i] = new JTextField(5); dataText[i].setEditable( false ); dataTextPanel.add ( dataText[i] ); } // end for dataLabelPanel = new JPanel(); dataLabelPanel.add( dataLabel ); dataButtonPanel = new JPanel(); dataButtonPanel.add( dataButton ); dataPanel = new JPanel(); dataPanel.setLayout( new BoxLayout( dataPanel, BoxLayout.Y_AXIS ) ); dataPanel.add ( dataLabelPanel ); dataPanel.add ( dataTextPanel ); dataPanel.add ( dataButtonPanel ); // sort group sortLabel = new JLabel( "Cannot sort until array has been created." ); sortButton = new JButton( "Sort Array" ); sortButton.addActionListener(this); sortButton.setActionCommand( "SortArray" ); sortBubbleRadioButton = new JRadioButton( "Bubble Sort", true ); sortBubbleRadioButton.addActionListener(this); sortBubbleRadioButton.setActionCommand( "BubbleSort" ); sortSelectionRadioButton = new JRadioButton( "Selection Sort", false ); sortSelectionRadioButton.addActionListener(this); sortSelectionRadioButton.setActionCommand( "SelectionSort" ); sortInsertionRadioButton = new JRadioButton( "Insertion Sort", false ); sortInsertionRadioButton.addActionListener(this); sortInsertionRadioButton.setActionCommand( "InsertionSort" ); sortType = new ButtonGroup(); sortType.add ( sortBubbleRadioButton ); sortType.add ( sortSelectionRadioButton ); sortType.add ( sortInsertionRadioButton ); sortLabelPanel = new JPanel(); sortLabelPanel.add( sortLabel ); sortRadioButtonPanel = new JPanel(); sortRadioButtonPanel.setLayout( new BoxLayout( sortRadioButtonPanel, BoxLayout.X_AXIS ) ); sortRadioButtonPanel.add( sortBubbleRadioButton ); sortRadioButtonPanel.add( sortSelectionRadioButton ); sortRadioButtonPanel.add( sortInsertionRadioButton ); sortButtonPanel = new JPanel(); sortButtonPanel.add( sortButton ); sortPanel = new JPanel(); sortPanel.setLayout( new BoxLayout( sortPanel, BoxLayout.Y_AXIS ) ); sortPanel.add ( sortLabelPanel ); sortPanel.add ( sortRadioButtonPanel ); sortPanel.add ( sortButtonPanel ); // content pane getContentPane().setLayout( new BoxLayout( getContentPane(), BoxLayout.Y_AXIS ) ); getContentPane().add( dataPanel ); getContentPane().add( sortPanel ); } /** * actionPerformed method. This method handles the GUI click events * for the application. In the data section, clicking the button * creates a new array of random integers. The sort section holds 3 * radio buttons which determine what type of sort is performed. * Clicking the button performs the sort. * * @param ActionEvent supplied by Java's Swing event handler * @return void */ public void actionPerformed( ActionEvent evt ) { if ( evt.getActionCommand().equals( "NewArray" ) ) { dataLabel.setText( "Unsorted Array:" ); for(int j = 0; j < ARRAYSIZE; j++) { // (re)set textfields to white background dataText[j].setBackground( Color.white ); } // End for Random number = new Random(); String snum = ""; for(int i = 0; i < ARRAYSIZE; i++) { snum = String.valueOf(number.nextInt() % 1001); dataText[i].setText( snum ); // Selects random numbers, limiting choices between -1000 & 1000 } // End for loop sortLabel.setText( "Select a sort type and click Sort Array" ); } else if( evt.getActionCommand().equals( "SortArray" ) ) { if( dataText[0].getText() == null ) { return; // do nothing } else { if( sortBubbleRadioButton.isSelected() ) { bubbleSort(); // Call method that does bubble sort dataLabel.setText( "Sorted Array:" ); } else if( sortSelectionRadioButton.isSelected() ) { selectionSort(); // Call method that does selection sort dataLabel.setText( "Sorted Array:" ); } else if( sortInsertionRadioButton.isSelected() ) { insertionSort(); // Call method that does insertion sort dataLabel.setText( "Sorted Array:" ); } } // End else } // End else if( evt.getActionCommand().equals( "SortArray" ) ) } // End actionPerformed( ActionEvent evt ) method /** * bubbleSort method - Uses the bubble sort algorithm to sort * an array of random integers ranging between -1000 & +1000. * @param void - no data required * @return void - no data returned */ private void bubbleSort() { int out, in; // loop variables int elementA, elementB; for(out = ARRAYSIZE - 1; out > 0; out--) { for(in = 0; in < out; in++) { elementA = Integer.parseInt( dataText[in].getText() ); elementB = Integer.parseInt( dataText[in+1].getText() ); if( elementA > elementB ) { dataText[in].setBackground( Color.red ); dataText[in+1].setBackground( Color.red ); swap(in, in+1); // Calls swap method } // End if dataText[in].setBackground( Color.white ); dataText[in+1].setBackground( Color.white ); } // End for(in = 0; in < out; in++) loop dataText[out].setBackground( Color.green ); } // End for(out = ARRAYSIZE - 1; out > 1; out--) loop dataText[0].setBackground( Color.green ); repaint(); } /** * selectionSort method - Uses the selection sort algorithm to sort * an array of random integers ranging between -1000 & +1000. * @param void - no data required * @return void - no data returned */ private void selectionSort() { int out, in, min; for(out = 0; out < ARRAYSIZE - 1; out++) { min = out; for(in = out + 1; in < ARRAYSIZE; in++) { if( (Integer.parseInt( dataText[in].getText() )) < (Integer.parseInt( dataText[min].getText() )) ) { min = in; dataText[out].setBackground( Color.red ); dataText[min].setBackground( Color.red ); } // End if } // End for(in = 0; in < out; in++) loop swap(out, min); // Calls swap method dataText[min].setBackground( Color.white ); dataText[out].setBackground( Color.green ); } // End for(out = ARRAYSIZE - 1; out > 1; out--) loop dataText[ARRAYSIZE - 1].setBackground( Color.green ); // set last textfield to green repaint(); } /** * insertionSort method - Uses the insertion sort algorithm * to sort an array of random integers ranging between * -1000 & +1000. * @param void - no data required * @return void - no data returned */ private void insertionSort() { int out, in, min; int[] checkSorted = new int[ARRAYSIZE]; // array to hold if values are sorted boolean[] isSorted = new boolean[ARRAYSIZE]; int max; for(out = 1; out < ARRAYSIZE ; out++) { int temp = Integer.parseInt( dataText[out].getText() ); in = out; while(in > 0 && (Integer.parseInt( dataText[in-1].getText() ) >= temp) ) { dataText[in].setBackground( Color.red ); dataText[in-1].setBackground( Color.red ); swap(in, in-1); dataText[in].setBackground( Color.white ); dataText[in-1].setBackground( Color.white ); --in; // the next section of code checks to see if // any of the numbers are in final sorted order for( int loop = 0; loop < ARRAYSIZE; loop++ ) { checkSorted[loop] = Integer.parseInt( dataText[loop].getText() ); } // end for loop for( int i = ARRAYSIZE - 1; i > -1; i-- ) { max = checkSorted[i]; for( int j = 0; j < i; j++ ) { if(max < checkSorted[j]) { max = checkSorted[j]; j = i; // end loop } } // end for j loop if (max == checkSorted[i]) { isSorted[i] = true; } else { isSorted[i] = false; i = -1; // escapes from loop } } // end for i loop for( int setTheColor = 0; setTheColor < ARRAYSIZE; setTheColor++ ) { if (isSorted[setTheColor] == true) { dataText[setTheColor].setBackground( Color.green ); } else { dataText[setTheColor].setBackground( Color.white ); } } // end for setTheColor loop } // end while dataText[in].setText(String.valueOf(temp) ); } // End for repaint(); } // end insertionSort method /** * swap method - swaps the values of the two textfields * based on the textfield index values passed to it. * @param int one - The first textfield index * @param int two - The second textfield index * @return void - no data returned */ private void swap(int one, int two) { int temp = Integer.parseInt( dataText[one].getText() ); dataText[one].setText( dataText[two].getText() ); dataText[two].setText( String.valueOf( temp ) ); // run the delay thread delay = new DelayThread(DELAYLENGTH); delay.start(); // run the delay thread while( delay.isAlive() ); { paintAll(getContentPane().getGraphics()); } } // end swap(int one, int two) method /** * main method. This creates a new instance of SortGUI, then * instantiates a new WindowQuitter() object and lastly sets the * size and visibility of the GUI window. */ public static void main ( String[] args ) { // New GUI instance SortGUI sortApp = new SortGUI(); // New WindowQuitter instance WindowQuitter wquit = new WindowQuitter(); sortApp.addWindowListener( wquit ); // Set window size & make visible sortApp.setSize( 400, 200 ); sortApp.setVisible( true ); } // end main } // End Class SortGUI ////////////////////////////////////////////// class WindowQuitter extends WindowAdapter { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } }