Header Ads

Applet

 



Q.1 What is Applet? Give its advantages over applications.
Ans.:

1) Java programs are classified into two groups namely applications and applets.
2) An applet is a Java program that executes inside a web page. An applet is loaded and executed in Java enabled browser.
3) It can access the resources of the host machine.
4) In another way, applets are small applications that are accessed on an internet Server, transported over the Internet, automatically installed, and run as a part of a web document.
5) After an Applet arrives on the client, it has limited access to resources, so that it can produce an arbitrary multimedia user interface and run complete computations without introducing the risk of viruses or breaching data integrity.

ADVANTAGES OF APPLETS

1) Applets are portable and supported by most web browsers.
2) Applets are cross platform and can run on Windows, Mac OS and Linux platform.
3) Applets can work all the version of Java Plug-in.
4) An applet runs on sandbox, so the user does not need to trust the code, so it can work without security approval.

DISADVANTAGES OF APPLETS

1) Java plug-in is required to run applet. 
2) Java applet requires JVM, so first time it takes significant startup time.

Q. Explain the difference between Java Application and Java Applet.

difference between Java Application and Java Applet

Q.2 Give description on Applet class.
Ans.:

1) The Applet class is contained in the java.applet package. It provides all necessary support for applet execution, such as starting and stopping.
2) It also provides method that load and display images and methods that loads audio clips.
3) Applet class extends the AWT class panel and it extends container, which extends component. This class provides support for Java's windows based, graphical interface. 
Thus, Applet provides all of the necessary support for window based activities.

Q. 3 Explain Applet Architecture.
Ans.:

1)  Since an applet is windows-based program, that's why applet are event driven. An Applet waits until an event occurs. The AWT notifies the applet about an event by calling an event handler that has been provided by the applet.
2)  Once this happens, Applet must take appropriate action and then quickly return control to the AWT.
3)  For example, when the user clicks a mouse inside the applet's window, a mouse click event is generated. If the user presses a key while the applet's window has input focus, a key press event is generated.
4)  Applet can contain various controls, such as push buttons and check boxes.
5)  When the user interact with these controls, an event is generated.

Q. 4 Describe life cycle of an applet.
Ans.:

life cycle of Applet
An applet can react to major event such as

1) It can initialize itself.
2) It can start running.
3) It can stop running.
4) It can perform a final cleanup, in preparation for being unloaded.

Consider these four major events, we say that applet goes though 4 states :

1) Born
2) Running
3) Idle
4) Destroyed

BORN STATE (APPLET IS LOADED)

init ( ) Method

The init ( ) method is called when once the first time an applet is loaded into the memory of a computer, we can initialize variables and add components like buttons and check boxes in init ( ) method.

start ( ) Method

The start method is called immediately after the init ( ) method and every time the applet receives focus as a result scrolling in the active winds. we can use this method when we want to restart a process, such as thread animation, every time the applet receives the focus.

stop ( ) Method

The stop ( ) method is called every time the applet loses focus. We can use this method to reset variable and stop the thread that are running.

destroy ( ) Method

The above method is called by the browser when the user move to another page. We can use this method to perform clean-up operations likes closing a life.

If it is not necessary to use any or all the method of Applet. these methods are automatically called by Java environment. None of the above methods accept parameters.

Q. 5 How does initialization and termination of applet occurs?
Ans.:

When an applet begins, the AWT calls the following methods, in the sequence.
1.        init ( )                          2.  start ( )                                   3. paint ( )

When an applet is terminated, the following sequence of method call take place.
1.        stop ( )                                     2.  destroy ( )

NOTE: Students must describe above two methods in detail. (Refer Q. 4)

Following are the steps required for preparing the applets-

1. Write an applet code in a file and save it using the filename extension .java.
2. Compile and create an executable file. This file also have the extension .class.
3. Design a web page using the <applet> code inside the web page.
4. Embed the <applet> code inside the web page.
5. Develop the HTML file.
6. Execute and test the applet program.

Q. 6 What is use of update ( ), paint ( ) and repaint ( ) methods?
Ans.:

When we scroll to an applet, the screen has to be refreshed to show the new contentWindows handles this by making the area (rectangle) that has to be refreshed. The area is then painted to  display  the  result of scrolling. This is handled by update ( ) and paint ( ) methods.

The update ( ) Method :  It takes a  Graphic class  object as a  parameter. When a applet area needs to be redrawn, the windows system starts the painting process. The update ( ) method is called to clear the screen and calls the paint  ( ) method.  The screen is  then  refreshed by  the system.

The paint ( ) Method : It draws the graphics of  the  applet  in  drawing  area. The method is automatically called the first time the applet is displayed on the screen and and every time the applet receives the focus. The paint ( ) can be triggered by invoking the repaint( ) method.

The repaint ( ) Method : We call the repaint ( ) method when  we  want  the applet area to be redrawn. The repaint ( ) methods calls the update ( ) method to signal that the applet has to be updated. The default action of update ( ) method is clear the applet area and call the paint ( ) method.

Q. 7 WAP to draw an Applet with the message "Life without LOVE is not a life". 
Ans.:

Code

import java.applet.*; 
import java.awt.*; 
/*<applet code = "Message Applet" width= 500 height= 500></applet> */ 
public class MyApplet extends Applet 
{ 
   public void paint (Graphics g) 
   { 
      g.drwaString("Life without LOVE is not a life", 10, 20);
   }
}
Q. 8 State some simple Applet display Methods.
Ans.:

1) void drawstring (message, int x, int y);
To output a string to an applet. Message is the string to be output beginning at x, y.

2) void setBackground (Color newcolor)
To set the background color of an applet's window.

3) void setForeground (color newcolor)
To set the foreground color. (i.e. color in which text is shown)

Q. 8 Write a program to show the applet skeleton.
Ans

Code

import java.applet.*; 
import java.awt.*; 
/*<applet code = AppletSkel.class" width = 400 height=400> </applet>*/ 
public class AppletSkel extends Applet 
{ 
  public void init () // called first 
  {            
     // initialization 
  }
  public void start () // called second 
  { 
     // start or resume execution 
  } 
  public void stop () 
  { 
   // suspends execution 
  } 
  public void destroy () 
  { 
    // perform shutdown activities 
  } 
  public void paint (Graphics g) 
  { 
    //redisplay content of windows 
  } 
}
Q. 9 What is the use of HTML APPLET tag? Give the element (attributes) of it.
Ans.: 

The APPLET tag is used to embed (insert) an applet in HTML document. The APPLET tag takes zero or more parameters

Syntax :

<APPLET CODE = "name of the .class file that extends java.applet.Applet" CODE BASE = "path  of the class file"
HEIGHT = maximum height of applet in pixel
WIDTH = maximum width of applet in pixel
HSPACE= horizontal space between the applet and the rest of HTML in pixel
SPACE = Vertical space between the rest of HTML in pixel
ALIGN= alignment of applet with respect to rest of page
ALT="alternate text to be displayed if browser do not support applets">
<PARAM NAME="Parameter-name" VALUE="Value-of-parameter">
<PARAM NAME = "Parameter name" VALUE="Value-of-parameter">
</APPLET>

Ex. <APPLET CODE = "AppletDemo.class" HEIGHT = 450 WIDTH = 500> </APPLET>

Q.10 How will you pass parameters to an applet ? Give a suitable example.
Ans.:

The Applet tag in HTML allows us to pass parameters to our applet. To retrieve parameterwe use getParameter () method. It returns the value of specified parameters in the form of string object.

Thus for numeric and Boolean values, we need to convert their string representations into their internal formats.

Example:

Code

import java.awt.*; 
import java.applet.*; 
public class ParamTestApplet extends Applet 
{  
  Font f= new Font("TimesRoman", Font.BOLD, 40); 
  String name; 
  public void init() 
  { 
    name = getParameters ("name"); 
    if(name = =null) 
    { 
      name = "friend"; 
    } 
    name="Have nice day" + name; 
 } //end of init() 
 public void paint (Graphics g) 
  { 
   g.setFont (f); 
   g.setColor (Color.darkGray); 
   g.drawString (name, 50, 50); 
  }
} //end of the class. 
In above program, getParameter () method, which accepts the name (a string) as its parameters. If no value is given to the variable "name" then default is given by stat.name = "friend"

<applet code = "ParameterTestApplet.class" WIDTH = 400 HEIGHT = 400>
<ALIGN= TOP>
<PARAM NAME= name VALUE = "PRIYA">
</applet>

The ParamTestApplet.html file must be included along with ParamTestApplet.html file must be included along with ParamTestApplet.java in the same directory. If HTML file is present in other directory then codebase attribute is used to specify the path.

Q. 11 Give details of getDocumentBase( ), showDocument ( ) and getCodeBase ( ) methods of applet.
Ans.:

In many cases of applet, we need to explicitly load media and text.java will allow the applet to load data from the directory holding HTML file that started the applet (the document base) and the directory from which the applet's .class file was loaded (the base code).

These directories are returned as URL object by getDocumentBase () and getCodeBase ( ). They can be concatenated with a string that names the file you want to load.

To actually load another file, we will use showDocument () method defined in Applet Context interface. Once we obtained document into view by calling showDocument ( ). This method do not return any value or throws exception

No comments

Powered by Blogger.