Java playground

DWAPS
3,316 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Real animation in console

by dwaps

1
2
3
4
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// { autofold
public class Main {
public static void main(String[] args) {...}
}
class AnimationManager {
private int nbOfRepetition = 1;
public void numberOfRepetition(int nb) { nbOfRepetition = Math.abs(nb); }
public void startAnimation(String[][] strFrames) {
startAnimation(strFrames, false);
}
public void startAnimation(String[][] strFrames, boolean reverseAnim) {
if (!reverseAnim) {
for (int i = 0; i < strFrames.length; i++) display(strFrames[i]);
}
else {
for (int i = strFrames.length-1; i >= 0; i--) display(strFrames[i]);
}
nbOfRepetition--;
if (nbOfRepetition > 0) {
reverseAnim = !reverseAnim;
if (strFrames == FramesProvider.CAT_AND_MOUSE) changeCatDirection(strFrames, reverseAnim);
startAnimation(strFrames, reverseAnim);
}
}
private void display(String[] frameParts) {
clearConsole(50);
for (String f : frameParts) System.out.println(f);
sleep();
}
private void sleep() {
try { Thread.sleep(100); Runtime.getRuntime().exec("clear"); }
catch (Exception e) { e.printStackTrace(); }
}
private void clearConsole(int br) {
for (int i = 0; i < br; i++) System.out.println();
}
private void changeCatDirection(String[][] strFrames, boolean reversing) {
String[] tab = { "-oM", "Mo-" };
String oldStr = reversing ? tab[0] : tab[1];
String newStr = reversing ? tab[1] : tab[0];
for (int i = 0; i < strFrames.length; i++) {
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

® DWAPS Formation - Michael Cornillon

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content