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

📄 intarraystickanimationview.java

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

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

import graphicAnimation.intAnimationView.*;

/**
 * 利用各种整数视图展示整数数组
 * @author 周晓聪
 * @since 2007/7/1
 *
 */
public class IntArrayStickAnimationView extends IntArrayAnimationView {
	protected int elementWidth = 20;					// 每个元素的宽度
	protected int spacingWidth = 10;					// 各元素之间的间隔
	protected boolean showValue = false; 			// 是否显示数组元素的整数数值
	protected Color valueColor = Color.white;			// 显示整数数值的颜色
	
	protected int scopeStartIndex = -1;				// 正关注的数组范围的起始下标
	protected int scopeEndIndex = -1;					// 正关注的数组范围的终止下标
	protected int baseLineIndex = -1;					// 作为基线元素的下标
	protected boolean showScope = false;				// 是否显示范围

	public IntArrayStickAnimationView() { }
	public IntArrayStickAnimationView(Point pos, int elementWidth, int spacingWidth, Color color) {
		super(pos, color);
		this.elementWidth = elementWidth;
		this.spacingWidth = spacingWidth;
	}
	
	/**
	 * 以最简单的整数视图创建数组元素的视图
	 * @param data 待创建视图的数组
	 */
	public void createElementAnimationView(int[] data) { 
		elements = new IntAnimationView[data.length];

		int x = position.x;
		int y = position.y;
		for (int i = 0; i < data.length; i++) {
			elements[i] = new IntAnimationView(data[i], new Point(x, y), color);
			x = x + elementWidth + spacingWidth;
		}
		valueColor = color;
	}
	
	/**
	 * 以正方形盒子的整数视图创建数组元素的视图
	 * @param data 待创建视图的数组
	 * @param valueColor 显示数组元素整数数值的颜色
	 */
	public void createElementBoxAnimationView(int[] data, Color valueColor) { 
		elements = new IntBoxAnimationView[data.length];

		int x = position.x;
		int y = position.y;
		for (int i = 0; i < data.length; i++) {
			elements[i] = new IntBoxAnimationView(data[i], new Point(x, y), color, valueColor, elementWidth);
			x = x + elementWidth + spacingWidth;
		}
		this.valueColor = valueColor;
	}
	
	/**
	 * 以圆形的整数视图创建数组元素的视图
	 * @param data 待创建视图的数组
	 * @param valueColor 显示数组元素整数数值的颜色
	 */
	public void createElementCircleAnimationView(int[] data, Color valueColor) { 
		elements = new IntCircleAnimationView[data.length];

		int x = position.x+elementWidth/2;
		int y = position.y;
		for (int i = 0; i < data.length; i++) {
			elements[i] = new IntCircleAnimationView(data[i], new Point(x, y), color, valueColor, elementWidth/2);
			x = x + elementWidth + spacingWidth;
		}
		this.valueColor = valueColor;
	}
	
	/**
	 * 以矩形条的整数视图创建数组元素的视图
	 * @param data 待创建视图的数组
	 * @param valueColor 显示数组元素整数数值的颜色
	 * @param unitHeight 矩形条的单位高度
	 * @param showValue 是否显示数组元素的整数数值,因为这种视图以矩形条的高度代表整数数值,所以提供
	 *    一个额外的选择,让用户确定是否在矩形条的下面同时显示元素的整数数值
	 */
	public void createElementRectAnimationView(int[] data, Color valueColor, int unitHeight, boolean showValue) { 
		elements = new IntRectAnimationView[data.length];

		int x = position.x;
		int y = position.y;
		for (int i = 0; i < data.length; i++) {
			elements[i] = new IntRectAnimationView(data[i], new Point(x, y), color, valueColor, elementWidth, unitHeight);
			x = x + elementWidth + spacingWidth;
		}
		this.valueColor = valueColor;
		this.showValue = showValue; 
	}
	

	/**
	 *  设置每个元素的矩形框的宽度
	 * @param width 宽度值
	 */
	public void setElementWidth(int width) { elementWidth = width; }
	
	/**
	 *  设置每个元素之间的间隔宽度
	 * @param width 宽度值
	 */
	public void setSpacingWidth(int width) { spacingWidth = width; }
	
	/** 
	 * 返回每个数组元素的矩形框宽度
	 * @return 宽度值
	 */
	public int getElementWidth() { return elementWidth; }
	
