So I'm working on a project where an array is being created with a random number generator containing 10-100 values, its then being painted as a bar graph on a GUI unsorted. The bars then get cycled through and highlighted/unhighlited until the end of the array.
This was the first part of that project.
Second part is I have to first Sort the array, display that array somewhat in the same manner(no highlighting) but also keep the unsorted array. This is where I'm having trouble, Im getting the array to be sorted, but when I go to paint it onto the interface it takes the place of the unsorted.
I think I explained that right but heres the code for the main class, if you guys could take a look at it and see why it would be taking the unsorted arrays place that would be great.
Code
public class ArrayViewer
{
static int[] array;
static int[] sortedArray;
public static void main(String[] args)
{
int size=0;
Scanner in=new Scanner(System.in);
do
{
System.out.print("Enter the size of the array (should be between 10 and 100): ");
size=in.nextInt();
}
while (size<10 || size>100);
JFrame frame = new JFrame("Graph");
JPanel text = new JPanel(new GridLayout(2,3));
JPanel graph = new JPanel(new GridLayout(1,1));
JPanel sortedGraph = new JPanel(new GridLayout(1,1));
GridLayout grid = new GridLayout(3, 1);
frame.setLayout(grid);
array= ArrayUtil.randomIntArray(size,200);
System.out.println(ArrayUtil.printArray(array));
sortedArray = array;
final ArrayComponent arrayGraph = new ArrayComponent(array);
frame.setSize(1025,700);
graph.add(arrayGraph);
JButton sortArray = new JButton("Click to sort and display the Array");
JButton linSearch = new JButton("Linear Search");
JButton binSearch = new JButton("Binary Search");
JTextArea searchText = new JTextArea("Search Item");
JTextArea resultText = new JTextArea("The result will be placed here");
text.add(binSearch);
text.add(searchText);
text.add(linSearch);
text.add(resultText);
graph.setVisible(true);
text.setVisible(true);
frame.add(text);
frame.add(graph);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
class TimerListener implements ActionListener
{
int index = 0;
public void actionPerformed(ActionEvent e)
{
if(0<=index && index<array.length)
{
arrayGraph.highlight(index);
index++;
}
else
{
arrayGraph.noHighlight();
}
}
}
TimerListener listener = new TimerListener();
final Timer timer = new Timer(500,listener);
timer.start();
class sortArrayListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
MergeSorter sorter = new MergeSorter(sortedArray);
sorter.mergeSort();
final ArrayComponent sortArray = new ArrayComponent(sortedArray);
}
}
sortArrayListener sort = new sortArrayListener();
sortArray.addActionListener(sort);
sortedGraph.add(sortArray);
frame.add(sortedGraph);
text.add(sortArray);
}
}
If you guys need to see the other 3 classes I can do that too. Theres ArrayUtil, which does the random numbers, ArrayComponent which uses the PaintComponent to draw the objects, and MergeSorter to merge sort the array.
This post was edited by pyromaniac09 on Apr 29 2014 10:21pm