Four methods in the Applet class give you the framework on which you build any serious applet:
Every applet is an extension of the java.applet.Applet class. The base Applet class provides methods that a derived Applet class may call to obtain information and services from the browser context.
These include methods that do the following:
Four methods in the Applet class give you the framework on which you build any serious applet:
Every applet is an extension of the java.applet.Applet class. The base Applet class provides methods that a derived Applet class may call to obtain information and services from the browser context.
These include methods that do the following:
Additionally, the Applet class provides an interface by which the viewer or browser obtains information about the applet and controls the applet's execution. The viewer may:
Let's see an applet that will display "Dhanpat" in the applet frame. For this we use the paint() method of Component class of java.awt package. It is noted that all the methods of applet and applet class itself should be declared as public, otherwise they are not available to the browser to execute.
// the following program creates an applet having white background color and a message "SILAN SOFTWARE".
import java.awt.*; import java.applet.*; public class AppletDemo extends Applet { public void init() { setBackground(Color.white); } public void paint(Graphics g) { g.drawString("SILAN SOFTWARE",100,100); } }
Output
D:\app>javac AppletDemo.java
Now AppletDemo.class is created which is called as byte code. Now we will embedded this byte code into a HTML page usingtag which is as follows :
<html> <applet code="AppletDemo.class" height=250 width=350> </applet> </html>
Now we will save the above HTML code in Silan.HTML file. This HTML page contains the applet which can be opened in the web browser or by giving the command at system prompt as :
D:\app>appletviewer Silan.html
It is possible to pass user-defined parameters to an applet using <PARAM> tags. Each <PARAM> tag has a name attributes such as color, and a value attribute such as red. Inside the applet code, the applet can refer to that parameter by name to find it's value. For example, we can change the color of the text displayed to yellow by an applet by using a <PARAM> tag as follows :
<applet> <PARAM=color VALUE="yellow"> </applet>
Similarly we can change the text to be displayed by an applet by supplying new text to the applet through a <PARAM> tag as follows :
<PARAM NAME=text VALUE="Silan Software" >
To setup and handle parameters, we have to do two things :
Parameters are passes on an applet when it is loaded. We can define the init() method in the applet to get hold of the parameters defined in the tags.
This is done using getParameter() method which takes one string argument representing the name of the parameter and returns a string containing the value of that parameter.
Let's see a program to understand the concept of passing parameters to applets:
import java.awt.*; import java.applet.*; public class Himalaya extends Applet { String msg; public void init() { msg=getParameter("string"); if(msg==null) msg="java"; msg="welcome to"+msg; } public void paint(Graphics g) { g.drawString(msg,10,100); } }
Output:
C:\>javac Himalaya.java
Now Himalaya.class is created. Then this byte code is embedded into a HTML page which is as follows:
<html> <head> <title>welcome 2 Himalaya Publication</title> <body> <applet code=Himalaya.class height=200 width=300> <PARAM NAME="string" VALUE="Trilochan"> </applet> </body> </html>
Output:
C:\> appletviewer app1.html
import java.awt.*; import java.applet.*; public class Numeric extends Applet { public void paint(Graphics g) { int x=15; int y=25; int z=x+y; String s="sum of x and y:"+String.valueOf(z); g.drawString(s,150,250); } }
Now we compile this code as :
C:\>javac Numeric.java
Now Numeric.class created. Then this byte code will embed into a HTML page as:
<html> <applet code=Numeric.class height=200 width=200> </applet> </html>
Output:
Appletviewer app2.html
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc