⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 heapsortinganimationpainter.java

📁 用JAVA实现排序等简单算法的演示
💻 JAVA
字号:
package sortingAnimation;

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

import animationController.AnimationController;


public class HeapSortingAnimationPainter extends SortingAnimationPainter {
	
	private HeapSortingAnimator heapAnimator = null;				// 当选择堆排序并且适合用树表示的数组展示器时主区域使用的演示器
	
	public HeapSortingAnimationPainter(JComponent parent, JComponent[] toBlock, AnimationController controller) {
		super(parent, toBlock, controller);
	}

	protected void createAnimator() {
		// 创建演示堆排序时所使用的演示器
		heapAnimator = new HeapSortingAnimator(animationDriver);
		heapAnimator.setStepDelay(localController.stepDelay);
		heapAnimator.setUnitMoving(localController.unitMoving);
		heapAnimator.setSortingColor(localController.dataColor, localController.compareColor, localController.swapColor, localController.selectColor);
		mainAreaAnimator = heapAnimator;
		
		// 创建小区域所使用的演示器,通常是使用盒子视图的演示器
		boxAnimator = new SortingArrayAnimator(animationDriver);
		boxAnimator.setStepDelay(localController.stepDelay);
		boxAnimator.setUnitMoving(localController.unitMoving);
		boxAnimator.setSortingColor(localController.dataColor, localController.compareColor, localController.swapColor, localController.selectColor);

		// 创建提示排序时间、比较次数、移动次数、交换次数、选择次数等信息的演示器
		infoAnimator = new SortingInfoAnimator();
		infoAnimator.setSortingColor(localController.dataColor, localController.compareColor, localController.swapColor, localController.selectColor);
		infoAnimator.setStartTime();
	}
	
	protected void connectAnimatorAndArea() {
		// 信息演示区域的演示器是infoAnimator,其他演示区域的演示器待定,因为当排序元素很多时不使用小区域的演
		// 示器,因此这里也暂时没有设置小区域的演示器为 boxAnimator
		infoArea.setAnimator(infoAnimator);

		// 设置各演示器所分别使用的演示区域,不过同一时刻某个演示区域只有一个演示器使用,在选定要演示的排序算
		// 法的同时,我们也会确定到底使用怎样的演示器
		infoAnimator.setAnimationArea(infoArea);
		heapAnimator.setAnimationArea(mainArea);
		boxAnimator.setAnimationArea(smallArea);
	}
	
	/**
	 * 使用数组 data 创建演示用的最初的视图
	 */
	public void createAnimationView(int[] data) {
		// 注意在展示中可能不用 smallArea 中的展示,因此 smallAreaAnimator 可能是 null,但不允许 mainAreaAnimator 为 null,它应该
		// 等于 rectAnimator, heapAnimator 或 mergeAnimator 之一
		if (localController.showValue) {
			smallAreaAnimator = boxAnimator;
			smallArea.setAnimator(smallAreaAnimator);
			boxAnimator.createBoxAnimationScene(data, new Point(localController.startX, localController.smallStartY), localController.elementWidth, 0, Color.white);
		} else {
			smallAreaAnimator = null;
			smallArea.setAnimator(null);
		}
		
		// 这时总有 mainAreaAnimator == heapAnimator
		mainArea.setAnimator(heapAnimator);
		heapAnimator.createHeapAnimationScene(data, new Point(localController.startX, localController.mainMiddleY), localController.elementWidth, 0, Color.white);
		heapAnimator.createHeapTreeAnimatorScene(data, new Point(localController.startX, localController.mainStartY), Math.max(24, localController.elementWidth), Color.white, localController.windowWidth-2*localController.startX, localController.mainStartY+20);

		Animator.sleep(localController.stepDelay);
	}


	/**
	 * 使用动画展示正在交换的两个数组元素,注意只有在需要移动数组元素时,用树演示的堆排序和其他排序才有区别,
	 * 因为这时树上节点的移动没有与小区域中数组元素的移动同步(不同步可更容易看清楚节点的移动),因此没有使用
	 * 动画管理器 animationManager,仅仅委托给 heapAnimator 展示就可以了,而其他排序小区域与主区域的数组元
	 * 素移动是同步的,因此需要 animationManager 在下面的方法一起 run ,而不是委托给 mainAreaAnimator 进行
	 * 动画展示
	 */ 
	public void showSwapScene(int firstIndex, int secondIndex) {
		infoAnimator.incSwapCounter();

		heapAnimator.showSwapScene(firstIndex, secondIndex);
		if (smallAreaAnimator != null) {
			animationDriver.clear();
			smallAreaAnimator.createSwapAnimation(firstIndex, secondIndex);
			animationDriver.run();
			smallAreaAnimator.swapElementView(firstIndex, secondIndex);
			Animator.sleep(localController.stepDelay);
		}
	}

	/**
	 * 使用动画展示数组 fromIndex 处的元素向 toIndex 处的移动。注意,为了使连续移动显得快一些,移动元素没有步延迟
	 */
	public void showMoveElementScene(int fromIndex, int toIndex) {
		infoAnimator.incMoveCounter();

		heapAnimator.showMoveElementScene(fromIndex, toIndex);
		if (smallAreaAnimator != null) {
			animationDriver.clear();
			smallAreaAnimator.createMoveElementAnimation(fromIndex, toIndex);
			animationDriver.run();
		}
	}


	/**
	 * 使用动画展示放置被选中元素到 index 处,也就是将被选中的元素移动到 index 处
	 */
	public void showPlaceScene(int index) {
		infoAnimator.incMoveCounter();

		heapAnimator.showPlaceScene(index);
		if (smallAreaAnimator != null) {
			animationDriver.clear();
			smallAreaAnimator.createPlaceAnimation(index);
			smallAreaAnimator.showNormalColor(index);
			animationDriver.run();
			Animator.sleep(localController.stepDelay);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -