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

📄 sortingarrayanimator.java

📁 用JAVA实现排序等简单算法的演示
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package sortingAnimation;

import graphicAnimation.*;
import graphicAnimation.intContainerAnimationView.*;

import java.awt.*;

/**
 * 排序算法的演示器,它负责实现处理排序算法上下文所需要的一些演示场景的绘制
 * @author 周晓聪
 * @since 2007/6/24
 * @version 1.0
 *
 */
public class SortingArrayAnimator extends Animator { 
	protected int selectedData = 0;						// 排序时所选中的某个特殊元素值
	protected Point selectedPos = null; 
	protected int selectedIndex = -1;						// 排序时所选中的某个特殊元素的下标
	
	protected IntArrayAnimationView arrayView = null;		// 用于展示正在排序数组的模型
	protected Color dataColor = null;						// 展示排序数组元素的颜色
	protected Color compareColor = null;					// 展示正在比较的元素的颜色
	protected Color swapColor = null;						// 展示正在交换的元素的颜色
	protected Color selectColor = null;					// 展示正被选中的元素的颜色
	
	protected int stepDelay = 100;							// 每一步(交换、比较、移动、选择等算一步)时的延迟
	protected int unitMoving = 1;							// 展示动画时的单位移动像素数
	
	public SortingArrayAnimator() { 
		super();
	}
	public SortingArrayAnimator(AnimationArea area) {
		super(area);
	}
	public SortingArrayAnimator(AnimationDriver animationManager) {
		super(animationManager);
	}
	public SortingArrayAnimator(AnimationArea area, AnimationDriver animationManager) {
		super(area, animationManager);
	}
	
	/** 
	 * 设置展示数组时所使用的各种颜色
	 * @param dColor 一般数据所使用的颜色
	 * @param cColor 正在比较的数据所使用的颜色
	 * @param sColor 正在交换的数据所使用的颜色
	 * @param lColor 正被选中的数据所使用的颜色
	 */
	public void setSortingColor(Color dColor, Color cColor, Color sColor, Color lColor) {
		dataColor = dColor;
		compareColor = cColor;
		swapColor = sColor;
		selectColor = lColor;
	}

	/**
	 * 设置步延迟
	 */
	public void setStepDelay(int delay) { stepDelay = delay; }

	/**
	 * 设置在展示动画时单位移动的像素数
	 * @param moving 单位移动的像素数
	 */
	public void setUnitMoving(int moving) { unitMoving = moving; }

	/**
	 * 返回展示数组所使用的模型
	 */
	public IntArrayAnimationView getArrayView() { return arrayView; }

	/**
	 * 返回展示数组时所使用的一般颜色值
	 */
	public Color getDataColor() { return dataColor; }
	/**
	 * 返回展示正在比较的元素所使用的颜色
	 */
	public Color getCompareColor() { return compareColor; }
	/**
	 * 返回展示正在交换的元素所使用的颜色
	 */
	public Color getSwapColor() { return swapColor; }
	/**
	 * 返回展示正被选中的元素所使用的颜色
	 */
	public Color getSelectColor() { return selectColor; }
	
	
	/**
	 * 初始化待排序数组后,建立该数组的视图,使用简单的整数视图来建立数组元素的视图
	 * @param data 待排序数组
	 * @param leftdown 数组视图的起始位置(左下角位置)
	 * @param elementWidth 元素视图的宽度
	 * @param spacingWidth 元素之间的间隔
	 */
	public void createAnimationScene(int[] data, Point leftdown, int elementWidth, int spacingWidth) {
		arrayView = new IntArrayStickAnimationView(leftdown, elementWidth, spacingWidth, dataColor);
		((IntArrayStickAnimationView)arrayView).createElementAnimationView(data);
		update();
	} 
	
	/**
	 * 初始化待排序数组后,建立该数组的视图,使用盒子整数视图来建立数组元素的视图
	 * @param data 待排序数组
	 * @param leftdown 数组视图的起始位置(左下角位置)
	 * @param elementWidth 元素视图的宽度
	 * @param spacingWidth 元素之间的间隔
	 * @param valueColor 在盒子中间显示整数数值时所使用的颜色
	 */
	public void createBoxAnimationScene(int[] data, Point leftdown, int elementWidth, int spacingWidth, Color valueColor) {
		arrayView = new IntArrayStickAnimationView(leftdown, elementWidth, spacingWidth, dataColor);
		((IntArrayStickAnimationView)arrayView).createElementBoxAnimationView(data, valueColor);
		update();
	}
	
