java 做动画_JAVA怎么做一个排序的动画演示

这个Java程序创建了一个Canvas类,用于展示排序过程的动画。它使用了选择排序算法,并通过Graphics对象在Canvas上绘制节点和箭头,模拟元素的比较和交换。程序提供了不同状态的绘制方法,包括绘制初始数据、排序过程和排序结果。通过`proceed`方法执行排序步骤,并用延时函数控制动画的节奏。
摘要由CSDN通过智能技术生成

import java.awt.Canvas;

import java.awt.Color;

import java.awt.Component;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Image;

public class Demo extends Canvas

{

private final int NO_START = -1;

private final int HAS_START = 0;

private final int HAS_OVER = 1;

private final int BX = 20;

private final int BY = 20;

private int DX = 30;

private final int DY = 15;

private final int WIDTH = 20;

private final int HEIGHT = 30;

private int m_nNum;

private int m_nIIndex;

private int m_nJIndex;

private int m_nTemp;

//private int m_nLast;

private int m_nCur;

private String m_sOldData;

private char[] m_sData = new char[12];

private int m_nStatus = -1;

private boolean m_bSortType = true;

private Graphics m_Graph;

private Graphics m_offG;

private Image m_offImg;

public void paint(Graphics g)//根据状态值m_nStatus来绘图,并且通过m_nIIndex,m_nJIndex和m_nTemp来控制箭头

{

this.m_offG.clearRect(0, 0, getSize().width, getSize().height);

switch (this.m_nStatus)

{

case -1:

break;

case 0:

drawData(false, this.m_offG);

for (int i = 1; i < this.m_nNum + 1; i++)

{

if (this.m_nCur == i)

continue;

Color color;

if ((i != this.m_nIIndex) && (i != this.m_nJIndex))

color = Color.black;

else

color = Color.yellow;

drawNode(this.m_sData[i], i, 10, getBackground(), color, this.m_offG);

}

if (this.m_nIIndex <= this.m_nNum){

drawArrow('i', this.m_nIIndex, 10, Color.red, this.m_offG);

}

if ((this.m_nJIndex > this.m_nIIndex) && (this.m_nJIndex <= this.m_nNum)) {

drawArrow('j', this.m_nJIndex, 10, Color.red, this.m_offG);

}

if ((this.m_nTemp >= 1) && (this.m_nTemp <= this.m_nNum)){

drawArrow1('t', this.m_nTemp, 10, Color.green, this.m_offG);

}

g.drawImage(this.m_offImg, 0, 0, this);

break;

case 1:

drawData(true, this.m_offG);

g.drawImage(this.m_offImg, 0, 0, this);

break;

}

}

public int proceed(int nStep)//这是算法执行的主要逻辑,其中case中的值要与Applet1.java中的showSource的nStep相对应起来

{                             //这里的逻辑主要是简单的选择排序

int nextStep = -1;

//int j = 0;

switch (nStep)

{

case -1:

this.m_nStatus = 0;

nextStep = 0;

break;

case 0:

nextStep = 2;

this.m_nIIndex = 0;

break;

case 2:

this.m_nIIndex += 1;

if (this.m_nIIndex > this.m_nNum)

nextStep = 10;

else

nextStep = 3;

break;

case 3:

this.m_nTemp = this.m_nIIndex;

this.m_nJIndex = this.m_nIIndex;

nextStep = 4;

break;

case 4:

this.m_nJIndex += 1;

if (this.m_nJIndex <= this.m_nNum)

nextStep = 5;

else

nextStep = 7;

break;

case 5:

//j = this.m_nJIndex;

for (int i = 0; i < 3; i++)

{  //显示一下动态效果

drawNode(this.m_sData[this.m_nTemp], this.m_nTemp, 10, Color.red, Color.yellow, getGraphics());

drawNode(this.m_sData[this.m_nJIndex], this.m_nJIndex, 10, Color.red, Color.yellow, getGraphics());

delay(120);

drawNode(this.m_sData[this.m_nTemp], this.m_nTemp, 10, Color.cyan, Color.yellow, getGraphics());

drawNode(this.m_sData[this.m_nJIndex], this.m_nJIndex, 10, getBackground(), Color.yellow, getGraphics());

delay(120);

}

if (compare(this.m_sData[this.m_nJIndex],this.m_sData[this.m_nTemp]))

nextStep = 6;

else

nextStep = 4;

break;

case 6:

this.m_nTemp = this.m_nJIndex;

nextStep = 4;

break;

case 7:

if (this.m_nIIndex != this.m_nTemp)

nextStep = 8;

else

nextStep = 2;

break;

case 8:

//交换节点

swapNode(this.m_nIIndex,this.m_nTemp,Color.lightGray,Color.yellow);

//j = this.m_nJIndex;

char ch = this.m_sData[this.m_nTemp];

this.m_sData[this.m_nTemp] = this.m_sData[this.m_nIIndex];

this.m_sData[this.m_nIIndex] = ch;

nextStep = 2;

break;

case 10:

this.m_nStatus = 1;

nextStep = -1;

break;

case 1:

case 9:

break;

} repaint();

return nextStep;

}

public void addNotify()

{

super.addNotify();

this.m_offImg = createImage(getSize().width, getSize().height);

this.m_offG = this.m_offImg.getGraphics();

}

private void drawNode(char ch, int i, int j, Color bkColor, Color fgColor, Graphics g)

{                   //绘制节点

Color old = g.getColor();

g.drawRect(20 + i * this.DX, 20 + j * 15, 20, 30);

g.setColor(bkColor);

g.fillRect(20 + i * this.DX + 1, 20 + j * 15 + 1, 19, 29);

char[] c = { ' ', ' ' }; c[0] = ch;

g.setColor(fgColor);

g.drawChars(c, 0, 1, 20 + i * this.DX + 6 + 1, 20 + j * 15 + 20);

g.setColor(old);

}

private void drawNode(char ch, float i, float j, Color bkColor, Color fgColor, Graphics g) {//绘制节点

Color old = g.getColor();

g.drawRect((int)(20.0F + i * this.DX), (int)(20.0F + j * 15.0F), 20, 30);

g.setColor(bkColor);

g.fillRect((int)(20.0F + i * this.DX + 1.0F), (int)(20.0F + j * 15.0F + 1.0F), 19, 29);

char[] c = { ' ', ' ' }; c[0] = ch;

g.setColor(fgColor);

g.drawChars(c, 0, 1, (int)(20.0F + i * this.DX + 6.0F + 1.0F), (int)(20.0F + j * 15.0F + 20.0F));

g.setColor(old);

}

public void swapNode(int I,int Temp, Color bkColor, Color fgColor)

{       //交换节点,I和Temp分别为两个互相交换节点的索引

Graphics g = getGraphics();

Color bk = getBackground();

Color old = g.getColor();

char ch1 = this.m_sData[I];

char ch2 = this.m_sData[Temp];

int y = 10;

g.setColor(bk);

g.fillRect(20 + I * this.DX - 1, 20 + y * 15 - 1, 22, 32);

g.setColor(old);

drawNode(ch1, (float)(I + 0.5D), y - 3, bk, fgColor, g);

delay(150);

g.setColor(bk);

g.fillRect(20 + Temp * this.DX - 1, 20 + y * 15 - 1, 22, 32);

g.setColor(old);

drawNode(ch2, I, y, bk, fgColor, g);

delay(150);

g.setColor(bk);

g.fillRect((int)(20.0D + (I + 0.5D) * this.DX - 1.0D), 20 + (y - 3) * 15 - 1, 22, 32);

g.setColor(old);

drawNode(ch1, Temp, y, bk, fgColor, g);

delay(150);

}

public void drawArrow(char ch, int i, int j, Color color, Graphics g)

{   //画箭头

Color old = g.getColor();

g.setColor(color);

char[] c = { ' ', ' ' };

c[0] = ch;

int x = 15 + i * this.DX + 6 + 4;

int y = 20 + j * 15 + 30 + 5;

g.drawChars(c, 0, 1, x - 8, y + 15);

g.drawLine(x, y, x, y + 25);

g.drawLine(x, y, x + 2, y + 5);

g.drawLine(x, y, x - 3, y + 5);

g.setColor(old);

}

public void drawArrow1(char ch, int i, int j, Color color, Graphics g)

{   //和上面的一样。就是x的坐标值不一样

Color old = g.getColor();

g.setColor(color);

char[] c = { ' ', ' ' };

c[0] = ch;

int x = 25 + i * this.DX + 6 + 4;

int y = 20 + j * 15 + 30 + 5;

g.drawChars(c, 0, 1, x - 8, y + 15);

g.drawLine(x, y, x, y + 25);

g.drawLine(x, y, x + 2, y + 5);

g.drawLine(x, y, x - 3, y + 5);

g.setColor(old);

}

public void setData(String sData, boolean bool)

{  //设置数据

this.m_sOldData = sData;

this.m_bSortType = bool;

this.m_nStatus = -1;

this.m_nNum = this.m_sOldData.length();

this.m_sData[0] = ' ';

if (this.m_nNum != 0)

this.DX = (300 / this.m_nNum);

else {

this.DX = 300;

}

for (int i = 1; i <= this.m_nNum; i++) {

this.m_sData[i] = sData.charAt(i - 1);

}

//this.m_nLast = 0;

this.m_nTemp = 0;

this.m_nJIndex = 0;

this.m_nIIndex = 0;

this.m_nCur = 0;

if (this.m_Graph == null) this.m_Graph = getGraphics();

}

public void update(Graphics g)

{   //更新视图

paint(g);

}

private void drawData(boolean bool, Graphics g)

{   //绘制数据

g.drawString("初始数据:", 20, 20);

Color bk = getBackground();

for (int i = 0; i < this.m_sOldData.length(); i++)

{

drawNode(this.m_sOldData.charAt(i), i + 1, 1, bk, Color.black, g);

}

if (bool)

{

g.drawString("排序结果:", 20, 150);

for (int i = 1; i < this.m_nNum + 1; i++)

drawNode(this.m_sData[i], i, 10, getBackground(), Color.black, g);

}

}

private boolean compare(char ch1, char ch2)

{   //比较,两种顺序

if (this.m_bSortType)

return ch1 < ch2;

return ch1 > ch2;

}

private void delay()

{

try

{

Thread.sleep(80L); } catch (InterruptedException e) {

}

}

private void delay(int time) {

try {

Thread.sleep(time);

}

catch (InterruptedException e)

{

}

}

public void init()

{  //初始化

setData("", true);

this.m_Graph.clearRect(0, 0, getSize().width, getSize().height);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值