【问题标题】:How to redraw print console info in java?如何在java中重绘打印控制台信息?
【发布时间】:2017-05-29 01:56:57
【问题描述】:

我想做一个有趣的终端图形库,并且看起来很酷,例如asciimoo/drawille

使用 \r 可以重绘当前行,但是前一行呢?
例如,我输出了三行(也许不应该使用\n):

print("00000\n")
print("0   0\n")
print("00000\n")

//output
00000
0   0
00000

在下一帧(例如 0.1 秒后),我想重绘第二行和第三行。希望结果为:

00000
00 00
00 00

如何在终端中执行此操作?

【问题讨论】:

  • 你能不能把每一行当作它自己的字符串,然后把它改成你想要的,然后将这些行重新打印到控制台?
  • @RAZ_Muh_Taz,是的,我想要重新打印控制台块,就像它是 GUI 窗口或画布一样。
  • 线条变化的原因/原因是什么?只是想了解什么时候一行必须是别的东西......
  • @RAZ_Muh_Taz,任何时候都会发生。一个基本的用法是我希望创建一个坦克游戏;-),所以当你用键盘控制你的坦克时它会改变。

标签: java terminal printf


【解决方案1】:

这将设置一个控制台,该控制台根据用户输入的内容具有不同的状态。现在我只能上(“w”)或下(“s”)

public static void main(String[] args) {

        HashMap<Integer, HashMap<String, String>> consoleStates = CreateConsoleStates();
        String input = "default";
        Scanner userInput = new Scanner(System.in);
        do {
            //draw default state of board
            if(input.equals("default") || input.equals("w") || input.equals("s"))
                printConsole(input, consoleStates);
            //get input from user until they exit
            input = userInput.nextLine().replaceAll("\n", "");

        } while (!input.equals("exit"));

    }//main method

    public static HashMap<Integer, HashMap<String, String>> CreateConsoleStates()
    {
        HashMap<Integer, HashMap<String, String>> consoleStates = new HashMap<>();
        HashMap<String, String> line1, line2, line3;

        line1 = new HashMap<>();
        line1.put("default", "00000");
        line1.put("w", "00000");
        line1.put("s", "00 00");

        line2 = new HashMap<>();
        line2.put("default", "0   0");
        line2.put("w", "00 00");
        line2.put("s", "00 00");

        line3 = new HashMap<>();
        line3.put("default", "00000");
        line3.put("w", "00 00");
        line3.put("s", "00000");

        consoleStates.put(1, line1);
        consoleStates.put(2, line2);
        consoleStates.put(3, line3);

        return consoleStates;

    }

    public static void printConsole(String state, HashMap<Integer, HashMap<String, String>> consoleStates)
    {
        //adding this will make it seem like a brand new console as suggested in another answer
        //System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
        //each number corresponds to the line in the console
        System.out.println(consoleStates.get(1).get(state));
        System.out.println(consoleStates.get(2).get(state));
        System.out.println(consoleStates.get(3).get(state));
    }
}

输出如下:

00000
0   0
00000
s
00 00
00 00
00000
w
00000
00 00
00 00
e
00000
0   0
00000
exit

注意:我使用的是大多数游戏都有的标准 wsad 控件。我推荐它。 w 向上,a 向左,s 向下,d 向右。此外,每当按下不正确的键并使用“exit”退出游戏时,我都会显示默认状态。

【讨论】:

  • 哦,我想你也误导了。请考虑让输入s 的结果能够覆盖其上部打印,而不是用新的一行输出。也就是说s/w/e只会改变三行的头部。:-(
  • 一旦你输出到控制台,据我所知,没有办法删除/覆盖你输出给它的内容@LoranceChen
  • 但是 \r 可以重写当前行,所以我认为终端同意我们将光标移动到前行位置。杰克所说的似乎有效。建议尝试一下!
  • 如何调用 \r 来重写当前行?它所做的只是在 system.out.print 调用中添加一个新行
  • 试试print("\raaaa");Thread.sleep(2000);print("\rbbbb");,你会看到aaaa被bbbb覆盖。
猜你喜欢
  • 2020-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-20
  • 1970-01-01
相关资源
最近更新 更多