0

I have been making a terminal styled... thing, and the only problem I'm still having trouble with (so far) is the loading screen. I simply wanted to add the illusion that it needed to load, nothing too complex. I need to delete the previous output line so it will load like, you know, a loading screen. I have looked through a lot of Q/As here and everything I tried didn't help I always either got random text or the text wasn't where it was supposed to be, or it plain didn't work. I am using Bluej.

Current Code:

public static void main(String [] args){
    try{
        System.out.println("▒█▀▀▀█ █▀▀█ █▀▀█ █▀▀█ ▀▀█▀▀ █▀▀█ █▀▀▄");
        Thread.sleep(1000);
        System.out.println("░▀▀▀▄▄ █░░█ █▄▄█ █▄▄▀ ░░█░░ █▄▄█ █░░█");
        Thread.sleep(1000);
        System.out.println("▒█▄▄▄█ █▀▀▀ ▀░░▀ ▀░▀▀ ░░▀░░ ▀░░▀ ▀░░▀");
        Thread.sleep(1000);
        System.out.println("o()xxxxxx[{:::::::::::::::::::::::::::::::>");
            Thread.sleep(1000);
            
            System.out.println("Loading... 0%");
            Thread.sleep(2000);
            //Code goes here
            System.out.println("Loading... 13%");
            Thread.sleep(2000);
            //Code goes here
            System.out.println("Loading... 42%");
            Thread.sleep(2000);
            //Code goes here
            System.out.println("Loading... 74%");
            Thread.sleep(4000);
            //Code goes here
            System.out.println("Loading... 92%");
            Thread.sleep(1000);
            //Code goes here
            System.out.println("Loading... 100%");
    }
    catch (InterruptedException e) {
        System.err.format("IOException: %s%n", e);
    }
}

I would like to post the full output, but the art won't mix with this, so here's what it says, minus the art:

Spartan

Loading... 0%

Loading... 13%

Loading... 42%

Loading... 74%

Loading... 92%

Loading... 100%

It shouldn't show several "Loading..."s

I am open to any suggestions, within reason.

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • Not a Java expert, but you can try it with Ansi escape codes like described in this answer: https://stackoverflow.com/questions/45068783/java-how-to-print-output-to-previous-line-after-input/45069499 – Alex Berger Jan 15 '21 at 15:36
  • [This](https://stackoverflow.com/questions/2979383/java-clear-the-console) could help as well – Jacob K Jan 15 '21 at 15:49

3 Answers3

1

This should do the trick:

public static void main(String [] args){
    try{
        System.out.println("▒█▀▀▀█ █▀▀█ █▀▀█ █▀▀█ ▀▀█▀▀ █▀▀█ █▀▀▄");
        Thread.sleep(1000);
        System.out.println("░▀▀▀▄▄ █░░█ █▄▄█ █▄▄▀ ░░█░░ █▄▄█ █░░█");
        Thread.sleep(1000);
        System.out.println("▒█▄▄▄█ █▀▀▀ ▀░░▀ ▀░▀▀ ░░▀░░ ▀░░▀ ▀░░▀");
        Thread.sleep(1000);
        System.out.println("o()xxxxxx[{:::::::::::::::::::::::::::::::>");
            Thread.sleep(1000);
            
            System.out.print("Loading... 0%xxxxxxxx");
            Thread.sleep(2000);
            //Code goes here
            System.out.print("\033[2K");
            System.out.print("\rLoading... 13%");
            Thread.sleep(2000);
            //Code goes here
            System.out.print("\033[2K");
            System.out.print("\rLoading... 42%");
            Thread.sleep(2000);
            //Code goes here
            System.out.print("\033[2K");
            System.out.print("\rLoading... 74%");
            Thread.sleep(4000);
            //Code goes here
            System.out.print("\033[2K");
            System.out.print("\rLoading... 92%");
            Thread.sleep(1000);
            //Code goes here
            System.out.print("\033[2K");
            System.out.print("\rLoading... 100%");
    }
    catch (InterruptedException e) {
        System.err.format("IOException: %s%n", e);
    }
}

The "\033[2K" deletes the current line and the "\r" brings the cursor back to the beginning of the line and overwrites what has already been written.

You can also just use "\r", but it would have this behavior:

System.out.print("Loading... 0%xxxx");
System.out.print("\rLoading... 13%");

The resulting output will be Loading... 13%xxx because the second print statement did not clear the entire line - it just wrote over what was already there!

Kavfixnel
  • 151
  • 3
  • 10
  • Yeah, that was one of the things I tried, and it just gave me a bunch of random text with the lines, I did use your exact code. Thanks for the suggestion though. – James The Arsonist Jan 15 '21 at 15:49
1

have been making a terminal styled... thing

System.out.println("▒█▀▀▀█ █▀▀█ █▀▀█ █▀▀█ ▀▀█▀▀ █▀▀█ █▀▀▄");

The problem is, System.out is 'standard output' and not a terminal. Sure, out of the box, on pretty much any OS, if you type java -jar yourapp.jar, 'standard output' is hooked up to the terminal, and terminals as a fundamental concept support the notion of overwriting/erasing previously printed text.

The problem is, the abstraction of 'standard output' allows you to hook up almost anything to standard out. You can write: java -jar yourapp.jar >/dev/printer and your printer will start rattling off your asciiart skills.

Now you see the problem: It is a bit tricky to instruct the printer to grow a pair of legs, hop off cupboard, jump over to the desk of the gal that just grabbed the piece of paper with your ascii art and 'loading' text, and unprint the text.

That's why System.out has no eraseLine method: Not all takes on what System.out is representing support such a principle.

Fortunately (?), terminals do support it, but, they do so by sending them 'commands' which takes the form of sending them creative character sequences that the terminal then interprets as 'oh, you want me to hop over to this position, change the background colour to green, the foreground colour to blue. Okay, got it'.

Unfortunately, there are many terminals out there (windows dos boxes, but once you get to the unix space, hoo boy, a list as long as my leg) and these differ in what magic sequences you have to send.

Thus, get a library that took care of it. I'm sure you'll be very interested in this: Lanterna.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Ok, first off, I don't really know how to respond other than 'not what I was asking, but will note' but I will try my best. The art seems to work just fine but the text box on this site makes it all one line. I was asking about how to replace one output line with another, in a sense, to make the loading message more believable, in a sense. and lastly, regardless of this answer being off topic from the question (from what i could tell), it is extremely helpful. Thank you. – James The Arsonist Jan 15 '21 at 16:06
  • It _was_ what you were asking: You wanted to erase previous output. I explained why the abstraction System.out fundamentally doesn't support this idea whatsoever, then explained how to still get what you want: By using a library that 'speaks' terminal. To be clear: __You cannot replace one output line with another__ without speaking terminal, which is non-trivial and requires a library. – rzwitserloot Jan 15 '21 at 17:07
  • Well ok I see my error, I was just having a bit of trouble following. – James The Arsonist Jan 16 '21 at 17:00
0

I guess you are looking for carriage return:

public static void main(String[] args) throws Exception
{
    System.out.print("Loading... 10%");
    Thread.sleep(1000);
    System.out.print("\rLoading... 20%");
    Thread.sleep(1000);
            
}
maio290
  • 6,440
  • 1
  • 21
  • 38