2

I want to do a terminal Graphic lib for funny and have seem many cool responsity, such as, asciimoo/drawille.

Using \r can redraw current line but how about former line?
For example, I have output three line(perhaps shouldn't use \n):

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

//output
00000
0   0
00000

at next frame(eg. after 0.1s), I want redraw the second and third line. Hope result as:

00000
00 00
00 00

how to do this in terminal?

LoranceChen
  • 2,453
  • 2
  • 22
  • 48

1 Answers1

-1

This will set up a console that has different states based on what the user enters. For now I just have the ability to go up ("w") or go down ("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));
    }
}

Output looks like:

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

Note: that I'm using the standard wsad controls that most games have. I recommend it. w for up, a for left, s for down and d for right. Also i display the default state whenever an incorrect key is pressed and "exit" is used to exit the game.

RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
  • Oh, I think you also make some mislead.Please consider make input `s`'s result able to override its upper print rather then output with new a line. In other word, `s/w/e` will only change the head of three lines.:-( – LoranceChen Jan 13 '17 at 17:44
  • well as soon as you output to console there isn't a way, as far as I know, that will be able to delete/overwrite what you output to it @LoranceChen – RAZ_Muh_Taz Jan 13 '17 at 17:53
  • but \r works to rewrite current line so I think terminal agree us to move cursor to former line place.what the jack says seems work.Recommand try it! – LoranceChen Jan 13 '17 at 18:08
  • how would call \r to rewrite current line? all it does is add a new line in the system.out.print call – RAZ_Muh_Taz Jan 13 '17 at 18:10
  • 1
    try `print("\raaaa");Thread.sleep(2000);print("\rbbbb");` and you will see aaaa be override by bbbb. – LoranceChen Jan 13 '17 at 18:16
  • when i call System.out.print() with your \raaaa then sleep then call \rbbbb it just outputs it on the line below it... it doesn't overwrite it @LoranceChen – RAZ_Muh_Taz Jan 13 '17 at 18:19
  • sorry,I just awake now. what's the IDE do you use?idea IDE can run it well with run button.I think compile it to jar and run in powershell or Unix-like shell well be better(not sure yet). – LoranceChen Jan 14 '17 at 00:15