0

I am trying to create a version of Conway's Game of Life in Java, where after each iteration, the console is updated in some way to imitate the next generation.

I have seen Runtime.getRuntime().exec("cls"); but that clears the console and creates a black screen delay between iterations, which is visually annoying. Using @Holger 's info from this answer,

    public class CLS {
    public static void main(String... arg) throws IOException, InterruptedException {
        new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
    }
}

I am more wondering if there is a way to make it smoother. For example, the updating mechanic requires the entire console data to be removed, which causes a momentary black screen that is distracting. Is there a way to update the console without clearing?

Please let me know if you can think of any workaround or better way of solving this problem!

1 Answers1

0

The following code works for me. Notice how the expression with ProcessBuilder is separated into a new static method; perhaps you were confused by the use of main method in the linked answer.

import java.io.*;

public class Test {

    private static void clearScreen() throws IOException, InterruptedException {
        new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
    }

    public static void main(String... arg) throws IOException, InterruptedException {
        System.out.println("One");
        Thread.sleep(1000l); // Wait 1 second
        clearScreen();

        System.out.println("Two");
        Thread.sleep(1000l); // Wait 1 second
        clearScreen();

        System.out.println("Three");
    }

}
OLEGSHA
  • 388
  • 3
  • 13
  • Yes, that works, but I am wondering if there is a way to make it smoother. For example, the updating mechanic requires the entire console data to be removed, which causes a momentary black screen that is distracting. Is there a way to update the console without clearing? – RagingMonkey Oct 19 '22 at 16:44
  • I think you should consider using a proper console drawing library if you want better performance. – OLEGSHA Oct 19 '22 at 17:04