Java Gotoxy(X,Y) for Console Applications

Move console cursor to specified position

If by gotoxy(x,y), you want to reposition your cursor somewhere specific on the console, you can usually use VT100 control codes to do this. See http://www.termsys.demon.co.uk/vtansi.htm.

Do something like

char escCode = 0x1B;
int row = 10; int column = 10;
System.out.print(String.format("%c[%d;%df",escCode,row,column));

Which should move the cursor to position 10,10 on the console.

How to print a character in an x,y coordinate in the console using kotlin?

Just as with C, Java provides no built-in way to do full-screen terminal output.

And, as with C, you have two main options:

  • You could assume that the terminal understands ANSI escape codes, and output those directly.  That's quick-and-dirty; it doesn't require any dependencies, and the basics can be done fairly easily.  But it's not very maintainable; anything more sophisticated gets very fiddly — and, more importantly, it's likely to fail on other terminal types.

  • Or you could use a library which does all the hard work.  These questions ask about that, and have several answers.

Move printing position of Command Line Interface in Java without using External library

The ability to do this is a property of the terminal, not the language. So in principal, if you're connected to a sufficiently capable terminal emulator, then yes, of course it's possible.

The purpose of a library like ncurses is to abstract away the gory details of terminal-dependent cursor movement, etc. You don't need something like ncurses, you could always just directly emit the appropriate codes for your target terminal.

By, "is there an equivalent method in Java," do you mean are there libraries which also can provide you terminal-agnostic abstractions? Yes (see the other responses). But nothing is going to make every host system to the JVM provide a VT100 emulator. For example, good luck on Windows. In this sense, 2D graphics in Java are more universal than the terminal environment!



Related Topics



Leave a reply



Submit