所以我正在编写一个使用折线图类型的东西来动画插入排序算法的程序。它现在的编写方式是,当单击填充按钮时,它将生成一个随机整数数组并根据代表线绘制一个 DrawingPanel,当按下暂停按钮时,它会冻结 5 秒,然后显示排序的图形。我想显示每次迭代,一次显示一条移动。有什么建议么。我真的不确定如何在 Java 中使用多线程,我很新。我将不胜感激任何建议。
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import java.util.Random;
import java.util.Arrays;
public class AnimationApplication extends JFrame {
private static final long serialVersionUID = 1L;
AnimationPanel panel1 = new AnimationPanel();
AnimationPanel panel2 = new AnimationPanel();
public static void main(String[] args) {
AnimationApplication prog = new
AnimationApplication("Animation Application");
prog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
prog.setSize(450, 300);
prog.setVisible(true);
}
AnimationApplication(String title) {
super(title);
setLayout(new BorderLayout());
add(panel1, BorderLayout.WEST);
add(panel2, BorderLayout.EAST);
}
}
class AnimationPanel extends JPanel implements ActionListener, Runnable {
private static final long serialVersionUID = 1L;
private int currentSize;
private JButton populate = new JButton("Populate Array");
private JButton pauseB = new JButton("Pause");
private JButton stopB = new JButton("Stop");
private DrawingPanel drawingCanvas = new DrawingPanel();
private volatile Thread animator = null;
private volatile boolean animationSuspended = false;
ArrayList<Integer> pointList = new ArrayList<Integer>();
private Integer [] rndInts;
AnimationPanel() {
setLayout(new BorderLayout());
JPanel buttonP = new JPanel(new GridLayout(1, 3, 5, 5));
buttonP.add(populate);
buttonP.add(pauseB);
buttonP.add(stopB);
populate.addActionListener(this);
pauseB.addActionListener(this);
stopB.addActionListener(this);
add(drawingCanvas, BorderLayout.CENTER);
add(buttonP, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == populate) {
rndInts = new Integer[ getSize().width-1];
for(int i = 0; i < rndInts.length; i++)
{
Random rand = new Random();
rndInts[i]=rand.nextInt((getSize().height)-1);
// System.out.println(rndInts[i]);
}
currentSize = rndInts.length;
pointList = new ArrayList<Integer>(Arrays.asList(rndInts));
//System.out.println("Start button pressed");
// Check if no animation thread exists
if (animator == null) {
// If not, start the animation
start();
} else {
// If animation is paused, resume it
if (animationSuspended) {
resume();
}
}
} else if (e.getSource() == pauseB) {
insertionSort(rndInts , currentSize);
// Check if animation thread exists
if (animator != null) {
// If so, suspend the animation thread
animationSuspended = true;
}
} else if (e.getSource() == stopB) {
stop();
clear();
}
}
public void run() {
Thread thisThread = Thread.currentThread();
drawingCanvas.repaint();
while (animator == thisThread) {
//System.out.println("Animation thread running");
drawingCanvas.repaint();
try {
// Thread.sleep(1);
drawingCanvas.repaint();
if (animationSuspended) {
synchronized (this) {
while (animationSuspended && animator == thisThread) {
drawingCanvas.repaint();
wait();
drawingCanvas.repaint();
}
}
}
} catch (InterruptedException e) {
break;
}
// Repaint the panel
drawingCanvas.repaint();
}
}
public void start() {
drawingCanvas.repaint();
// Create a new animation thread and start it
animator = new Thread(this);
animationSuspended = false;
animator.start();
animationSuspended = true;
}
public synchronized void stop() {
drawingCanvas.repaint();
animator = null;
notify();
}
public synchronized void resume() {
animationSuspended = false;
notify();
}
public void clear() {
pointList.clear();
repaint();
}
void insertionSort(Integer [] arr, int length)
{
int i, j, tmp;
for (i = 1; i < length; i++)
{
j = i;
while (j > 0 && arr[j - 1] > arr[j])
{
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
j--;
stop();
drawingCanvas.repaint();
start();
//resume();
}
}
}
class DrawingPanel extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g) {
// Call superclass version of method
super.paintComponent(g);
this.setBackground(Color.WHITE);
//clear the background
g.clearRect(0, 0, getSize().width-1, getSize().height-1);
g.setColor(Color.RED);
// Draw points
for (int i = 0; i < currentSize ; i++) //pointList.size(); i++)
{
g.drawLine(i, getSize().height, i, rndInts[i]);
repaint();
//resume();
}
}
}
}