mga bossing. here is my program. kanang algorithm in sorting ug search. ang akong problema ba kai kining JScrollPane sa JList di man mugana oe. pls help. mao ni screenshot
here are my codes. scratch akong paghimo ani kai di ko kamao sa built-in. anyone can help especially
Superstar, stealthghost, and etc. pls help. tnx
pls read the red lines. tnx
Code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class SortingGUI extends JFrame{
private JLabel inputL, searchValueL;
private JTextField inputTF, searchValue;
private JButton QuickSort, InsertionSort,BinarySearch,
MergeSort, SelectionSort, BubbleSort;
private JTextArea output;
private int array [];
private Container pane;
private JList list;
private JTextArea area;
public SortingGUI(){
super( "Sorting GUI" );
this.pane = getContentPane();
this.pane.setBackground( new Color ( 0, 200, 200 ) );
this.pane.setLayout( null );
this.inputTF = new JTextField( );
this.inputTF.setSize( 500 , 25 );
this.inputTF.setLocation( 200, 20 );
this.pane.add( this.inputTF );
this.inputL = new JLabel( "Input values:" );
this.inputL.setSize( 100, 25 );
this.inputL.setLocation( 50, 20 );
this.pane.add( this.inputL );
this.BinarySearch = new JButton( "Binary Search" );
this.BinarySearch.setSize( 150, 30 );
this.BinarySearch.setLocation( 20 , 200 );
this.pane.add( this.BinarySearch );
this.BubbleSort = new JButton( "Bubble Sort" );
this.BubbleSort.setSize( 150, 30 );
this.BubbleSort.setLocation( 20, 240 );
this.pane.add( this.BubbleSort );
this.InsertionSort = new JButton( "Insertion Sort" );
this.InsertionSort.setSize( 150, 30 );
this.InsertionSort.setLocation( 20, 280 );
this.pane.add( this.InsertionSort );
this.MergeSort = new JButton( "Merge Sort" );
this.MergeSort.setSize( 150, 30 );
this.MergeSort.setLocation( 20, 320 );
this.pane.add( this.MergeSort );
this.QuickSort = new JButton( "QuickSort" );
this.QuickSort.setSize( 150, 30 );
this.QuickSort.setLocation( 20, 360 );
this.pane.add( this.QuickSort );
this.SelectionSort = new JButton( "Selection Sort" );
this.SelectionSort.setSize( 150, 30 );
this.SelectionSort.setLocation( 20, 400 );
this.pane.add( this.SelectionSort );
this.searchValue = new JTextField();
this.searchValue.setSize( 40, 25 );
this.searchValue.setLocation( 265, 80 );
this.pane.add( this.searchValue );
this.searchValueL = new JLabel();
this.searchValueL.setText( "Input a value to for Binary Search:" );
this.searchValueL.setSize( 300, 25 );
this.searchValueL.setLocation( 20, 80 );
this.pane.add( this.searchValueL );
this.list = new JList();
this.list.setSize( 500, 400 );
this.list.setLocation( 200, 130 );
this.pane.add( this.list ); //naa dapit dri ni. ako unta ilisan ni na line
//ug pane.add( new JScollPane( this.list));, //di man mu display
this.list.setFont( new Font( "Courier", Font.BOLD, 14 ) );
addMouseMotionListener( new MouseHandler() );
this.QuickSort.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent e ){
QuickSort test = new QuickSort( array );
list.setListData( test.list );
}
}
);
this.SelectionSort.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent e ){
SelectionSort test = new SelectionSort( array );
list.setListData( test.list );
}
}
);
this.BubbleSort.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent e ){
BubbleSort test = new BubbleSort( array );
list.setListData( test.list );
}
}
);
this.InsertionSort.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent e ){
InsertionSort test = new InsertionSort( array );
list.setListData( test.list );
}
}
);
this.BinarySearch.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent e ){
BinarySearch test = new BinarySearch( array, Integer.parseInt( searchValue.getText() ) );
list.setListData( test.list );
}
}
);
}
public void Tokenize( String val ){
int index = 0;
StringTokenizer myToken = new StringTokenizer( val, " ," );
this.array = new int [ myToken.countTokens() ];
while( myToken.hasMoreTokens() ){
try{
this.array [ index ] = Integer.parseInt (myToken.nextToken() );
index++;
}catch( NumberFormatException e ){}
}
}
private class MouseHandler implements MouseMotionListener{
public void mouseDragged( MouseEvent e ) {}
public void mouseMoved( MouseEvent e ) {
Tokenize( inputTF.getText() );
}
}
}
here are the subclasses:
Code:
import java.util.*;
public class QuickSort extends SortingGUI{
public Vector list = new Vector();
private int count;
public QuickSort( int [] array ){
StringBuilder str = new StringBuilder();
str.append("Unsorted Array: ");
for( int a : array )str.append(a+" ");
list.add(str);
quick_srt( array, 0, array.length -1 );
}
public void quick_srt(int a [] ,int left , int right ){
if( left >= right ) return;
int pivot = a [ ( left + right ) / 2 ] ;
int i = left; int j = right;
while( i <= j ){
while( a [ i ] < pivot && i < j )
i++;
while( a [ j ] > pivot && i < j )
j--;
if( i <= j ){
int temp = a [ i ];
a [ i ] = a [ j ];
a [ j ] = temp;
i++; j--;
}
}
Output( a );
quick_srt( a, left, j );
quick_srt( a, i, right );
}
public void Output( int array [] ){
this.count++;
StringBuilder str = new StringBuilder();
str.append( count+".) ");
for( int a : array )str.append(a+" ");
list.add(str);
}
}
Code:
import java.util.*;
public class BubbleSort extends SortingGUI{
private boolean swapping;
private int count;
public Vector list = new Vector();
public BubbleSort( int array [] ){
StringBuilder str = new StringBuilder();
str.append("Unsorted Array: ");
for( int a : array )str.append(a+" ");
list.add(str);
Process( array );
}
public void Process(int a []){
while( 1 > 0){
for( int x = 0; x < a.length; x++ ){
try{
if( a [ x ] > a [ x + 1] ){
this.swapping = true;
int temp = a [ x ];
a [ x ] = a [ x + 1 ];
a [ x + 1 ] = temp;
Output( a );
}
}catch( Exception e ){}
}
if( this.swapping == false )break;
else this.swapping = false;
}
}
public void Output( int array [] ){
this.count++;
StringBuilder str = new StringBuilder();
str.append( count +".) ");
for( int a : array )str.append( a+" ");
this.list.add( str );
}
}
Code:
import java.util.*;
public class SelectionSort extends SortingGUI{
private int count;
public Vector list = new Vector();
public SelectionSort( int [] array ){
StringBuilder str = new StringBuilder();
str.append("Unsorted Array: ");
for( int a : array )str.append(a+" ");
list.add(str);
Process( array );
}
public void Process( int [] a ){
for( int x = 0; x < a.length ; x++ ){
for( int y = x +1; y < a.length; y++ ){
if( a [ y ] < a [ x ] ){
int temp = a [ x ];
a [ x ] = a [ y ];
a [ y ] = temp;
}
Output( a );
}
Output( a );
}
}
public void Output( int array [] ){
this.count++;
StringBuilder str = new StringBuilder();
str.append( count+".) ");
for( int a : array )str.append( a+" " );
list.add(str);
}
}
Code:
import java.util.*;
public class InsertionSort extends SortingGUI{
private int count;
public Vector list = new Vector();
public InsertionSort( int array [] ){
StringBuilder str = new StringBuilder();
str.append("Unsorted Array: ");
for( int a : array )str.append(a+" ");
list.add(str);
Process( array, array.length );
}
public void Process( int array [], int length ){
LinkedList list = new LinkedList();
boolean isGreater = false;
list.add( array[0]);
int index = 0, count=1;
for( int i = 1; i < length; i++ ){
for( int x = 0; x < list.size(); x++){
if( array[i] > (int) list.get(x) )
isGreater = true;
else{
isGreater = false;
index = x;
break;
}
}
if( isGreater == false )
list.add( index, array [ i ]);
else
list.add( array [ i ]);
this.list.add( count+".) "+list );
count++;
}
}
}
Code:
import java.util.*;
public class BinarySearch extends SortingGUI{
public Vector list = new Vector();
public BinarySearch( int a [], int val ){
Process( a, val );
}
public void Process( int a[] ,int val ){
StringBuilder str = new StringBuilder();
boolean found = false;
int first = 0, mid = 0, last = a.length-1;
while( first <= last ){
mid = (int) ( (first +last) / 2 );
System.out.println("aw");
if( a [ mid ] == val ){
list.add("\n"+val+" was found in index "+mid);
found = true;
break;
}else if( a [ mid ] > val )
last = mid - 1;
else
first = mid + 1;
list.add("\nPassed in index "+mid);
}
if( found == false )
list.add( "\n"+val+" was not found" );
}
}
here is the main program
Code:
import javax.swing.JFrame;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* 5, 4, 2, 6, 8, 3, 7, 9, 10, 59 ,34, 16, 0, -1, -9, -20
* @author jairoh
*/
public class SortingGUITest {
public static void main( String args [] ){
SortingGUI test = new SortingGUI();
test.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
test.setVisible( true );
test.setSize( 800, 600 );
}
}