	/**
	 * 初始化待排序数组后,建立该数组的视图,使用圆形整数视图来建立数组元素的视图
	 * @param data 待排序数组
	 * @param leftdown 数组视图的起始位置(左下角位置)
	 * @param elementWidth 元素视图的宽度
	 * @param spacingWidth 元素之间的间隔
	 * @param valueColor 在圆形中间显示整数数值时所使用的颜色
	 */
	public void createCircleAnimationScene(int[] data, Point leftdown, int elementWidth, int spacingWidth, Color valueColor) {
		arrayView = new IntArrayStickAnimationView(leftdown, elementWidth, spacingWidth, dataColor);
		((IntArrayStickAnimationView)arrayView).createElementCircleAnimationView(data, valueColor);
		update();
	}
	
	/**
	 * 初始化待排序数组后,建立该数组的视图,使用矩形条整数视图来建立数组元素的视图
	 * @param data 待排序数组
	 * @param leftdown 数组视图的起始位置(左下角位置)
	 * @param elementWidth 元素视图的宽度
	 * @param spacingWidth 元素之间的间隔
	 * @param valueColor 在矩形条下面显示整数数值时所使用的颜色
	 * @param unitHeight 矩形条的单位高度,矩形条的高度等于整数数值乘以单位高度
	 * @param showValue 是否在矩形挑下面显示整数数值
	 */
	public void createRectAnimationScene(int[] data, Point leftdown, int elementWidth, int spacingWidth, Color valueColor, int unitHeight, boolean showValue) {
		arrayView = new IntArrayStickAnimationView(leftdown, elementWidth, spacingWidth, dataColor);
		((IntArrayStickAnimationView)arrayView).createElementRectAnimationView(data, valueColor, unitHeight, showValue);
		update();
	}
	
	/**
	 * 初始化待排序数组后,建立该数组的视图,使用Dot视图来建立数组元素的视图
	 * @param data 待排序数组
	 * @param leftdown 数组视图的起始位置(左下角位置)
	 * @param totalWidth 视图可占用的总宽度
	 * @param totalHeight 视图可占用的总高度
	 */
	public void createDotAnimationScene(int[] data, Point leftdown, int totalWidth, int totalHeight) {
		arrayView = new IntArrayDotAnimationView(leftdown, dataColor, totalWidth, totalHeight);
		((IntArrayDotAnimationView)arrayView).createElementDotAnimationView(data, 500);
		update();  
	} 
	

	/**
	 * 当某一次排序终止之后,展示排序后的最终数组
	 *
	 */
	public void showFinishedScene() {
		arrayView.setArrayColor(dataColor);
		if (arrayView instanceof IntArrayStickAnimationView) {
			((IntArrayStickAnimationView)arrayView).clearFocusScope();
		}
		update();
	}
	
	/**
	 * 改变正在比较的两个数组元素的颜色值,然后驱动视图在屏幕上作相应的修改,从而展示两个正在比
	 * 较的数组元素
	 */
	public void showCompareScene(int firstIndex, int secondIndex) {
		arrayView.setElementColor(firstIndex, compareColor);
		arrayView.setElementColor(secondIndex, compareColor);
		update();
		sleep(stepDelay);
		arrayView.setElementColor(firstIndex, dataColor);
		arrayView.setElementColor(secondIndex, dataColor);
		update();
	}

	/**
	 * 创建展示 firstIndex 和 secondIndex 处的视图进行交换的动画,该动画创建之后交给演示绘制器运行,
	 * 以便实现多个演示器的动画同步运行。在运行该动画之后,还必须调用下面的 swapElementView()以实际
	 * 交换这两个试图,从而保证 firstIndex 处的视图在屏幕上总是第 firstIndex 个矩形条!
	 */
	public void createSwapAnimation(int firstIndex, int secondIndex) {
		// 设置为正在交换的颜色值
		arrayView.setElementColor(firstIndex, swapColor);
		arrayView.setElementColor(secondIndex, swapColor);

		// 创建firstIndex和secondIndex 两个视图相对移动的动画路径,下面的50表示垂直移动的像素数 
		AnimationPath firstPath = createMoveElementPath(firstIndex, secondIndex, 50);
		AnimationPath secondPath = createMoveElementPath(secondIndex, firstIndex, 50);

		Animation firstAnimation = new Animation(this, arrayView.getElementAnimationView(firstIndex));
		Animation secondAnimation = new Animation(this, arrayView.getElementAnimationView(secondIndex));
		
		firstAnimation.setPath(firstPath);
		secondAnimation.setPath(secondPath);
		
		animationManager.add(firstAnimation);
		animationManager.add(secondAnimation);
	}
	
	/**
	 * 运行交换的动画之后实际交换 firstIndex 和 secondIndex 处的视图,因为动画只是将 firstIndex 处的视图移动到 secondIndex 处,如果不

⌨️ 快捷键说明

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