Thursday, July 3, 2014

APPLET IN JAVA



Applet in JAVA

An applet is a small Java program that is embedded and ran in some other Java interpreter program such as
  1. a Java technology-enabled browser
  2. Sun’s applet viewer program called appletviewer

Life Cycle of an Applet:
init: This method is intended for whatever initialization is needed for an applet.
start: This method is automatically called after init method. It is also called whenever user returns to the page containing the applet after visiting other pages.
stop: This method is automatically called whenever the user moves away from the page containing applets. This method can be used to stop an animation.
destroy: This method is only called when the browser shuts down normally.

Simple Java Applet Code

Hello.java
import java.applet.Applet;
import java.awt.*;
public class Hello extends Applet{
public void init() {
repaint();
}
public void paint(Graphics g) {
g.drawString(“Hello World!”,30,30);
}
}

Hello.html
<html>
<body>
<applet code=“Hello.class”
width=300height=300>
</applet>
</body>
</html>



Passing Parameters to applet
getParameter(“parameter name”) : takes parameters value.
<param> tag : set the parameter value
e.g
<param name=”msg” value=”Hello Applet”>
</param>

Sample Graphics methods
A Graphics is something you can paint on
g.drawString(“Hello”, 20, 20);      Hello
g.drawRect(x, y, width, height);   
g.fillRect(x, y, width, height);       
g.drawOval(x, y, width, height);
g.fillOval(x, y, width, height);
g.setColor(Color.red);

No comments:

Post a Comment

JAC Board Computer || IIT 2025 || Introductory Information Technology || Class 10 || Practical Examination 2025

Marks Division HOE 30 Marks IT Application Report File 20 Marks Viva - Voce 10 Marks 1) Write an HTML code to generate a web p...