] > Assignment 1: Getting Started—Control Characters, ASCII Art, and ANSI Art (Due: Tu, Oct 7)

Assignment 1: Getting Started—Control Characters, ASCII Art, and ANSI Art (Due: Tu, Oct 7)

Note. The program assumes a coordinate system whose origin is at the upper-left corner.

      2    4    6   8   10  ----|-|-|--|-|-|-|--|-|-|||X  -- 2--      ∙ (3,2)  -- 4--  -- 6--|    Y

<..Art.java..>
 class Art{
    public static void main( String [] args )
                                  throws Exception{
       System.out.println( "Type ‘q’ or nothing + Enter" );
       while( true ){
          int c = System.in.read();
          if( c == ’q’ ){ break; }
          clear();
          moveTo( (int)  Math.round( Math.random()*50 )
                   ,
                  (int) Math.round( Math.random()*50 )
                );
          move(1,1);   paintRect(8,8);
          move(4,8);   paintRect(4,4);
          move(4,0);   paintRect(2,2);
          move(0,-6);  paintRect(2,2);
          move( 10,0);
          paintText( "Type ‘q’ or nothing + Enter" );
    }  }
 
    static char escape = (char) 27;
    static char pen = ’#’;
    static int row = 0, col = 0;
 
    static void clear(){
      System.out.print( escape + "[2J" );
      row = 0;
      col = 0;
    }
    static void setPen(char p){
       pen = p;
    }
    static void moveTo(int r, int c){
       row = r;
       col = c;
    }
    static void move(int r, int c){
       row += r;
       col += c;
    }
    static void paintRect( int  width, int  height )
    {
      for( int i=0; i<height; i++) {
        for( int j=0; j<width; j++ ){
           System.out.print( escape + "["
                + (row+i) + ";" + (col+j) + "H" );
           System.out.print( pen );
    } } }
    static void paintText( String text ){
        System.out.print( escape + "[" + row + ";" + col + "H" );
        System.out.print( text );
    }
 }
-_-_-