0

I am currently working on Sorting Visualizer using Swing Components. First, here is the code to one of my chosen sorting algorithms:

package sortingAlgorithms;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;

import javax.swing.Timer;

import framework.sortingPanel.SortingPanel;

public class InsertionSort{
    private Timer sortingTimer;

    private int outerCtr, innerCtr;
    private double[] bars;
    private int iteration;
    private double barWidth;
    private int candidateBar, traversingBar;
    private int delay;
    private boolean proceedToOuter;

    public InsertionSort(SortingPanel sortingPanel) {
        sortingTimer = new Timer(delay, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(proceedToOuter) {
                    innerCtr = outerCtr;
                    candidateBar = outerCtr;
                }

                if(outerCtr < bars.length) {
                    if(innerCtr > 0 && bars[innerCtr - 1] > bars[candidateBar]) {
                        sortingPanel.setNumOfIteration(++iteration);
                        traversingBar = innerCtr - 1;

                        if(bars[candidateBar] < bars[traversingBar]) {
                            double tempBar = bars[candidateBar];
                            bars[candidateBar] = bars[traversingBar];
                            bars[traversingBar] = tempBar;

                            int temp = candidateBar;
                            candidateBar = traversingBar;
                            traversingBar = temp;
                        }

                        sortingPanel.repaint();
                        innerCtr--;
                        proceedToOuter = false;
                    }else {
                        proceedToOuter = true;
                        outerCtr++;
                    }
                }else {
                    sortingTimer.stop();
                    sortingPanel.sortingIsDone();
                    System.out.println("SORTED");
                    //everything else after the algorithm
                }               
            }
        });
    }

    public void initComponents(double[] bars, double barWidth, int delay) {
        iteration = 0;
        this.bars = bars;
        this.barWidth = barWidth;
        this.delay = delay;
        setDelay(delay);
        outerCtr = 1;
        innerCtr = outerCtr;
        candidateBar = outerCtr;
        traversingBar = 0;
        proceedToOuter = true;
    }

    public void draw(Graphics g) {
        g.setColor(Color.WHITE);
        Graphics2D g2d = (Graphics2D) g;
        Rectangle2D r2d;
        for(int i = 0; i < bars.length; i++) {
            r2d = new Rectangle2D.Double(i*barWidth, 0, barWidth, bars[i]);
            g2d.fill(r2d);
        }

        g.setColor(Color.RED);
        r2d = new Rectangle2D.Double(traversingBar*barWidth, 0, barWidth, bars[traversingBar]);
        g2d.fill(r2d);

        g.setColor(Color.GREEN);
        r2d = new Rectangle2D.Double(candidateBar*barWidth, 0, barWidth, bars[candidateBar]);
        g2d.fill(r2d);
    }

    public void setDelay(int delay) {   sortingTimer.setDelay(delay);   }
    public void playSort() {        sortingTimer.start();   }
    public void stopSort() {        sortingTimer.stop();    }   
    public void continueSort() {    sortingTimer.start();   }
}

I used a JPanel to display the double bars[] which are the bars to be sorted. I created an method initComponents() to initialize the variables needed in the algorithm (I did this since I designed the project in such a way that the user could shuffle and re-sort another bars[] using the same algorithm; plus, I don't know why I can't access the values passed during instantiation of InsertionSort class so I've decided to create a separate method to handle the issue).

I draw() method accepts a Graphics from the sortingPanel in which all the bars are being displayed. I have overriden the paintComponent in sortingPanel and pass its Graphics to this draw method.

I just need your opinion whether I'm right in implementing Swing Timer.

ADDED NOTE: This code works perfectly but I want to hear some opinion especially in the technical side of the code structure. I also know that Thread.Sleep() is a no go so I just want to see if this is correct or are there WAY BETTER solution than this.

0 Answers0