자바 애플 릿 콘 솔 문자 애니메이션 구현

앞에서 말 하 다
대학 1 학년 소프트웨어 공학 을 읽 고 있 습 니 다.자바 몽 은 새로운 한 마리 입 니 다.처음으로 블 로 그 를 쓰 는 것 은 기술 이 매우 뛰 어 나 므 로 뿌리 지 마 세 요.잘못 이 있 으 면 지적 을 환영 합 니 다!
이 프로그램 은 친구 의 생일 선물 로 반나절 이 걸 렸 는데 실제로 쓰 면 만 나 는 지식 과 어려움 이 많 기 때문에 보 내 서 공유 합 니 다.
프로그램 효과



캔버스 사 이 즈 를 설정 하고 그래 픽 키 트 를 추가 하 며 키 트 좌표 와 효 과 를 설정 할 수 있 습 니 다.부속품 반 짝 임 효과,가로 스크롤 효과.
코드 구현
그래 픽 소자 부류

public class Shape implements IShape{
	String shape[];//       
	String shape_flicker[];//       
	int height,width;// 、 
	int x,y;//    
	String id;//  id,             
	public Shape(int x,int y,String id) {//       
		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;//     
	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   -      
		}	
			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      
		
		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);
	}
}

총결산
자바 애플 릿 의 콘 솔 문자 애니메이션 실현 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.자바 콘 솔 문자 애니메이션 에 관 한 내용 은 예전 의 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 많은 응원 부탁드립니다!

좋은 웹페이지 즐겨찾기