I'm writing a simple console application (80x24) in Java.
Is there a gotoxy(x,y)
equivalent for the console?
I'm writing a simple console application (80x24) in Java.
Is there a gotoxy(x,y)
equivalent for the console?
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.
powershell
to start PowerShell and then in PowerShell start your program. This might require Windows 10 with the Anniversary Update. β
Attrahent I don't think there's a built-in function to do that in Java. There's a Java curses library called JCurses that you can use though.
Not without pulling in a console curses
style library...
You can try javacurses and see if that helps you.
I found Lanterna to be a very good library. It does not dependend on any native library but runs 100% in pure Java.
It offers a Screen
class which allows text output based on a coordinate system. For OS with a graphical environment it uses a Swing based terminal emulator. Unfortunately, you are not able to force terminal mode on Windows, so if you really need the terminal, use one of the solutions in the other answers.
write(char, x, y, foreground, background)
. do you know of a more recent replacement or any alternatives? i'll probably go with laterna but found this topic difficult to research. β
Range screen.putString()
. Example: screen.putString(0, 0, "foo", Color.WHITE, Color.BLACK);
β
Avens The easiest way to do that in my opinion is using ANSI escape sequences.
For this specific operation we'll use the escape sequence H
, to move to the point x,y we'll print to the screen ESC x;yH
.
In java that will be System.out.print("\u001B" + x + ";" + y + "H");
© 2022 - 2024 β McMap. All rights reserved.