溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用Java實現(xiàn)控制臺字符動畫的小程序

發(fā)布時間:2022-02-23 15:20:32 來源:億速云 閱讀:157 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“如何使用Java實現(xiàn)控制臺字符動畫的小程序”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“如何使用Java實現(xiàn)控制臺字符動畫的小程序”文章能幫助大家解決問題。

代碼呈現(xiàn)

圖形元件父類

public class Shape implements IShape{
	String shape[];//圖形形狀字符串
	String shape_flicker[];//閃爍形狀字符串
	int height,width;//高、寬
	int x,y;//位置坐標(biāo)
	String id;//元件id,用于制作動畫效果時獲取元件
	public Shape(int x,int y,String id) {//構(gòu)造方法初始化
		this.x=x;this.y=y;this.id=id;
	}
	
	public Shape(String id) {
		this(0,0,id);
	}
}

圖形繪畫工具類

import java.util.HashMap;

public class Shapes {//存放圖形元件
	int width,height;//畫布大小
	public static String canvas[];//畫布圖像字符串
	HashMap<String, Shape> ShapeMap=new HashMap<String,Shape>();//圖形元件容器,添加到畫布的圖形都會存放在這
	public Shapes(int width ,int height) {//初始化空白畫布
		this.width=width;
		this.height=height;
		canvas=new String[height];
		for(int h=0;h<height;h++) {
			String line="";
			for(int w=0;w<width;w++){
				line+=" ";
			}
			canvas[h]=line;
		}
	}
	
	public void draw(Shape myShape) {//將元件添加到畫布中
		int px,py;
		px=myShape.x;
		py=myShape.y;
		int count=0;
		if(myShape.height+py>height-1) {
			System.out.println("超出畫布邊界!!");
			return;
		}
		if(myShape.width+px>width-1) {
			System.out.println("超出畫布邊界!!");
			return;
		}
		ShapeMap.put(myShape.id,myShape);//將元件添加到容器中
		for(String line :myShape.shape) {
			
			char Line[]=canvas[py+count].toCharArray();
			for(int i=px;i<myShape.width+px;i++) {
				
				Line[i]=line.charAt(i-px);
			}
			canvas[py+count]=String.valueOf(Line);
			count++;
		}

	}
	
	public void drawCanvas() {//繪制畫布
		System.out.print(" ");
		for(int i=0;i<width;i++) {
			System.out.print(i%10);
		}
		System.out.println();
		int count=0;
		for(String line: canvas) {
			System.out.println(count+line);
			count++;
		}
	}
}

動畫類

import java.io.IOException;

public class Animation {//用于動畫效果
	long timer;//計時器
	int rolled;//滾動計數(shù)器
	private Shapes shapes;//圖形工具
	
	public Animation() {
		timer=0;
		rolled=0;
		init();
	}
	public void flicker(String id,int interval) {//閃爍效果,id為元件的id,interval是閃爍間隔
		
		Shape myShape=shapes.ShapeMap.get(id);
		String shape_flicker[]=myShape.shape.clone(); //閃爍圖像
		for(int i=0;i<shape_flicker.length;i++) {
			shape_flicker[i]=shape_flicker[i].replaceAll("O","-");//將O替換為-實現(xiàn)閃爍效果
		}	
			myShape.shape_flicker=shape_flicker;
			//繪制圖像
			if(timer%interval==0) {
				int px,py;
				px=myShape.x;
				py=myShape.y;
				int count=0;
				if((timer/interval)%2==0) {
					for(String line :myShape.shape_flicker) {
						
						char Line[]=Shapes.canvas[py+count].toCharArray();
						for(int i=px;i<myShape.width+px;i++) {
							
							Line[i]=line.charAt(i-px);
						}
						Shapes.canvas[py+count]=String.valueOf(Line);
						count++;
					}
					
				}else {
					
					for(String line :myShape.shape) {
						char Line[]=Shapes.canvas[py+count].toCharArray();
						for(int i=px;i<myShape.width+px;i++) {
							
							Line[i]=line.charAt(i-px);
						}
						Shapes.canvas[py+count]=String.valueOf(Line);
						count++;
					}
				}

				

			}
			
		
	}
	
