Wednesday 18 February 2015

How to run an Applet?

How to run an Applet?

There are two ways to run an applet
  1. By html file.
  2. By appletViewer tool (for testing purpose).

Simple example of Applet by html file:

To execute the applet by html file, create an applet and compile it. After that create an html file and place the applet code in html file. Now click the html file.
//First.java  
import java.applet.Applet;  
import java.awt.Graphics;  
public class First extends Applet{  
  
public void paint(Graphics g){  
g.drawString("welcome",150,150);  
}  
  
}  

myapplet.html

<html>  
<body>  
<applet code="First.class" width="300" height="300">  
</applet>  
</body>  
</html> 

Simple example of Applet by appletviewer tool:

To execute the applet by appletviewer tool, create an applet that contains applet tag in comment and compile it. After that run it by: appletviewer First.java. Now Html file is not required but it is for testing purpose only.

//First.java  
import java.applet.Applet;  
import java.awt.Graphics;  
public class First extends Applet{  
  
public void paint(Graphics g){  
g.drawString("welcome to applet",150,150);  
}  
  
}  
/* 
<applet code="First.class" width="300" height="300"> 
</applet> 
*/  
To execute the applet by appletviewer tool, write in command prompt:

c:\>javac First.java
c:\>appletviewer First.java

No comments:

Post a Comment