This article explains how to write a small applet.Before we are going to write applets we must ensure the following are available in local system.
Requirements to build and run an applet
- Check Java is installed properly in your system.
- Java enabled in your browser.
Steps involved in creating and testing an applet
- write the applet program in your local system using any editor.
- Create an executable java class (In this case applet).
- Create a web page in HTML.
- Prepare the applet tags in HTML page.
- Note the URL of applet in local system.
- Embed the applet in HTML page using <APPLET>tags.
- Test the applet using Java Enabled Browser.
Creating and testing an applet
Now we are going to create and test a sample applet.
- Create a java applet code file using the following code (MyApplet.java)
package friends;
import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet
{
/**
*
*/
private static final long serialVersionUID = 1L;
public void paint(Graphics g)
{
g.drawString("Hello Friend I am your Applet ", 10, 20);
g.fillRect(10,25, 150, 160);
}
}
- Compile the program using following command.
C:/>Javac MyApplet.java
- Now your applet class is ready
- You can check your applet using appletviewer tool using the following commandC:/>appletviewer MyApplet
- The output will be as follows
If you get the above out put, your applet class is ready.
- Now , embed your applet into HTML page.Create an HTML page with following code.
<Html>
<Head>
<Title>MyApplet test</Title>
</Head>
<Body>
This is my page<br>
You can find applet<br>
<br>
<Applet Code="MyApplet.class" width=200 Height=100>
</Applet>
</Body>
</Html>
Make sure that applet class is in the same folder of HTML page.Open the HTML page using any browser.you will get the following output.
Happy surfing...............
0 comments:
Post a Comment