Bouncing Ball pattern program in java


Hello friends, welcome to programmingscoop. Today i'm going share my new pattern design program written in java. As we all know pattern designing is the best way to enhance our logical building skill. so try to learn form today's code for understand logic building. Comments and suggestion are most welcome.

About program 
 
Bouncing ball pattern
Square bouncing ball pattern
In this program we will create a pattern in which there are '*' (Start)  shape balls are there and they move in square form. I recommend you to download the full code and run it on your pc for better understanding of this program. you can download the full code from here. if you have any query so feel free to ask in comment section. so let's understand the program. 

Let's start 

In this program i have created only one class namely square_pattern for designing the pattern. It contains two methods main() and design_pattern().

Main() Method

In main method we create the object of square_pattern class and call its method design_pattern().

  1. /*    main method    */       
  2.     public static void main(String args[])
  3.     {
  4.         square_pattern obj=new square_pattern();  //creating object
  5.         obj.design_pattern();        //calling method   
  6.         System.out.println();
  7.     }

design_pattern() Method 

This method do the main functioning of program it create the pattern on screen.I have used many comments so you can easily understand the code but if you still have any query ask me from comment section any time.



  1. class square_pattern
  2. {
  3.     int width=80;   //width of terminal
  4.     int height=24;  //height of terminal
  5.     int i;        //used for loop        
  6.     //int f,s,t;
  7.     int nl;         //used for taking new line
  8.     int loop=1;     //used for continuous of printting
  9.     int rev=0;      //used to reverse the ball
  10.     int rwidth=0;    //represent right side width of ball
  11.     int lwidth=0;   //represent left side width of ball
  12.     /*   used for desining pattern    */
  13.     public void design_pattern()
  14.     {     
  15.        /*   this while loop will repeat the complete  pattern again & again     */
  16.         while(loop!=0)
  17.         {   
  18.                   rev=0;rwidth=0;lwidth=0;    //reinitializing of variables
  19.                      
  20.            /* this loop will print the complete pattern once*/   
  21.                       for(i=1;i<=height-3;i++)
  22.             {
  23.                
  24.                 /*   for clearing and holding the screen */   
  25.                 try{
  26.                     Thread.sleep(50);                   //for holding screen
  27.                     System.out.print("\033[H\033[2J");  //for clearing screen
  28.                     System.out.flush();
  29.                     }
  30.                 catch(Exception e)
  31.                   {
  32.                   }
  33.                
  34.                  /******     printing of first ball             ********/
  35.                      //this will move '*' downward    
  36.                     if(i<=(height/2)-1)
  37.                     {   
  38.                        for(nl=1;nl<i;nl++)
  39.                        System.out.println(" ");
  40.                     }
  41.                    //this will move '*' upward   
  42.                     if(i>(height/2)-1)
  43.                     {   
  44.                        rev=rev+2;
  45.                        for(nl=1;nl<i-rev;nl++)
  46.                        System.out.println(" ");
  47.                     }
  48.                        
  49.                        
  50.                     System.out.println();
  51.        
  52.                    // calculating spaces to print before '*'   
  53.                     for(int space=1;space<=width/2;space++)
  54.                         System.out.print(" ");
  55.        
  56.                   //printing first star                            
  57.                     System.out.print("*");
  58.                    /******        printing middle line             *******/
  59.                 //this maintain the distance between first '*' and middle line                    
  60.                     if(i<=(height/2)-1)
  61.                     {  
  62.                        for(nl=i;nl<=(height/2)-2;nl++)
  63.                        System.out.println(" ");
  64.                     }
  65.                     if(i>(height/2)-1)
  66.                     {   
  67.                        for(nl=i;nl>(height/2)-1;nl--)
  68.                        System.out.println(" ");
  69.                     }
  70.            
  71.                
  72.                
  73.                     System.out.println();
  74.                 //this will move the first middle line '*' towards right   
  75.                     if(i<=11)   
  76.                     {   
  77.                        lwidth=lwidth+3;
  78.                        for(int space=1;space<=lwidth;space++)
  79.                        System.out.print(" ");
  80.                     }
  81.                 //this will move the first middle line '*' towards left   
  82.                     if(i>11)   
  83.                     {
  84.                                     lwidth=lwidth-3;
  85.                         if(i==21){lwidth=lwidth-1;}               
  86.                        
  87.                         for(int space=1;space<=lwidth;space++)
  88.                         System.out.print(" ");
  89.                     }   
  90.                 //printing first middle line '*'   
  91.                     System.out.print("*");
  92.                 //this will maintain the distance between first and second middle line '*'
  93.                     for(int space=1;space<=(width/2)-(lwidth+1);space++)
  94.                           System.out.print(" ");
  95.                 //printing second middle line '*'
  96.                     System.out.print("*");
  97.                 //this will move the third middle line '*' towards left       
  98.                     if(i<=11)   
  99.                     {    rwidth=rwidth+3;
  100.                         for(int space=1;space<=(width/2)-rwidth;space++)
  101.                         System.out.print(" ");
  102.                     }
  103.                 //this will move the third middle line '*' towards right   
  104.                     if(i>11)   
  105.                     {    rwidth=rwidth-3;
  106.                         for(int space=1;space<=(width/2)-rwidth;space++)
  107.                         System.out.print(" ");
  108.                     }
  109.                
  110.                 //printing third middle line star
  111.                     System.out.print("*");
  112.                    
  113.                   /******        printing last ball            ******/
  114.                    //this will move the '*' towards upward
  115.                     if(i<=(height/2)-1)
  116.                         {
  117.                             for(nl=i;nl<(height/2)-1;nl++)
  118.                            System.out.println(" ");
  119.                         }
  120.                   //this will move the '*' towards downward
  121.                     if(i>(height/2)-1)
  122.                         {   
  123.                            for(nl=i;nl>(height/2)-1;nl--)
  124.                            System.out.println(" ");
  125.                         }
  126.                     System.out.println();
  127.                 //this will calculate no. of spaces before printing '*'
  128.                     for(int space=1;space<=width/2;space++)
  129.                        System.out.print(" ");
  130.                 //printing last '*'   
  131.                     System.out.print("*");
  132.                    
  133.             }       
  134.         }
  135.     }

Now I hope you learn something from this program and it help tell me your views by commenting. you can download the full code form here.
Visit our other post to enhance or learn your programming.