java小程序之控制臺(tái)字符動(dòng)畫的實(shí)現(xiàn)
說(shuō)在前面
大一軟件工程在讀,java萌新一只,第一次寫博客,技術(shù)很菜勿噴。如有錯(cuò)誤歡迎指出!
這個(gè)小程序是給朋友的生日禮物,耗時(shí)半天,實(shí)際寫起來(lái)碰到的知識(shí)點(diǎn)和困難還挺多,故發(fā)出來(lái)分享一下。
程序效果
可設(shè)置畫布尺寸,添加圖形元件,設(shè)置元件坐標(biāo)和效果。元件閃爍效果,橫向滾動(dòng)效果。
代碼呈現(xiàn)
圖形元件父類
public class Shape implements IShape{ String shape[];//圖形形狀字符串 String shape_flicker[];//閃爍形狀字符串 int height,width;//高、寬 int x,y;//位置坐標(biāo) String id;//元件id,用于制作動(dòng)畫效果時(shí)獲取元件 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>();//圖形元件容器,添加到畫布的圖形都會(huì)存放在這 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++; } } }
動(dòng)畫類
import java.io.IOException; public class Animation {//用于動(dòng)畫效果 long timer;//計(jì)時(shí)器 int rolled;//滾動(dòng)計(jì)數(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替換為-實(shí)現(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) {//滾動(dòng)效果,id為元件id,from,to為起始和終止點(diǎn),speed為滾動(dòng)速度 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 {//播放動(dòng)畫,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中無(wú)效) { new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor(); // 清屏命令 } }
主類
import java.io.IOException; public class Main {//啟動(dòng)動(dòng)畫 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); } }
總結(jié)
到此這篇關(guān)于java小程序之控制臺(tái)字符動(dòng)畫實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)java控制臺(tái)字符動(dòng)畫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
這篇文章主要介紹了java中struts2實(shí)現(xiàn)文件上傳下載功能的方法,以實(shí)例形式較為詳細(xì)的分析了struts2實(shí)現(xiàn)文件上傳下載功能的具體實(shí)現(xiàn)技巧與相關(guān)問題的解決方法,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01Eclipse、MyEclipse 導(dǎo)入svn項(xiàng)目具體步驟
這篇文章主要介紹了Eclipse、MyEclipse 導(dǎo)入svn項(xiàng)目具體步驟的相關(guān)資料,需要的朋友可以參考下2016-10-10java中synchronized Lock(本地同步)鎖的8種情況
本文主要介紹了java中synchronized Lock(本地同步)鎖的8種情況,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Java 字節(jié)數(shù)組類型(byte[])與int類型互轉(zhuǎn)方法
下面小編就為大家?guī)?lái)一篇Java 字節(jié)數(shù)組類型(byte[])與int類型互轉(zhuǎn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-02-02SpringBoot 如何實(shí)現(xiàn)Session共享
這篇文章主要介紹了SpringBoot 如何實(shí)現(xiàn)Session共享,幫助大家更好的理解和學(xué)習(xí)spring boot框架,感興趣的朋友可以了解下2020-09-09