import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* @author Dung X. Nguyen
* @author Stephen B. Wong
*/
public class SortGUI extends JFrame {
private BorderLayout _borderLayout1 = new BorderLayout(); // overall layout manager.
private JButton _randomizeBtn = new JButton ("Randomize");
private JButton _sortBtn = new JButton ("Sort");
private JPanel _jPanel1 = new JPanel(); // for the randomize and sort butons.
private JTextArea _outputTA = new JTextArea(); // displays the array model in text.
private JScrollPane _jScrollPane1 = new JScrollPane(); // to scroll _outputTA.
private JRadioButton _insertionRBtn = new JRadioButton ("Insertion Sort");
private JRadioButton _quickRBtn = new JRadioButton ("Quick Sort");
private JRadioButton _mergeRBtn = new JRadioButton ("Merge Sort");
private JRadioButton _selectionRBtn = new JRadioButton ("Selection Sort");
private JPanel _jPanel2 = new JPanel(); // for the sort radio buttons.
private GridLayout _gridLayout1 = new GridLayout(); // lays out the sort radio buttons.
private ButtonGroup _buttonGroup = new ButtonGroup(); // to group the sort radio buttons as exclusive choices.
private GraphCanvas _graphCanvas = new GraphCanvas(); // graphs the array model
public SortGUI() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
initGUI();
}
catch(Exception e) {
e.printStackTrace();
}
_buttonGroup.add(_insertionRBtn);
_buttonGroup.add(_quickRBtn);
_buttonGroup.add(_mergeRBtn);
_buttonGroup.add(_selectionRBtn);
}
//Component initialization
private void initGUI() throws Exception {
setSize (new Dimension(600, 450));
setTitle ("Sort Animation");
_jPanel1.add (_randomizeBtn, null);
_jPanel1.add (_sortBtn, null);
_gridLayout1.setColumns (1);
_gridLayout1.setRows (0);
_jPanel2.setLayout (_gridLayout1);
_selectionRBtn.setToolTipText ("");
_graphCanvas.setPreferredSize (new Dimension( 400, 400));
_jPanel2.add (_insertionRBtn, null);
_jPanel2.add (_selectionRBtn, null);
_jPanel2.add (_mergeRBtn, null);
_jPanel2.add (_quickRBtn, null);
_jScrollPane1.getViewport ().add (_outputTA, null);
getContentPane().setLayout (_borderLayout1);
getContentPane().add (_graphCanvas, BorderLayout.EAST);
getContentPane().add (_jPanel1, BorderLayout.NORTH);
getContentPane().add (_jPanel2, BorderLayout.WEST);
getContentPane ().add (_jScrollPane1, BorderLayout.CENTER);
}
//Overridden so we can exit on System Close
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public JTextArea getOutputTA() {
return _outputTA;
}
public JRadioButton getInsertionRBtn() {
return _insertionRBtn;
}
public JRadioButton getQuickRBtn() {
return _quickRBtn;
}
public JRadioButton getMergeRBtn() {
return _mergeRBtn;
}
public JRadioButton getSelectionRBtn() {
return _selectionRBtn;
}
public JButton getRandomizeBtn() {
return _randomizeBtn;
}
public JButton getSortBtn() {
return _sortBtn;
}
public GraphCanvas getGraphCanvas () {
return _graphCanvas;
}
}