Tuesday 31 May 2022

Question: Explain the concept of life cycle of an applet in java.

 

an applet is a special type of program embedded in the web page to generate dynamic content. Applet is a class in Java.


एप्‍लेट एक स्‍पेशल टाइप का प्रोग्राम होता है जिसे वेब पेज के साथ डायनेमिट कंटेंट जनरेट करने के लिए उपयोग किया जाता है। एप्‍लेट जावा में एक क्‍लास है।


Methods of Applet Life Cycle

 

Stages in the Life Cycle of Java Applet

  1. ·     Initializing an Applet  एप्‍लेट के लिए मैमोरी में जगह बनाना

    ·         Starting the Applet      एप्‍लेट को चालू करना

    ·         Painting the Applet एप्‍लेट को पेन्‍ट करना

    ·         Stopping the Applet एप्‍लेट को बंद करना

    ·         Destroying the Applet एप्‍लेट को मैमोरी से मिटाना

 

Life Cycle of Applet

Step 1: Initialization

public void init()

There is no main method unlike our normal java programs. Every Applet will start it’s execution from init() method. It is executed only once

एप्‍लेट में सामान्‍य जावा प्रोग्रामों के विपरीत मैन मैथड नहीं होता है। प्रत्‍येक एप्‍लेट का एक्‍जीक्‍यूशन init() मेथड से ही प्रारम्भ होता है। इसे केवल एक बार ही किया जाता है।

  

Step 2: Start

public void start()

After init() method start() method is invoked. Executed when the browser is maximized

init() मेथड के बाद स्‍टार्ट मेथड का प्रयोग किया जा सकता है। इसे प्रयोग करने के लिए ब्राउजर फुल साइज में खुला होना चाहिए।

 

Step 3: Paint

public void paint (Graphics g)

Paint method is used to display the content on the applet. We can create the objects or components to the applet or we can directly write a message on the applet. It will take Graphics class as a parameter.

पेन्‍ट मै‍थड का प्रयोग एप्‍लेट के कंटेंट को दिखाने के लिए किया जाता है। अब हम कंटेंट को ऑबजेक्‍ट के माध्‍यम से या फिर डायरेक्‍ट मैसेज लिखकर भी दिखा सकते है। इसके लिए ग्राफिक्‍स क्‍लास को पेन्‍ट मैथड में पैरामीटर(रेफरेंस) के तौर पर प्रयोग करना होता है।

 

Step 4: Stop

public void stop()

stop() method is used to stop the applet. It is executed when the browser is minimized.

स्‍टॉप मैथड का प्रयोग एप्‍लेट को स्‍टॉप करने के लिए किया जाता है। इसे प्रयोग करने के लिए ब्राउजर की साइज को छोटी करना होता है।

 

Step 5: Destroy

public void destroy()

destroy() method is used to completely close the applet. It is executed when the applet is closed.

डेस्‍ट्राय मेथड का प्रयोग मेमोरी से एप्‍लेट को हटाने के लिए किया जाता है इसे तब प्रयोग किया जाता है जब एप्‍लेट को बंद(Close) कर दिया जाता है।

 

समझने के लिए

यह ठीक वैसा ही है जैसे हमें कोई लैटर टाइप करने लिए पहले वर्ड की फाइल बनानी होती है। फिर उसे खोलना होता है। फिर लैटर टाइप करके सेव करना होता है । फिर वर्ड फाइल को बंद किया जाता है। फिर अंत में हम काम पूरा होने के बाद वर्ड फाइल को डिलीट कर देते हैं।

 

Example 1: In order to begin with Java Applet, let’s understand a simple code to make the Applet 

// Java Program to Make An Applet

// Importing required classes from packages

import java.awt.*;

import java.awt.applet.*;

// Class 1

// Helper class extending Applet class

public class AppletDemo extends Applet

// Note: Every class used here is a derived class of applet,

// Hence we use extends keyword Every applet is public

{

    public void init()

    {

        setBackground(Color.black);

        setForeground(Color.yellow);

    }

    public void paint(Graphics g)

    {

        g.drawString("Welcome", 100, 100);

    }

}

// Save file as AppletDemo.java in local machine

 

<html>

  <applet code = AppletDemo

          width = 400

          height = 500>

    </applet>

  </html>

<!-- Save as Applet.html -->

 

 


No comments:

Post a Comment