/**
*Midterm Project 1
*September 7 & 8,2013
*name:BubbleSort_GraphicalUserInterface
*Programmed by: Hanthony Cyrey Villaceran Tagam
*/
import javax.swing.*; //<-------------importing package to be passed on code
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class BubbleSort_GraphicalUserInterface //<--------------class bubble sort
{
JFrame Bubble_Sort = new JFrame("Tonyo Bubble Sort"); //<--------------frame
//<--------------//building a constructor using J for swing
//<--------------//assigning variable names unto Swing Components
JPanel line1 = new JPanel(); //<--------------//panels
JPanel line2 = new JPanel();
JPanel line3 = new JPanel();
JLabel descrip = new JLabel("fill the blanks"); //<--------------//labels
JLabel display = new JLabel();
int tagam[] = new int[5]; // <---------------//linklist
JTextField SortField1 = new JTextField(7); //<---------------//textfields
JTextField SortField2 = new JTextField(7);
JTextField SortField3 = new JTextField(7);
JTextField SortField4 = new JTextField(7);
JTextField SortField5 = new JTextField(7);
int TonyoSortArray[] = new int[5]; //<---------------//int array that stores 5 elements on TonyoSortArray
JButton AscendSort = new JButton("Ascend"); //<---------------//buttons
JButton DescendSort = new JButton("Descend");
JButton ClearSort = new JButton("Clear");
JButton ExitSort = new JButton("Exit");
JTextArea SortTextArea = new JTextArea("") ; //<---------------//textArea
JScrollPane TextAreaScrollPane = new JScrollPane();
public void frame() //<---------------//frame costumization
{
Bubble_Sort.setVisible(true);
Bubble_Sort.setLayout(new BorderLayout(20,20));
Bubble_Sort.setSize(500,200);
Bubble_Sort.setResizable(true);
Bubble_Sort.getContentPane().setBackground(Color.black);
Bubble_Sort.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
line1.setBackground(Color.cyan); //<---------------//panel costumization
line2.setBackground(Color.cyan);
FlowLayout customLayout = new FlowLayout();
line2.setLayout(customLayout);
line3.setBackground(Color.cyan);
descrip.setForeground(Color.red); //<---------------//descrip label costumization
descrip.setFont(new Font("Duplexide",Font.PLAIN,14));
AscendSort.setBackground(Color.cyan); //<---------------//ascend button costumization
AscendSort.setForeground(Color.red);
DescendSort.setBackground(Color.cyan); //<---------------//descend button costumization
DescendSort.setForeground(Color.red);
ClearSort.setBackground(Color.cyan); //<---------------//clear button costumization
ClearSort.setForeground(Color.red);
ExitSort.setBackground(Color.cyan); //<---------------//exit button costumization
ExitSort.setForeground(Color.red);
}
public void ascendAction() //<---------------//method ascend action
{
AscendSort.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
TonyoSortArray[0] = Integer.parseInt(SortField1.getText());
TonyoSortArray[1] = Integer.parseInt(SortField2.getText());
TonyoSortArray[2] = Integer.parseInt(SortField3.getText());
TonyoSortArray[3] = Integer.parseInt(SortField4.getText());
TonyoSortArray[4] = Integer.parseInt(SortField5.getText());
int temp = 0;
for (int counter = 0; counter < TonyoSortArray.length - 1; counter++)
{
for (int index = 0; index < TonyoSortArray.length - 1 - counter; index++)
{
if (TonyoSortArray[index] > TonyoSortArray[index + 1])
{
temp = TonyoSortArray[index];
TonyoSortArray[index] = TonyoSortArray[index + 1];
TonyoSortArray[index + 1] = temp;
}
}
}
for (int i = 0; i < TonyoSortArray.length; i++)
{
}
SortTextArea.append("\n Ascended:" + TonyoSortArray[0] + " " + TonyoSortArray[1] + " " + TonyoSortArray[2]+ " " + TonyoSortArray[3] + " " + TonyoSortArray[4] );
SortField1.setFocusable(true);
}
});
}
public void descendAction() //<---------------//method Descend Action
{
DescendSort.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
TonyoSortArray[0] = Integer.parseInt(SortField1.getText());
TonyoSortArray[1] = Integer.parseInt(SortField2.getText());
TonyoSortArray[2] = Integer.parseInt(SortField3.getText());
TonyoSortArray[3] = Integer.parseInt(SortField4.getText());
TonyoSortArray[4] = Integer.parseInt(SortField5.getText());
int temp = 0;
for (int counter = 0; counter < TonyoSortArray.length - 1; counter++)
{
for (int index = 0; index < TonyoSortArray.length - 1 - counter; index++)
{
if (TonyoSortArray[index] > TonyoSortArray[index + 1])
{
temp = TonyoSortArray[index];
TonyoSortArray[index] = TonyoSortArray[index + 1];
TonyoSortArray[index + 1] = temp;
}
}
}
for (int i = 0; i < TonyoSortArray.length; i++)
{
}
SortTextArea.append("\n Descended:" + TonyoSortArray[4] + " " + TonyoSortArray[3] + " " + TonyoSortArray[2]+ " " + TonyoSortArray[1] + " " + TonyoSortArray[0] );
}
});
}
public void clearAction() //<---------------//method Clear Action
{
ClearSort.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
SortField1.setText("");
SortField2.setText("");
SortField3.setText("");
SortField4.setText("");
SortField5.setText("");
SortTextArea.setText("");
}
});
}
public void exitAction() //<---------------//method Exit Action
{
ExitSort.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
public BubbleSort_GraphicalUserInterface()
{
frame(); //<--------------- method can be called here
ascendAction();
descendAction();
clearAction();
exitAction();
Bubble_Sort.setFont(new Font("Baskerville Old Face",Font.PLAIN,12)); //<---------------//setting its layout
Bubble_Sort.setLayout(new BorderLayout(10,10));
Bubble_Sort.add(line1,BorderLayout.NORTH); //<---------------//adding variable components to its frame
Bubble_Sort.add(line2,BorderLayout.SOUTH);
Bubble_Sort.add(line3,BorderLayout.WEST);
line1.add(descrip); //<---------------//setting components unto panel
line2.add(SortField1);
line2.add(SortField2);
line2.add(SortField3);
line2.add(SortField4);
line2.add(SortField5);
line3.add(AscendSort);
line3.add(DescendSort);
line3.add(ClearSort);
line3.add(ExitSort);
/**Bubble_Sort.add(descrip);
Bubble_Sort.add(nameSortField);
Bubble_Sort.add(contactSortField);
Bubble_Sort.add(AscendSort);
Bubble_Sort.add(DescendSort);
Bubble_Sort.add(ClearSort);
Bubble_Sort.add(ExitSort);
*/
Bubble_Sort.add(SortTextArea);
Bubble_Sort.add(TextAreaScrollPane);
TextAreaScrollPane = new JScrollPane(SortTextArea);
Bubble_Sort.add(TextAreaScrollPane);
}
public static void main(String []args)
{
new BubbleSort_GraphicalUserInterface();
}
}