【问题标题】:Animating Graphical Sort动画图形排序
【发布时间】:2013-03-03 13:25:18
【问题描述】:

我正在做一个名为“图形排序”的任务,它基本上是用图形动画排序算法。

我只需要帮助为排序过程设置动画。

我尝试使用 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 ();
}
}

我应该使用什么来为流程设置动画。我还想显示在排序过程中比较了哪些矩形条。

谢谢

【问题讨论】:

  • 您应该使用SwingWorker。每次通过排序时,它都需要请求重新绘制 UI,然后需要暂停一小段时间,然后再开始下一个循环
  • 我明白你使用 SwingWorker 的意思,但是有点问题。我们作为学生被告知要使用“准备编程”,就我而言,它不接受符号 ,它在类 SwingWorker 中。这意味着程序将抛出错误并且不会编译。您有什么不同的方法来减慢重绘方法的速度吗?

标签: java algorithm sorting bubble-sort


【解决方案1】:

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

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

最后,我画的JPanel实现了SortListener接口,并通过重新绘制动画来响应onCompare()onSwap()

【讨论】:

  • 我明白了,但我是 Java 新手,所以介意用几个 sn-ps 详细说明一下吗?谢谢
猜你喜欢
  • 2015-12-07
  • 2019-07-22
  • 1970-01-01
  • 2020-06-09
  • 2014-03-25
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多