0

i want to move my shape on my console like they are falling down. what i tired doing is looping the shape 3 times issue is the top shapes still remain i do i delete/remove the previous shape or change there visibility? i was told you cant do that, then i want to know how do i go about this then and why cant i do so? hope this makes sense.

class Pyramid extends Thread {

    //shape length
    static int shapeLength = 10;
    //symbol for shape
    static String symbol = "$";

    static int m = 0;
    static int k;

    public static void move() throws Exception {

        //printing shape multiple times
        for (int m = 0; m < 3; m++) {
            try {
                Thread.sleep(500);
                //geting shape lenth
                for (int i = 0; i <= shapeLength; i++) {
                    //get shape size
                    for (int j = 0; j <= shapeLength - i; j++) {
                        //changing shape to 90 degress
                        System.out.print(" ");

                    }
                    //getting size
                    for (int k = 0; k <= i; k++) {
                        //giving space between symbol
                        System.out.print(symbol + " ");
                    }
                    //making triangle shape
                    System.out.println();
                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        move();
    }
}
azro
  • 53,056
  • 7
  • 34
  • 70
jack
  • 1
  • 3
  • 1
    Side note: those static ints `m` and `k` aren't used anywhere and can be removed. That being said to make it look like the shape moved you'd need to clear the console at the start of the rendering cycle. – Thomas Oct 30 '19 at 09:23

1 Answers1

1

In any rendering process you'd need to clear the canvas and render a new frame to get animations. Since you're printing on the console that's your canvas and thus you need to clear the console each time you want to render your shape.

The problem with consoles is that they're quite different and thus not all approaches work everywhere. For how to do it on a Windows or Linux command line have a look here: Java: Clear the console

So one way to make it work in Windows (and probably Linux as well) would be to add System.out.print("\033[H\033[2J");, e.g. your loop could look like this:

//m would be the "frame" number
for( int m = 0; m < 30; m++ ) {
  try {        
    //clear the console
    System.out.print("\033[H\033[2J");                      

    //render empty lines, one more for each "frame"
    for( int l = 0; l < m; l++) {
      System.out.println( );
    }

    ... //render your shape here

    //delay the next "frame"
    Thread.sleep( 100 );
  }
}

Note that because consoles work differently this won't work in an Eclipse console and I would assume neither does it in other IDEs' consoles.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • am just letting you know i tried the clear eclipse link https://stackoverflow.com/questions/2979383/java-clear-the-console and none of those work – jack Oct 30 '19 at 14:40
  • @jack well, yes that's what I wrote in my last sentence: it won't work in Eclipse and probably other IDEs as well. What works though (I tested it on Windows) would be to open a terminal in eclipse (or just a normal OS terminal) and run `java Pyramid` there. – Thomas Oct 30 '19 at 15:08