'Why is my GUI graphing a sorted array when running my main program, but graphs an unsorted array when running the program that creates the GUI?
I am currently working on a sorting algorithm visualizer that displays a GUI of a graph that sorts itself. This graph represents an array of random integers. I have not added the sorting functionalities to the GUI. The GUI is created in another class. The main program calls it.
The problem begins when I run the main program. When running the program that creates the GUI, the graph displays the unsorted array that it is based on. When I run my main program, the GUI displays a sorted array. I don't want the sorted array to be displayed. What could be causing this?
Main program:
public class sortVisualizerShell{
public static int[] unsortedArray = arrayGenerator();
public static int[] arrayGenerator() {
int N = 500;
int[] array = new int[N];
Random rand = new Random();
for (int i = 0; i < N; i++) {
int random_num = rand.nextInt(N);
array[i] = random_num;
}
return array;
}
public static void selectionSort(int[] array, int n) {
for (int i = 0; i < (n-1); i++) {
int min = i;
for (int j = (i+1); j < n; j++) {
if (array[min] > array[j]) {
min = j;
}
}
int key = array[min];
while(min > i) {
array[min] = array[min-1];
min = min - 1;
}
array[i] = key;
}
}
public static void bubbleSort(int[] array, int n) {
boolean swapped;
int i,j,temp;
for (i = 0; i < (n-1); i++) {
swapped = false;
for (j = 0; j < ((n-1) - 1); j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
swapped = true;
}
}
if (swapped == false) {
break;
}
}
}
public static void insertionSort(int[] array) {
int n = array.length;
for (int i = 1; i < n; i++) {
int key = array[i];
int j = i -1;
while (j >= 0 && array[j] > key) {
array[j+1] = array[j];
j = j - 1;
}
array[j+1] = key;
}
}
public static void quickSort(int[] array, int left, int right) {
if (left < right) {
int pivot = partition(array, left, right);
quickSort(array, left, pivot - 1);
quickSort(array, pivot + 1, right);
}
}
public static int partition(int[] array, int low, int high) {
int pivot = array[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (array[j] < pivot) {
i++;
swap(array, i ,j);
}
}
swap(array, i + 1, high);
return (i + 1);
}
public static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void mergeSort(int[] array, int len) {
if (len < 2) {
return;
}
int middle = len / 2;
int[] left_arr = new int[middle];
int[] right_arr = new int[len-middle];
int k = 0;
for (int i = 0; i<len; i++) {
if (i < middle) {
left_arr[i] = array[i];
}
else {
right_arr[k] = array[i];
k = k + 1;
}
}
mergeSort(left_arr, middle);
mergeSort(right_arr, len-middle);
merge(left_arr, right_arr, array, middle, (len-middle));
}
public static void merge(int[] left_arr, int[] right_arr, int[] array, int left_side, int right_side){
int i = 0;
int left = 0;
int right = 0;
while (left < left_side && right < right_side) {
if (left_arr[left] < right_arr[right]) {
array[i++] = left_arr[left++];
}
else {
array[i++] = right_arr[right++];
}
}
while (left < left_side) {
array[i++] = left_arr[left++];
}
while(right < right_side) {
array[i++] = right_arr[right++];
}
}
public static void userInputFrame(String requestedSort, int[] array) {
if (requestedSort.equals("merge sort")) {
mergeSort(array, array.length);
}
else if (requestedSort.equals("quick sort")) {
quickSort(array, 0, array.length - 1);
}
else if (requestedSort.equals("insertion sort")) {
insertionSort(array);
}
else if (requestedSort.equals("bubble sort")) {
bubbleSort(array, array.length);
}
else if (requestedSort.equals("selection sort")) {
selectionSort(array, array.length);
}
}
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("Welcome to my sort visualizer");
System.out.println("");
System.out.println("Which sort would you like to see in action? ");
String requestedSort = myScanner.nextLine();
requestedSort = requestedSort.toLowerCase();
Drawer draw = new Drawer();
draw.createGUI();
myScanner.close();
userInputFrame(requestedSort,unsortedArray);
}
}
GUI Program:
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Drawer extends JPanel{
public int[] y = sortVisualizerShell.unsortedArray;
public static void createGUI() {
JFrame frame = new JFrame();
JButton button = new JButton();
JPanel panel = new JPanel();
Drawer draw = new Drawer();
panel.setOpaque(false);
panel.setLayout(new BoxLayout(panel,BoxLayout.PAGE_AXIS));
panel.add(Box.createRigidArea(new Dimension(0,5)));
panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
panel.add(button);
panel.add(draw);
frame.add(panel);
frame.getContentPane().setBackground(Color.BLACK);
frame.setVisible(true);
frame.pack();
frame.setSize(550,600);
}
public void paintComponent(Graphics g) {
for (int i = 0; i < y.length;i++) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawRect(i,0, 5, y[i]);
g2d.setColor(Color.WHITE);
};
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createGUI();
}
});
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
