2

I am working on an assignment called "Graphical Sort", which is basically animating sort algorithm graphically.

I just need help on animating the sorting process.

I tried using Thread, but the program hangs till the threading process is completed, then it shows the final result.

Below are the picture of how my program looks like:

enter image description here

enter image description here

Below is the class of the panel I use to paint on

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class PaintPanel extends JPanel
{
    // Create an array of 34 element size
    int[] Arr = new int [34];
    // Set default X pointer to 20
    int x = 50;
    // Declare Y pointer to 660
    int y = 660;
    // Set the length of array to n variable
    int n = Arr.length;

    /*
     * main method
     * @param none
     * @return none
     */
    public PaintPanel ()
    {
        randomNums ();
    }


    /*
     * Generates random numbers between 50 and 750 and stores it into the Arr variable
     * @param none
     * @return none
     */
    public void randomNums ()
    {
        // call randomGenerator object
        Random randomGenerator = new Random ();
        // Loop 33 times = Generates 33 random integers
        for (int i = 0 ; i <= 33 ; ++i)
        {
            // Generate random Number
            int randomInt = randomGenerator.nextInt (700);
            // Conditional statement, if any number is less than 50, then discard it and generate new number
            if (randomInt > 50)
            // Assign each random number into Arr Element
            Arr [i] = randomInt;
        else
        {
            // Regenerate Random Number
            randomInt = randomGenerator.nextInt (700);
            // Assign it again
            Arr [i] = randomInt;
            }
        }
    }


/*
 * Bubble Sort Algorithm
 * @param none
 * @return none
 */
public void bubble ()
{ //Pre: a is an array with values. It is of size n
    //Post: the values in a are put in ascending order
    int temp;
    int a[] = Arr;
    for (int i = 0 ; i < n - 1 ; i++)
    {
        for (int j = 0 ; j < n - 1 - i ; j++)
        { // compare the two neighbours
            if (a [j + 1] < a [j])
            { //swap the neighbours if necessary
                temp = a [j];
                a [j] = a [j + 1];
                a [j + 1] = temp;
            }
        }
    }
}


/*
 * Paints 33 rectangle Strips to the screen
 * @param Graphics g
 * @return none
 */
public void paintComponent (Graphics g)
{
    super.paintComponent (g);
    // Call Graphics2D Object
    Graphics2D g2 = (Graphics2D) g.create ();
    // Create Paint Object with gradient Fill
    Paint p = new GradientPaint (
            0, 0, new Color (0x44A2FF),
            getWidth (), 0, new Color (0x0CBEAE),
            true
            );
    // Set the gradient fill to the Graphics2D Object
    g2.setPaint (p);

    // Loop through the Array and display series of Rectangular Strips
    for (int i = 0 ; i < Arr.length ; ++i)
    {
        // Fill out the Rectangle
        g2.fillRect (x, y, Arr [i], 8);
        y = y - 15;
    }
    g2.dispose ();
}
}

What should I use to animate the process. I also want to show which rectangular strips are being compared during the process of sorting.

Thank You

4

1 回答 1

2

当我写一个类似的程序时,我SortListener用两个方法创建了一个接口:onCompare()onSwap(). 我使用突出onCompare()显示与不同颜色进行比较的两个元素,并onSwap()通知 GUI 重新绘制 istelf。

我还为特定的排序算法创建了一个“SortingAlgorithm”抽象类和几个子类。addSortListener()定义为允许其他类注册侦听器的超类。然后在排序算法期间,我onCompare()在比较onSwap()后立即调用,并在交换两个元素后立即调用。

最后,我在其中进行绘画的 JPanel 实现了SortListener界面onCompare()onSwap()通过重新绘制动画来响应和响应。

于 2013-03-15T00:30:30.297 回答