As we can embed applet into web pages in two ways i.e. by writting our own applet and then embed into web pages. Or by downloading it from a remote computer system and then embed it into webpage.
An applet developed locally and stored in a local system is known as local applet. Therefore when webpage is trying to find local applet it doen not need the internet connection.
A remote applte is that which is developed by some one else and stored on a remote computer connected to the internet. If our system is connected to the internet then we can download it from remote computer and run it. In order to locate and load a remote applet, we must know the applet’s address on the web. This address is known as Uniform Resourse locator(URL) and must be specified in applet’s document.
Example 1 // Example to illustrate Applet Lifecycle
import java.awt.*; import java.applet.*; /* <applet code="AppletTest" width=200 height= 100> </applet> */ public class AppletTest extends Applet { public void init() { System.out.println("Applet Initialised..."); setBackground(Color.cyan); } public void start() { System.out.println("Applet Started...."); } public void stop() { System.out.println("Applet Stoppen...."); } public void destroy() { System.out.println("Applet Destryoed...."); } public void paint(Graphics g) { g.drawString("Applet Text",200,400); showStatus("This is shown in Status."); } }
Save the file as AppletTest. Java
Compile the file using javac AppletTest.java
On successful compilation, execute the file using appletviewer AppletTest.java
The output appers as shown in following figure :
Example 2 // Example to illustrate Applet Lifecycle
import java.awt.*; import java.applet.*; /* <applet code="Sample" width=200 height= 100> </applet> */ public class Sample extends Applet { String msg; public void init() { setBackground(Color.cyan); setForeground(Color.red); msg = "Inside init()-"; } public void start() { msg += "Inside start()-"; } public void paint(Graphics g) { msg +="Inside paint()-"; g.drawString(msg,10,30); showStatus("This is shown at status"); } }
Save the file as Sample. Java
Compile the file using javac Sample.java
On successful compilation, execute the file using appletviewer Sample.java
The output appers as shown in following figure :
Example 3 // Example for passing parameters
import java.awt.*; import java.applet.*; /* <applet code="ParamDemo" width=300 height= 80> <param name=fontName value=Courier> <param name=fontSize value=14> <param name=leading value = 2> <param name=accountEnabled value= true> </applet> */ public class ParamDemo extends Applet { String fontName; int fontSize; float leading; boolean active; public void start() { String param; fontName=getParameter("fontName"); if(fontName==null) fontName= "Not Found"; param=getParameter("fontSize"); try { if(param!=null) fontSize=Integer.parseInt(param); else fontSize=0; } catch(NumberFormatException e) { fontSize=-1; } param=getParameter("leading"); try { if(param!=null) leading=Float.valueOf(param).floatValue(); else leading=0; } catch(NumberFormatException e) { leading=0; } param=getParameter("accountEnabled"); if (param!=null) active =Boolean.valueOf(param).booleanValue(); } public void paint(Graphics g) { g.drawString("Font Name." + fontName,0,10); g.drawString("Font Size." + fontSize,0,26); g.drawString("Leading." + leading,0,42); g.drawString("Account Active." + active,0,58); } }
Save the file as ParamDemo. Java
Compile the file using javac ParamDemo.java
On successful compilation, execute the file using appletviewer ParamDemo.java
The output appers as shown in following figure :
Example 4 // Example for getDocumentBase() & getCodeBase()
import java.awt.*; import java.applet.*; import java.net.*; /* <applet code="Bases" width=300 height= 50> </applet> */ public class Bases extends Applet { public void paint(Graphics g) { String msg; URL url= getCodeBase(); msg= "Code Base:" +url.toString(); g.drawString(msg,10,20); url= getDocumentBase(); msg= "Document Base:" +url.toString(); g.drawString(msg,10,40); } }
Save the file as Bases. Java
Compile the file using javac Bases.java
On successful compilation, execute the file using appletviewer Bases.java
The output appers as shown in following figure :
All Rights Reserved. © 2024 BookOfNetwork