	/**
	 * 返回某个数组元素矩形框之间的间隔宽度
	 * @return 间隔宽度
	 */
	public int getSpacingWidth() { return spacingWidth; }
	
	/**
	 * 设置当前正在关注(通常是排序)的数组下标范围
	 * @param startIndex 起始下标
	 * @param endIndex 终止下标
	 */
	public void setFocusScope(int startIndex, int endIndex) {
		if (0 <= startIndex && startIndex < elements.length &&
				0 <= endIndex && endIndex < elements.length && startIndex < endIndex) {
			scopeStartIndex = startIndex;
			scopeEndIndex = endIndex;
		}
	}
	
	/**
	 * 清除正在关注的数组下标范围
	 */
	public void clearFocusScope() {
		scopeStartIndex = scopeEndIndex = baseLineIndex = -1;
	}

	/**
	 * 设置基准元素的下标,基准元素用于在矩形条的视图中根据该元素的矩形条高度画出一条线,展示其他元素的
	 * 矩形条高度与该基准元素的比较情况
	 */
	public void setBaseLineElement(int index) {
		if (scopeStartIndex <= index && index <= scopeEndIndex) baseLineIndex = index;
	}

	/**
	 * 清除基准元素的下标
	 *
	 */
	public void clearBaseLine() {
		baseLineIndex = -1;
	}
	
	/**
	 * 设置是否显示正在关注的范围
	 */
	public void setShowScope(boolean showScope) {
		this.showScope = showScope;
	}
	
	/**
	 * 设置是否显示数组元素的整数数值
	 */
	public void setShowValue(boolean showValue) {
		this.showValue = showValue;
	}
	 
	/**
	 * 在屏幕上画出该整数数组
	 * @param gc 所使用的图形上下文
	 */
	public void paint(Graphics gc) {
		if (!visible) return;
		if (elements == null) return;
		if (elements.length <= 0) return;
		
		if (showValue) {
			// 在所有的矩形框下面画一条直线作为基线
			gc.setColor(color);
			gc.drawLine(position.x, position.y, position.x+elements.length*(elementWidth+spacingWidth)-spacingWidth, position.y);
		}

		int index = 0;
		int x = position.x;
		// 调用元素视图的 paint() 方法画出代表每个元素的矩形框,并在基线下面画出该相应的整数值
		while (index < elements.length) {
			elements[index].paint(gc);
			if (showValue) {
				int value = elements[index].getValue();
				gc.setColor(valueColor);
				gc.drawString(value + "", x, position.y + 12);
				x = x + elementWidth + spacingWidth; 
			}
			index = index + 1;
		}
		
		// 显示正在关注的数组元素范围,在起始下标处和终止下标各画一条竖直直线
		if (showScope && scopeStartIndex != -1) {
			int startX = position.x + scopeStartIndex*(elementWidth+spacingWidth);
			int endX = position.x + scopeEndIndex*(elementWidth+spacingWidth) + elementWidth;
			int startY = position.y - elementWidth - 50;
			int endY = position.y + 30;
			
			// 如果数组元素是 IntRectAnimatorView 或其子类时,要计算范围内矩形框的最大高度
			if (elements[scopeStartIndex] instanceof IntRectAnimationView) {
				startY = ((IntRectAnimationView)elements[scopeStartIndex]).getHeight();
				for (index = scopeStartIndex+1; index <= scopeEndIndex; index++) {
					// 计算在这范围内中最高
					int height = ((IntRectAnimationView)elements[index]).getHeight(); 
					if (startY < height) startY = height;
				}
				startY = position.y - startY - 50;
			}
			
			gc.setColor(color);
			gc.drawLine(startX, startY, startX, endY);
			gc.drawLine(endX, startY, endX, endY);
			
			// 以基准元素 baseLineIndex 处的矩形条高度为基准,水平画一条直线,以表明在关注范围内
			// 的矩形条高度与该基准高度之间的比较
			if (baseLineIndex != -1) {
				if (elements[baseLineIndex] instanceof IntRectAnimationView) {
					startY = position.y - ((IntRectAnimationView)elements[baseLineIndex]).getHeight();
					gc.drawLine(startX, startY, endX, startY);
				}
			}
		}
	}
}

⌨️ 快捷键说明

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