fork download
  1. package sortingstuff;
  2.  
  3. // @Author Damien Bell
  4. import java.awt.Color;
  5. import javax.swing.JButton;
  6. import javax.swing.JLabel;
  7. import javax.swing.Timer;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.ActionEvent;
  10. import javax.swing.event.ChangeEvent;
  11. import javax.swing.event.ChangeListener;
  12. import javax.swing.JPanel;
  13. import javax.swing.JFrame;
  14. import javax.swing.SwingUtilities;
  15. import java.util.Random;
  16. import java.awt.GridLayout;
  17. import javax.swing.BorderFactory;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.lang.Thread;
  21.  
  22.  
  23. public class SortingStuff extends JFrame{
  24. public SortingStuff(){
  25. initUI();
  26. }
  27.  
  28. public static class thread2 implements Runnable{
  29. public void run() {
  30. System.out.println("thread " +Thread.currentThread().getName());
  31. }
  32. public static void bubbleSort(final String numbers[], final JButton numButton[]){
  33. System.out.println("thread " +Thread.currentThread().getName());
  34. for (int k = 0; k < numbers.length - 1; k++)
  35. {
  36. boolean isSorted = true;
  37.  
  38. for (int i = 1; i < numbers.length - k; i++)
  39. {
  40. if (Integer.parseInt(numbers[i]) < Integer.parseInt(numbers[i - 1]) )
  41. {
  42. isSorted = false;
  43.  
  44. try{
  45. String tempVariable = numbers[i];
  46. numbers[i] = numbers[i - 1];
  47. numbers[i - 1] = tempVariable;
  48. String str1 = numButton[i].getText();
  49. numButton[i].setBackground(Color.RED);
  50. numButton[i-1].setBackground(Color.RED);
  51. Thread.currentThread().sleep(200);
  52. numButton[i].setText(numButton[i-1].getText());
  53. numButton[i-1].setText(str1);
  54. numButton[i].setBackground(null);
  55. numButton[i-1].setBackground(null);
  56. }catch(Exception e){ System.err.println("Error: " + e.getMessage());}
  57. int delay = 0; //milliseconds
  58. // ActionListener taskPerformer = new ActionListener() {
  59. // public void actionPerformed(ActionEvent evt) {
  60. // }
  61. // };
  62. // new Timer(delay, taskPerformer).start();
  63. }
  64. }
  65.  
  66. if (isSorted)
  67. break;
  68. }
  69. }
  70. }
  71.  
  72. JButton numButton[] = new JButton[12];
  73. String numbers[] = new String[12];
  74. JLabel spacer[]= new JLabel[7];
  75. JButton Bubble = new JButton();
  76. JButton Merge = new JButton();
  77. JButton Insertion = new JButton();
  78. JButton Heap = new JButton();
  79. JButton MergeSort = new JButton();
  80. JButton Randomize = new JButton();
  81.  
  82. public final void genRandom(String strArray[]){
  83. Random generator = new Random();
  84. for(int i=0; i<strArray.length; i++){
  85. strArray[i] = Integer.toString(Math.abs(generator.nextInt()%100));
  86. }
  87. }
  88.  
  89. public final void initUI(){
  90. JPanel panel = new JPanel();
  91. getContentPane().add(panel);
  92. panel.setBorder(BorderFactory.createEmptyBorder(2,13,4,5));
  93. panel.setLayout(new GridLayout(2,13,4,5));
  94.  
  95. genRandom(numbers);
  96.  
  97. for(int i=0; i<numbers.length; i++){
  98. numButton[i] = new JButton();
  99. numButton[i].setText(numbers[i]);
  100. panel.add(numButton[i]);
  101. }
  102. for(int i=0; i<4; i++){
  103. spacer[i] = new JLabel();
  104. spacer[i].setText("");
  105. panel.add(spacer[i]);
  106. }
  107.  
  108. Bubble.setText("Bubble Sort");
  109. Bubble.addActionListener(new ActionListener(){
  110. public void actionPerformed(ActionEvent ae){
  111. System.out.println("thread " +Thread.currentThread().getName());
  112. thread2.bubbleSort(numbers, numButton);
  113. }
  114. });
  115. panel.add(Bubble);
  116.  
  117. Merge.setText("Merge Sort");
  118. Merge.addActionListener(new ActionListener(){
  119. public void actionPerformed(ActionEvent ae){
  120. thread2.bubbleSort(numbers, numButton);
  121. }
  122. });
  123. panel.add(Merge);
  124.  
  125. Insertion.setText("Insertion Sort");
  126. Insertion.addActionListener(new ActionListener(){
  127. public void actionPerformed(ActionEvent ae){
  128. System.out.println("thread " +Thread.currentThread().getName());
  129. thread2.bubbleSort(numbers, numButton);
  130. }
  131. });
  132. panel.add(Insertion);
  133.  
  134. Heap.setText("Heap Sort");
  135. Heap.addActionListener(new ActionListener(){
  136. public void actionPerformed(ActionEvent ae){
  137. thread2.bubbleSort(numbers, numButton);
  138. }
  139. });
  140. panel.add(Heap);
  141.  
  142. Randomize.setText("Randomize");
  143. Randomize.addActionListener(new ActionListener(){
  144. public void actionPerformed(ActionEvent ae){
  145. genRandom(numbers);
  146. }
  147. });
  148. panel.add(Heap);
  149.  
  150. for(int i=4; i<7; i++){
  151. spacer[i] = new JLabel();
  152. spacer[i].setText("");
  153. panel.add(spacer[i]);
  154. }
  155.  
  156.  
  157. add(panel);
  158. setTitle("Test");
  159. setSize(1200,200);
  160. setLocationRelativeTo(null);
  161. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  162.  
  163.  
  164.  
  165. }
  166. public static void main(String[] args){
  167. SwingUtilities.invokeLater(new Runnable() {
  168. Thread thread = new Thread(new thread2());
  169. public void run(){
  170. thread.start();
  171. double startTime = System.nanoTime();
  172. SortingStuff ss = new SortingStuff();
  173. ss.setVisible(true);
  174. double endTime = System.nanoTime();
  175. double elapsedTime = endTime - startTime;
  176. System.out.println("This operation took " + elapsedTime + " nanoseconds, which is :" + ((elapsedTime/1000000)/1000) + " seconds.");
  177. }
  178. });
  179. }
  180. }
  181.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:23: class SortingStuff is public, should be declared in a file named SortingStuff.java
public class SortingStuff extends JFrame{
       ^
1 error
stdout
Standard output is empty