【Java】让控制台输出动起来
一、前言
有时候写的工具需要在控制台中输出日志等信息,尤其是运行在Linux系统下的工具,交互操作都只能在控制台中进行。看久了单调的信息输出后,总想来点不一样的。经过一番搜索和简单研究后写点东西记录一下。
二、动起来
让控制台的内容动起来其实相对简单,首先是清除控制台已输出的内容,其次根据动画的数据输出新的内容,然后重复上面的操作就可以得到动画效果。我也刚开始学习,写了一个简单类似柱状图的动态程序如下图。
1. 清除屏幕
清除控制台内容,以下区分了windows和Linux平台,已测试都可以用。
具体解释可以参考这篇文章。
https://blog.csdn.net/JackLang/article/details/52937071
protected void clear() { //windows new ProcessBuilder("cmd","/c" , "cls").inheritIO().start().waitFor(); //linux //new ProcessBuilder("clear").inheritIO().start().waitFor(); }
2. 进行绘制
我用“■”表示绘制的内容,用“□”表示背景网格,打印时类似柱状图从下往上显示具体的数据。
for(int i = 0; i < MAX_ROW_SIZE; i++) { StringBuilder sb = new StringBuilder(); for(Integer v : mValues) { if(MAX_ROW_SIZE - v <= i) { sb.append("■"); }else{ if(mBackShow) sb.append("□"); else sb.append(" "); } } System.out.println(sb.toString()); }
3. 增添色彩
控制台还可以输出不同颜色的内容,只需要在要输出的内容前面加上对应的颜色值,结束的时候不忘了重置颜色即可,例如下面的代码。【注意如果是在window平台运行,有可能会遇到无法显示颜色的情况】
System.out.println("\033[0;31m 这是红色的内容 \033[0m") System.out.println("\033[44m 这是蓝色的内容 \033[0m")
以下是在网上找到的控制台颜色定义,在要输入的内容前面加上颜色,最后需要加“\033[0m”进行重置颜色,以下内容并未经过充分验证,还请谨慎使用。
public class ConsoleColors { // Reset public static final String RESET = "\033[0m"; // Text Reset // Regular Colors public static final String BLACK = "\033[0;30m"; // BLACK public static final String RED = "\033[0;31m"; // RED public static final String GREEN = "\033[0;32m"; // GREEN public static final String YELLOW = "\033[0;33m"; // YELLOW public static final String BLUE = "\033[0;34m"; // BLUE public static final String PURPLE = "\033[0;35m"; // PURPLE public static final String CYAN = "\033[0;36m"; // CYAN public static final String WHITE = "\033[0;37m"; // WHITE // Bold public static final String BLACK_BOLD = "\033[1;30m"; // BLACK public static final String RED_BOLD = "\033[1;31m"; // RED public static final String GREEN_BOLD = "\033[1;32m"; // GREEN public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW public static final String BLUE_BOLD = "\033[1;34m"; // BLUE public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE public static final String CYAN_BOLD = "\033[1;36m"; // CYAN public static final String WHITE_BOLD = "\033[1;37m"; // WHITE // Underline public static final String BLACK_UNDERLINED = "\033[4;30m"; // BLACK public static final String RED_UNDERLINED = "\033[4;31m"; // RED public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW public static final String BLUE_UNDERLINED = "\033[4;34m"; // BLUE public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE public static final String CYAN_UNDERLINED = "\033[4;36m"; // CYAN public static final String WHITE_UNDERLINED = "\033[4;37m"; // WHITE // Background public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK public static final String RED_BACKGROUND = "\033[41m"; // RED public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN public static final String WHITE_BACKGROUND = "\033[47m"; // WHITE // High Intensity public static final String BLACK_BRIGHT = "\033[0;90m"; // BLACK public static final String RED_BRIGHT = "\033[0;91m"; // RED public static final String GREEN_BRIGHT = "\033[0;92m"; // GREEN public static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW public static final String BLUE_BRIGHT = "\033[0;94m"; // BLUE public static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE public static final String CYAN_BRIGHT = "\033[0;96m"; // CYAN public static final String WHITE_BRIGHT = "\033[0;97m"; // WHITE // Bold High Intensity public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m";// YELLOW public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m";// PURPLE public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE // High Intensity backgrounds public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";// BLACK public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";// RED public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREEN public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOW public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUE public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE }
三、结束
本篇文章内容有点简短,具体的代码工程我放在Github上面,还有就是我在找这期的资料的时候发现很多控制台做的小游戏,我也贴一篇其他人的文章,有兴趣深入学习的可以去看看,希望对大家有所帮助。
欢迎微信搜索"游戏测试开发"关注一起沟通交流。