Last active
October 27, 2016 03:46
-
-
Save tranvanthuc/f236af43599440e89de638f171250394 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package src; | |
import java.awt.EventQueue; | |
import javax.swing.JFrame; | |
import java.awt.GridBagLayout; | |
import java.awt.FlowLayout; | |
import javax.swing.JTextField; | |
import javax.swing.ScrollPaneConstants; | |
import java.awt.GridLayout; | |
import javax.swing.JButton; | |
import javax.swing.JTextArea; | |
import javax.swing.JPanel; | |
import javax.swing.border.TitledBorder; | |
import javax.swing.UIManager; | |
import java.awt.Color; | |
import java.awt.Font; | |
import java.awt.event.ActionListener; | |
import java.io.FileNotFoundException; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
import java.util.stream.Stream; | |
import java.awt.event.ActionEvent; | |
import javax.swing.JScrollPane; | |
import java.awt.Component; | |
import javax.swing.border.EmptyBorder; | |
import javax.swing.JLabel; | |
public class SortGUI { | |
private JFrame frmSortgui; | |
private JTextField txtInput; | |
private JTextField txtSize; | |
private JTextField txtMin; | |
private JTextField txtMax; | |
/** | |
* Launch the application. | |
*/ | |
public static void main(String[] args) { | |
EventQueue.invokeLater(new Runnable() { | |
public void run() { | |
try { | |
SortGUI window = new SortGUI(); | |
window.frmSortgui.setVisible(true); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}); | |
} | |
/** | |
* Create the application. | |
*/ | |
public SortGUI() { | |
initialize(); | |
} | |
/** | |
* Initialize the contents of the frame. | |
*/ | |
private void initialize() { | |
frmSortgui = new JFrame(); | |
frmSortgui.setTitle("SortGUI"); | |
frmSortgui.setBounds(100, 100, 612, 490); | |
frmSortgui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frmSortgui.getContentPane().setLayout(null); | |
txtInput = new JTextField(); | |
txtInput.setFont(new Font("Consolas", Font.PLAIN, 17)); | |
txtInput.setToolTipText("Enter number"); | |
txtInput.setBounds(30, 11, 430, 25); | |
frmSortgui.getContentPane().add(txtInput); | |
txtInput.setColumns(10); | |
JButton btnRun = new JButton("Run"); | |
btnRun.setBounds(489, 11, 86, 25); | |
frmSortgui.getContentPane().add(btnRun); | |
JPanel panel = new JPanel(); | |
panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Result", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0))); | |
panel.setToolTipText(""); | |
panel.setBounds(20, 124, 554, 316); | |
frmSortgui.getContentPane().add(panel); | |
panel.setLayout(null); | |
JTextArea txtArea = new JTextArea(); | |
txtArea.setBorder(new EmptyBorder(10, 10, 10, 10)); | |
txtArea.setFont(new Font("Consolas", Font.PLAIN, 15)); | |
txtArea.setEditable(false); | |
txtArea.setToolTipText(""); | |
txtArea.setBounds(10, 27, 521, 264); | |
JScrollPane scroll = new JScrollPane (txtArea ); | |
scroll.setLocation(10, 25); | |
scroll.setSize(540, 266); | |
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); | |
panel.add(scroll); | |
txtSize = new JTextField(); | |
txtSize.setBounds(70, 53, 86, 25); | |
frmSortgui.getContentPane().add(txtSize); | |
txtSize.setColumns(10); | |
JLabel lblSize = new JLabel("Size :"); | |
lblSize.setBounds(30, 53, 30, 25); | |
frmSortgui.getContentPane().add(lblSize); | |
JButton btnRandom = new JButton("Random"); | |
btnRandom.setBounds(489, 54, 86, 25); | |
frmSortgui.getContentPane().add(btnRandom); | |
JLabel lblMin = new JLabel("Min :"); | |
lblMin.setBounds(181, 53, 30, 25); | |
frmSortgui.getContentPane().add(lblMin); | |
txtMin = new JTextField(); | |
txtMin.setColumns(10); | |
txtMin.setBounds(221, 53, 86, 25); | |
frmSortgui.getContentPane().add(txtMin); | |
JLabel lblMax = new JLabel("Max :"); | |
lblMax.setBounds(334, 53, 30, 25); | |
frmSortgui.getContentPane().add(lblMax); | |
txtMax = new JTextField(); | |
txtMax.setColumns(10); | |
txtMax.setBounds(374, 53, 86, 25); | |
frmSortgui.getContentPane().add(txtMax); | |
btnRun.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
ArrayList<Double> arrList = new ArrayList<>(); | |
txtArea.setText(""); | |
String text = ""; | |
try { | |
text = txtInput.getText(); | |
}catch(Exception ex){ | |
txtArea.setText(""+ex); | |
} | |
Scanner kb = new Scanner(text); | |
while(kb.hasNext()){ | |
double x = 0; | |
try { | |
x = kb.nextDouble(); | |
}catch(Exception ex){ | |
txtArea.setText("\nThese are NOT numbers :" + ex); | |
break; | |
} | |
arrList.add(x); | |
} | |
Double[] Arr = new Double[arrList.size()]; | |
arrList.toArray(Arr); | |
double[] arr = Stream.of(Arr).mapToDouble(Double::doubleValue).toArray(); | |
MainClass.setArray(arr); | |
String result ="Sort number : \n"+MainClass.toStringArray(); | |
try { | |
result += MainClass.mainBubbleSort(); | |
result += MainClass.mainSelectionSort(); | |
result += MainClass.mainInsertionSort(); | |
result += MainClass.mainTopDownMergeSort(); | |
result += MainClass.mainHeapSort(); | |
result += MainClass.mainQuickSort(); | |
result += MainClass.mainShellSort(); | |
result += MainClass.mainCombSort(); | |
txtArea.setText(result); | |
}catch (Exception ex){ | |
txtArea.setText(""+ex); | |
} | |
} | |
}); | |
btnRandom.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
try { | |
int size = Integer.parseInt(txtSize.getText()); | |
int min = Integer.parseInt(txtMin.getText()); | |
int max = Integer.parseInt(txtMax.getText()); | |
MainClass.createRandomArray(size, min, max); | |
txtArea.setText(""); | |
String result ="Sort number : \n"+MainClass.toStringArray(); | |
result += "\nTime unit : nanoSecond"; | |
result += MainClass.mainBubbleSort(); | |
result += MainClass.mainSelectionSort(); | |
result += MainClass.mainInsertionSort(); | |
result += MainClass.mainTopDownMergeSort(); | |
result += MainClass.mainHeapSort(); | |
result += MainClass.mainQuickSort(); | |
result += MainClass.mainShellSort(); | |
result += MainClass.mainCombSort(); | |
txtArea.setText(result); | |
}catch(Exception ex){ | |
txtArea.setText("" +ex); | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment