Monday, March 30, 2020

Applet in java

  • Applets:-
  • Applets are small Java applications It runs inside the web browser ..
  • Any applet in Java is a class that extends the java.applet.Applet class.
  • An Applet class does not have any main() method. It is viewed using JVM. The JVM can use either a plug-in of the Web browser or applet viewer to run applet .
  • JVM creates an instance of the applet class and invokes init() method to initialize an Applet.
  • The java.applet.Applet class include some common methods
  1. Get applet parameter<parms>
  2. Print a status message in the browser using "showstatus" method
  3. Fetch an image
  4. Fetch an audio clip
  5. Play an audio clip
  6. Resize the applet

  Java Applet Life cycle

Following are the stages in Applet

  1. Applet is initialized using init() method
  2. Applet is started using start()
  3. Applet is painted using paint()
  4. Applet is stopped using stop()
  5. Applet is destroyed using destroy()
  • init() : init() is the first method to be called. where variable are initialized. This method is called only once during the runtime of applet.
  • start() : start() method is called after init(). 
  • stop() : stop() method is called to suspend thread that does not need to run when applet is not visible.
  • destroy() : destroy() method is called when your applet needs to be removed completely from memory or when user close web browser 
Example:-

import java.applet.*;
import java.awt.*;
public class App1 extends Applet
{
public void start()
{
System.out.println("Applet started");
}
public void stop()
{
System.out.println("Applet stopped");
}
public void init()
{
System.out.println("Welcome");
}
public void paint(Graphics g)
{
setBackground(Color.red);
g.setColor(Color.green);
g.drawString("Hello",20,20);
}}
// <applet code= "App1.class" height=200 width=200></applet>


Example 2:-
import java.awt.*;
import java.applet.*;
public class Tes2 extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawArc(50,50,100,60,50,80);
g.fillArc(200,200,100,60,80,50);
}}
// <applet code= "Tes2.class"height=200 width=200></applet>



Example 3:-

import java.awt.*;
import java.applet.*;
public class app extends Applet

{

public void paint(Graphics g)
{

Color c1=new Color(255,200,100);
Font f1=new Font("Serif",Font.BOLD,42);
g.setFont(f1);
g.setColor(c1);
g.drawString("hello",30,30);
g.setColor(c1);
g.drawLine(10,30,44,32);
g.drawRect(55,55,55,55);

g.drawRoundRect(50,50,50,50,50,50);
g.drawArc(80,100,200,400,85,96);
}

}
// <applet code= "app.class"height=200 width=200></applet>

.

 Images in Applet


java.awt.Image class is used for representing an image.
java.applet, java.awt and java.awt.image are the packages which are used for event handling.

Loading an image

In Applet, images are loaded using getImage() method. It is always  call the constructor in init() method.
 examples:
Image image2 = getImage(getDocumentBase(), "pic1.jpeg"); 

Displaying an image

In Applet, images are displayed using drawImage() method. This method is supplied by the Graphics object, which is passed to paint() method.

Param Tag

 The <param> tag contains two attributes: name and value which are used to specify the name of the parameter and the value of the parameter respectively. 
For example
<param name=”class” value=”msc” />
<param name=”seats” value=”40″ />
Now, these two parameters can be accessed in the applet program using the getParameter() method of the Applet class.
 getParameter() Method
 The getParameter() method of the Applet class can be used to retrieve the parameters passed from the HTML page. The syntax of getParameter() method is as follows:
Example:
import java.awt.*;
import java.applet.*;
public class App1 extends Applet 
{
String x;
String a;
public void init()
{
x = getParameter("class");
a = getParameter("seats");
}
public void paint(Graphics g)
{
g.drawString("class is: " + x, 20, 20);
g.drawString("seats is: " + a, 20, 40);
}
}
/*
<applet code="App1" height="300" width="500">
<param name="class" value="msc" />
<param name="seats" value="40" />
</applet>
*/

No comments:

Post a Comment