	public void roll(String id,int from ,int to,int speed) {//滾動效果,id為元件id,from,to為起始和終止點,speed為滾動速度
		
		rolled+=speed;
		Shape myShape=shapes.ShapeMap.get(id);
		String shape_roll[]=myShape.shape.clone();
		myShape.x=from+rolled%(to-from);
		
		int px,py;
		px=myShape.x;
		py=myShape.y;
		int count=0;
		System.out.println("rolled:"+rolled+"px:"+px);
			for(String line :shape_roll) {
				
				char Line[]=Shapes.canvas[py+count].toCharArray();
				for(int i=from;i<to;i++) {
					if(i>=px&&i<=to&&i<px+line.length()) {
						Line[i]=line.charAt(i-px);
					}else {
						Line[i]=' ';

					
				}
				
				}
				Shapes.canvas[py+count]=String.valueOf(Line);
				count++;
			}
	}
	
	private void init() {//初始化畫布,添加元件
		shapes=new Shapes(120,50);
		shapes.draw(new Shape_Text(5,10,"HB1"));
		shapes.draw(new Shape_Nineteen(52,21,"Nt1"));
		shapes.draw(new Shape_Cake(45,30,"Cake1"));
		shapes.draw(new Shape_Bubble(10,25,"BB1"));
		shapes.draw(new Shape_Bubble(90,25,"BB2"));
	}
	
	public void play(int sleep) throws  IOException, InterruptedException {//播放動畫,sleep設(shè)置刷新間隔
		
		while(true) {
			if(timer>300) {
				timer=0;
			}
			cls();
			if(timer<100) {
				flicker("HB1",5);
			}else {
				roll("HB1",0,110,1);
			}
			
			
			flicker("Nt1",10);
			shapes.drawCanvas();
			timer++;
			Thread.sleep(sleep);
			System.out.println(timer);
		}
		

	}
	

	public static void cls() throws IOException, InterruptedException//清屏方法(ide中無效)
	{

		new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor(); // 清屏命令
	}
}

主類

import java.io.IOException;

public class Main {//啟動動畫
	public static void main(String args[]) throws InterruptedException, IOException {
		Animation animator=new Animation();
		
		animator.play(30);
	}
		
	
}

具體圖形子類(Happy Birthday文字)

public class Shape_Text extends Shape{//繼承圖形父類
	
	String s[]= {//字符圖像
		"==================================================================================================",
		"= O    O   OO    OOOO   OOOO  O    O       OOOOO  OOOOO OOOOOO OOOOOO O    O OOOOO    OO   O    O =",
		"= O    O  O  O  O    O O    O O    O       O    O   O   O    O   OO   O    O O    O  O  O  O    O =",	
		"= OOOOOO O    O O    O O    O O    O       O    O   O   OOOOOO   OO   OOOOOO O    O O    O O    O =",	
		"= O    O OOOOOO OOOOO  OOOOO   OOOO        OOOOO    O   O O      OO   O    O O    O OOOOOO  OOOO  =",	
		"= O    O O    O O      O         O         O    O   O   O  O     OO   O    O O    O O    O    O   =",	
		"= O    O O    O O      O         O         OOOOOO OOOOO O   O    OO   O    O OOOOO  O    O    O   =",
		"=================================================================================================="
	};
	
	public Shape_Text(int i, int j,String id) {
		super(i,j,id);
		this.shape=s;
		this.height=shape.length;
		this.width=shape[0].length();
	}
	
	public Shape_Text(String id) {
		this(0,0,id);
	}
}

關(guān)于“如何使用Java實現(xiàn)控制臺字符動畫的小程序”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI