Built an applet that displays a horizontal rectangle in its center. Let the rectangle fill with color from left to right.

Experiment : 3
આપણે આ practical માં applet માં progress bar બનાવવાની છે.એના માટે વચ્ચે એક આળું લંબચોરસ ની અંદર કલર વાળા લંબચોરસ ને ભરી ને કરી સકાય.
Code = ProgressD.java


import java.applet.Applet;
import java.awt.*;

/*<applet code="ProgressD.class" width=320 height=150>
</applet>
*/

public class ProgressD extends Applet{

    public void paint(Graphics G){
        G.drawRect(48,50,218,50);
        
        for(int x=50;x<=260;x=x+12){
            try{
            Thread.sleep(500);
            G.fillRect(x,52,10,46);
            }catch(Exception e){

            }
        }
    }

}
  • Output :





 
ચાલો તો એક પછી એક કરી લાઈન by લાઈન કોડ ને સમજયે 

import java.applet.Applet;
import java.awt.*;
આ લાઈન માં import નો ઉપયોગ લાઇબ્રેરી ને add કરવા ઊપયોગ થયો છે, java applet અને java awt librery ને import કરવી છે એટલે તેના રહેલા function method નો ઉપયોગ આપણે કોડ માં કરી સકાય.

/*<applet code="ProgressD.class" width=500 height=300>
</applet>*/
ઉપર ની લાઈન માં આપણે applet ના dimention hight(ઊંચાઈ) ને width (પોહળાઇ) ને નક્કી કરી છે.

public class ProgressD extends Applet
ProgressD નામનો class (keyword) બનાવ્યો, અને extands (keyword) નો ઉપયોગ કરી ને applet class ને inherat કર્યો જેથી applet class ની વસ્તુ નો ઊપયોગ કરી સકાય.

public void paint(Graphics g)
અહી paint નામની method બનાવી જેનો return type void છે, એટલે એ કાઇપણ return કરસે નહિ. અને Graphics નો object g ને બનાવી પાસ કર્યો જેથી g નો ઉપયોગ કરી applet માં graphics લગતા function use કરી સકાય.
drawRect મેથોડનો ઉપયોગ કરીને સીધું બાજુથી 48 પિક્સલ અને 50 પિક્સલની સ્થાનિકતાથી 218 પિક્સલ અને 50 પિક્સલ સુધીનું રેક્ટેંગલ બનાવીશ છે.


ટ્રાય (Try) એક એક્સેપ્શન હેન્ડલિંગ નો મૂળ પદ્ધતિ છે જેમાં પ્રોગ્રામમાં એન્યુનો ભાંગવું અથવા અસામાન્ય ક્રિયા કરવી હોય, જેમનો પરિણામસ્વરૂપ એક એક્સેપ્શન ઉત્પન્ન થતો હોય.

ટ્રાય બ્લોકનો સારો ઉપયોગ એ છે કે તે એક સુસંગત પ્રોગ્રામ દ્વારા અસુવિધાની સ્થિતિઓનો ધ્યાન ધરાવે. ઉદાહરણ તરીકે, જો તમે ફાઇલ વાંચવાનો પ્રયાસ કરો છો અને ફાઇલ અસ્તિત્વમાં ન હોય, તો એક `FileNotFoundException` નો એક્સેપ્શન ઉત્પન્ન થઈ શકે છે. તેમાં, તમે ટ્રાય-કેચ બ્લોકનો ઉપયોગ કરીને આ એક્સેપ્શનને હેન્ડલ કરી શકો છો અને તમારો પ્રોગ્રામ બંદ ન થઈએ.

થ્રેડ સ્લીપ (Thread sleep) મેથડ એક થ્રેડને નિર્દિષ્ટ સમય સુધી ડાહ્યું રાખવા માટે ઉપયોગ થાય છે. જ્યારે તમે થ્રેડને સ્લીપ કરાવો છો, તે નિર્દિષ્ટ સમય માટે આપને પ્રતીક્ષા કરતું રહેશે.
થ્રેડને સ્લીપ કરાવવાનો ઉપયોગ કોઈ પ્રક્રિયાને બંધ કરવામાં આવે છે અથવા ડેલેય કરવામાં આવે છે, જે થ્રેડને પ્રતીક્ષામાં રાખે છે કે તેમની માટે તમારી પ્રોગ્રામમાં કંપ્યુટેશનલ્યુટેડ પ્રોસેસિંગને રોકે છે. આપેલ ઉદાહરણમાં, પ્રોગ્રામમાં સ્લીપ થઇ રહ્યું મોટાભાગનો કારણ આપનો પ્રોગ્રામ બધાની કાર્યક્રમનું પ્રતીક્ષા કરી રહ્યો છે જ્યારે થ્રેડ સ્લીપ થયો છે.

ટિપ્પણીઓ

આ બ્લૉગ પરની લોકપ્રિય પોસ્ટ્સ

Write an applet that display the position of the mouse at the upper left corner of the applet when it is dragged or moved. draw a 10x10 pixel rectangle filed with black at the current mouse position

Write an applet that uses the mouse listener, which overrides only two methods which are mousePressed and mouseReleased

Write an applet that draws a circle. The dimension of the applet should be 500 x 300 pixels. The circle should be centered in the applet and have a radius of 100 pixels. Display your name centered in a circle.( using drawOval() method ).