An applet is a Java program that runs in a Web browser.An applet is a java class that extends with java.applet.Applet class.A main() method is not invoked on an applet.In Applets the place of the main method init method is used because the main method is used as an entry point for applications, the init method is used as an entry point for applets.Two packages are importing to implement the applet these are:
WAP to implement simple applet
import java.awt.*; //creating user interfaces and for painting graphics and images.
import java.applet.*; //Provides the necessary classes to create an applet.
public class app extends Applet{ //Extending Applet class from applet package.
String msg;
public void init() //To initialize the applet
{
msg="This is my first applet";
}
//When the paint method is call AWT package uses a "callback" mechanism for painting.
public void paint(Graphics g)
{
g.drawString(msg,10,20); //It Returns the specified text at specified location.
}
}
Now save this program with name app.java and then compile it.Make sure that your program is error free.
Now open new notepad file and write the following code:
<html>
<body>
<applet code="app.class",width="200",height="200">
</applet>
</body>
</html>
Then save this file with name app.html in same folder where you have saved app.java file.
Output of this program:
- import java.awt.* :- AWT stands for abstract window toolkit.It is used for creating user interfaces and for painting graphics and images.
- import java.applet.*:- It Provides the necessary classes to create an applet.
WAP to implement simple applet
import java.awt.*; //creating user interfaces and for painting graphics and images.
import java.applet.*; //Provides the necessary classes to create an applet.
public class app extends Applet{ //Extending Applet class from applet package.
String msg;
public void init() //To initialize the applet
{
msg="This is my first applet";
}
//When the paint method is call AWT package uses a "callback" mechanism for painting.
public void paint(Graphics g)
{
g.drawString(msg,10,20); //It Returns the specified text at specified location.
}
}
Now save this program with name app.java and then compile it.Make sure that your program is error free.
Now open new notepad file and write the following code:
<html>
<body>
<applet code="app.class",width="200",height="200">
</applet>
</body>
</html>
Output of this program:
No comments:
Post a Comment