2

基本的にソートアルゴリズムをグラフィカルにアニメーション化する「グラフィカルソート」と呼ばれる課題に取り組んでいます。

並べ替えプロセスのアニメーションについて助けが必要です。

Thread を使用してみましたが、スレッド化プロセスが完了するまでプログラムがハングし、最終結果が表示されます。

以下は、私のプログラムがどのように見えるかの写真です。

ここに画像の説明を入力

ここに画像の説明を入力

以下は、ペイントに使用するパネルのクラスです

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 ();
}
}

プロセスをアニメーション化するには何を使用すればよいですか。また、並べ替えの過程でどの長方形のストリップが比較されているかを示したいと思います。

ありがとうございました

4

1 に答える 1

2

同様のプログラムを作成したときSortListener、2つのメソッドを使用してインターフェイスを作成しました:onCompare()onSwap()。を使用して、onCompare()比較対象の2つの要素を異なる色でonSwap()ハイライトし、GUIにistelfを再描画するように通知しました。

また、特定の並べ替えアルゴリズム用に「SortingAlgorithm」抽象クラスといくつかのサブクラスを作成しました。addSortListener()他のクラスがリスナーを登録できるようにするために定義されたスーパークラス。次に、並べ替えアルゴリズム中にonCompare()、比較の直後とonSwap()2つの要素が交換された直後に呼び出しました。

最後に、私がペイントを行ったJPanelは、SortListenerインターフェイスを実装し、アニメーションに応答しonCompare()て再ペイントしました。onSwap()

于 2013-03-15T00:30:30.297 に答える