Friday, May 7, 2010

JAVA : Scroll Bar Animation

Creating a Frame in which a number of Scrollbar (for example 20 Scrollbar) are doing animations.




 Code For Creating Scrollbar animation:-



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Scrollbar extends JFrame implements Runnable{
JScrollBar sb[]= new JScrollBar[20];            //declaring a array of 20 Scroll Bars
int i=1;                                                      //variable to know how many scroll bar will change in there value(used in animation)
boolean b=true;

Scrollbar(){
super();
setLayout(new GridLayout(1,1));                    //setting the layout

for(int a=0;a<20;a++){                                //loop for initializing memory to scrollbars
sb[a]= new JScrollBar();
sb[a].setMaximum(5000);                         //setting maximum value of scrollbar
sb[a].setVisibleAmount(2000);                 //setting visible amount of sliding bar
add(sb[a]); //adding scrollbar to frame
}

}


public void animation(){
try{

for(int n=0;n<=i;n++){
                        //this will run for increment the scrollbar visible amount
if((sb[n].getVisibleAmount()+22)<4000 && b==true)
{
//setting visible amount of sliding bar 
sb[n].setVisibleAmount(sb[n].getVisibleAmount()+n+1);

}

else{
b=false;         //boolean to decide which if condition will run
}



//this will run for decrement the scrollbar visible amount
if( (sb[n].getVisibleAmount()-22)>1000 && b==false){

 //setting visible amount of sliding bar 

sb[n].setVisibleAmount(sb[n].getVisibleAmount()-n-1);
}
else{
b=true;                     //boolean to decide which if condition will run
}
}

if (i<19){
i++;          //increment variable i (every time loops runs i incremented by 1 which increases the
                        //number of scroll bar changing there visible amount )
}
else{
i=0;          //if i becomes greater than number of scrollbar then setting i=0
}

}
catch(Exception e){
System.out.println("error" +e);
}
}

//function of implemented interface runnable

public void run(){
 while(true){                             //running the animation in infinite loop
animation();
try{
Thread.sleep(2);
}
catch(Exception e){
}
}

}


public static void main(String a[]){
Scrollbar s= new Scrollbar();
s.setVisible(true);
s.setDefaultCloseOperation(EXIT_ON_CLOSE);
s.setSize(370,700);
Thread t = new Thread(s);                 //creating thread
t.start();
}
}



Working of Program :-


Making a Thread:-
Thread is a single sequential flow of control within a program. Programmer may use java thread mechanism to execute multiple tasks at the same time. 
Here, Thread is use to run a process(animation method) in infinite loop and thread sleeps for 2 milliseconds every time time loop run again.


Animation method:-
Animation method  contains a for loop in which there is two if conditions, one for incrementing the visible amount of sliding bar and another is for decrementing the visible amount of sliding bar.
Every time the loop runs the the visible amount of the sliding bar gets increment till the value reaches the 4000 and then the decrement of visible amount starts until the value reaches 1000 and the same process goes on.

2 comments: