import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Random; // swing at // http://java.sun.com/docs/books/tutorial/uiswing/components/components.html public class SortList_demo3 extends JApplet implements ActionListener, MouseListener { // Variables declaration. For this applet, not all // declared labels and textfields are used private boolean isSorted=false; private int sortSrc=0; private boolean hasClicked=false; private int nCount=5; private int [] nums; private JPanel [] pNums; private JLabel [] lNums; private JPanel pMain; private int lang=0; private JTextField tCount; private JButton bMelange; private JButton bStepSort; private Container appletPanel; private JPanel progPanel; private Color bgColor; private JComboBox cbSelSort; String[] sortList = { "Bubble Sort", "Bogosort Sort" }; private int sortType; private static final int SORT_BUBBLE = 0; private static final int SORT_BOGOSORT = 1; private JTextArea tLog; public void init() { setSize(nCount*40,150); bgColor=new Color(254,220,40); JFrame.setDefaultLookAndFeelDecorated(true); appletPanel = getContentPane(); appletPanel.setLayout(new BorderLayout()); progPanel = new JPanel(); progPanel.setLayout(new BorderLayout()); tLog = new JTextArea("Applet Started"); tLog.setEditable(false); tLog.setRows(2); JScrollPane scrollPane = new JScrollPane(tLog, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); //panels pMain=new JPanel(); pMain.setLayout(new GridLayout(1,nCount)); pMain.setBackground(bgColor); // settin up the ComboBox cbSelSort = new JComboBox(sortList); cbSelSort.addActionListener(this); sortType = SORT_BUBBLE; progPanel.add(cbSelSort, BorderLayout.NORTH); progPanel.add(pMain,BorderLayout.CENTER); progPanel.setBackground(bgColor); pNums = new JPanel[nCount]; lNums = new JLabel[nCount]; for (int i=0;i=pVal) { pVal=nums[i]; } else { isSorted=false; return; } } appendLog("We have a sort!"); isSorted=true; } public void appendLog(String text) { tLog.append("\n" + text); tLog.setCaretPosition(tLog.getDocument().getLength()); } //ActionListener interface public void actionPerformed(ActionEvent e) { // is it the combo box? if (e.getSource()==bStepSort) { if (isSorted) return; switch (sortType) { case SORT_BUBBLE: bubbleSort(); break; case SORT_BOGOSORT: bogoSort(); break; } //check for sort checkSort(); } else if (e.getSource()==cbSelSort) { sortType = cbSelSort.getSelectedIndex(); } // is it the Melange button else if (e.getSource()==bMelange) { melanger(); } } //MouseListener interface public void mousePressed(MouseEvent e) { if (isSorted) return; for (int i=0;i nums[j]) { // swap! appendLog("Switched " + nums[i] + " with " + nums[j] ); int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; lNums[i].setText(""+nums[i]); lNums[j].setText(""+nums[j]); return; } } } } public void bogoSort() { Random generator = new Random(); int i, j; do { i = generator.nextInt(nCount); j = generator.nextInt(nCount); } while ( i == j); appendLog("Switched " + nums[i] + " with " + nums[j] ); int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; lNums[i].setText(""+nums[i]); lNums[j].setText(""+nums[j]); } }