I programmed a little sorting visualizer a while back and I have been meaning to get some feedback on the overall implementation and on what I could do better to have cleaner code or a "best practice" version.
I did my best to use OOP programming.
I have done some projects in the past so this is not my first one and I wouldn't say that I am a complete beginner but I guess I still have a lot to learn and to improve upon so any feedback is appreciated dearly! :)
Main class:
public class Main{
public static void main(String[] args) {
new Frame();
}
}
Panel class where the main logic of the program takes place:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Panel extends JPanel implements ActionListener{
private int[] array = new int[Constants.ELEMENTS.getValue()];
private SelectionSort selectionSort;
private InsertionSort insertionSort;
private BubbleSort bubbleSort;
private JButton[] buttons;
private JButton selectionSortButton = new JButton("selection sort");
private JButton reset = new JButton("reset");
private JButton insertionSortButton = new JButton("insertion sort");
private JButton bubbleSortButton = new JButton("bubble sort");
public Panel() {
this.buttons = new JButton[] {selectionSortButton, insertionSortButton, bubbleSortButton, reset};
this.selectionSort = new SelectionSort();
this.insertionSort = new InsertionSort();
this.bubbleSort = new BubbleSort();
this.setPreferredSize(new Dimension(Constants.WIN_WIDTH.getValue(), Constants.WIN_HEIGHT.getValue()));
this._initArray();
this.setOpaque(true);//otherwise backgroundcolor won't be visible
for(JButton button : buttons){
button.addActionListener(this);
this.add(button);
}
}
public void _initArray() {
for(int i = 0; i < this.array.length; i++) {
array[i] = (int)(Math.random() * Constants.RANGE.getValue()+1);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.ORANGE);
for(int i = 0; i < this.getArray().length; i++) {
g.drawRect(i, 800 - array[i], 1, array[i]);
g.fillRect(i, 800 - array[i], 1, array[i]);
}
}
public int[] getArray() {
return this.array;
}
public void render(int time) {
repaint();
try{
Thread.sleep(time);
}
catch(Exception e) {
e.printStackTrace();
}
}
public void resetArray() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < getArray().length; i++) {
getArray()[i] = (int)(Math.random() * Constants.RANGE.getValue()+1);
render(1);
}
}
});
t.start();
}
public void initSort(SortingInterface sortint, int start) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for(int i = start; i <= getArray().length; i++) {
sortint.sort(getArray(), i);
render(10);
}
}
});
t.start();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == this.reset) {
this.resetArray();
}
if(e.getSource() == this.selectionSortButton){
initSort(selectionSort, 0);
}
if(e.getSource() == this.insertionSortButton) {
initSort(insertionSort, 1);
}
if(e.getSource() == this.bubbleSortButton) {
initSort(bubbleSort, 0);
}
}
}
Frame class:
import javax.swing.*;
public class Frame extends JFrame{
public Frame() {
this.getContentPane().add(new Panel());
this.setTitle("Sorting visualizer");
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
this.setLocationRelativeTo(null);
}
}
Interface for all the sorting algorithms:
public interface SortingInterface {
public void sort(int[] array, int i);
}
My implementation of Selection sort:
public class SelectionSort implements SortingInterface{
@Override
public void sort(int[] array, int i) {
int min = i;
for(int j = i+1; j < array.length; j++) {
if(array[j] < array[min]) {
min = j;
}
}
if(min != i) {
int temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}
}
My implementation of Insertion sort:
public class InsertionSort implements SortingInterface{
@Override
public void sort(int[] array, int i) {
if(array[i] < array[i-1]) {
for(int j = 0; j < i; j++) {
if(array[i] < array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
}
My implementation of Bubble Sort:
public class BubbleSort implements SortingInterface{
@Override
public void sort(int[] array, int i) {
for(int j = i+1; j < array.length; j++) {
if(array[i] > array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
Enum for all (or most I think) constants, not because it is best practice (not sure about that actually) but because I felt like using an enum:
public enum Constants {
WIN_WIDTH(1200),
WIN_HEIGHT(800),
ELEMENTS(1200),
RANGE(WIN_HEIGHT.getValue()-100);
public final int value;
private Constants(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
}
I have been programming since June. I started with Mooc.fi java part 1 and 2 and then got a little into the Odin Project. But I stopped after the Tic-Tac-Toe project as I realized that web development really isn't it for me. I did the sorting visualizer like a month ago and ever since I kind of did not really have any further project ideas... So if someone has some interesting ideas, I'm all in :)