Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Facebook
Twitter
Pinterest
Youtube
Instagram
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Suryateja Pericherla Categories: Applets. 5 Comments on Applets Overview

In this article we will learn what are applets in Java, what are the differences between a Java program and an applet and how to create applets.

 

Contents

  • 1 Definition of Applet
  • 2 Applet Fundamentals
  • 3 Creating an Applet
  • 4 Running an Applet
  • 5 Differences between a Java Application and Applets

Definition of Applet

 

An applet is a small Java program that is a part of a web page and is downloaded automatically from a server when the client requests that web page.

 

Applet Fundamentals

 

Since applet is a part of a webpage, we will learn basic things related to web pages. A web page is a collection of information (ex: Google home page, Yahoo home page etc). Web pages are generally created using HTML.

 

A web page contains at least four basic HTML tags:

  1. <html> : Specifies that it is the root tag.
  2. <head> : Specifies the head section of the web page. It contains meta information, page title, scripts, external style sheet links etc.
  3. <body> : Contains the content that is displayed to a user who is visiting the web page.
  4. <title> : Specifies the title of the web page. It is specified inside <head> tags.

 

As an example let’s look at the HTML code of a web page that displays “Hello World” in large letters. The code is as follows:

 

hello.html

<html>
	<head>
		<title>My Webpage</title>
	</head>
	<body>
		<h1>Hello World</h1>
	</body>
</html>

 

The file will be saved with .html extension. When you open the file with a web browser, the output will be as follows:

 

hello-page

 

An applet can be displayed in a web page using the <applet> tag as shown below:

<applet code=”ClassName” height=”200″ width=”400″></applet>

 

An applet doesn’t contain a main() method like the Java console programs. An applet is used to display graphics or to display a GUI (ex: login form, registration form) to the users.

 

Creating an Applet

 

An applet can be created by extending either Applet class or JApplet class. First let’s see how to create an applet using the Applet class. The Applet class is available in java.applet package. An applet which displays “Hello World” is shown below:

 

MyApplet.java

import java.awt.*;
import java.applet.*;
public class MyApplet extends Applet 
{
	public void paint(Graphics g)
	{
		g.drawString("Hello World!", 20, 20);
	}
}
/*
	<applet code="MyApplet" height="300" width="500"></applet>
*/

 

Remember to include the <applet> tag in comments. This is useful for running the applet. The value of code attribute must match with the class name.

 

In the above applet program, the class MyApplet extends the Applet class and it contains a method named paint() which accepts a parameter of the type Graphics. The Graphics class belongs to java.awt package and is used to display text or graphics on our applet.

 

The paint() method is used to re-display the output when the applet is resized or minimized or maximized. The drawString() method belongs to Graphics class and it displays “Hello World!” on the applet display area at the point whose coordinates are (20, 20).

 

Save the file as MyApplet.java and compile it to generate MyApplet.class file.

 

Running an Applet

 

An applet can be executed in two ways:

  1. Using appletviewer command-line tool or
  2. Using a browser

 

Using appletviewer tool:

 

Java provides a command line tool named appletviewer for quick debugging of applets. Use the following syntax for running an applet using appletviewer:

 

appletviewer filename

 

As the file name of our applet is MyApplet.java, the command for executing the applet is:

 

appletviewer MyApplet.java

 

Output of the above command will be as shown below:

 

applets

 

Using a browser:

 

Create a web page named hello.html with the following HTML code:

<html>
	<head>
		<title>My Webpage</title>
	</head>
	<body>
		<applet code="MyApplet" height="200" width="400"></applet>
	</body>
</html>

 

Unfortunately from Java 7 onwards, local applets (unsigned) cannot be viewed using a browser and Google Chrome browser no longer supports applets in web pages.

 

It is recommended to use appletviewer tool to debug and execute applets.

 

Watch below video to learn how to execute Java applets using appletviewer command line tool:

 

Differences between a Java Application and Applets

 

application-vs-applet

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla
Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

5 Comments

You can follow any responses to this entry through the RSS 2.0 feed.

This is really useful, thanks.

Reply

Sir,how to execute this applet (where to run the command ,
appletviewer MyApplet.java).

Reply

    You have to run the command in command prompt (cmd) in Windows.

    sir, even if the comment section isn’t provided ,it shows the same result.
    I am using netbeans IDE , and I am running it in that IDE with the appletviewer.

    By command prompt it shows the following error

    ‘appletviewer’ is not recognized as an internal or external command,
    operable program or batch file.

    Please make a video on it and send it to me.
    [email protected]

    If command prompt says appletviewer is not recongnized, you have to add jdk bin directory to the path environment variable. I will make a video today night or tomorrow. Visit this page again later. I will update the video link here.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

代做工资流水公司南昌工作收入证明报价湘潭工资流水app截图打印江门做个人工资流水长沙房贷收入证明公司北京薪资流水单公司南昌办企业对私流水信阳入职工资流水德阳个人银行流水模板海口入职工资流水制作珠海贷款工资流水 开具成都贷款工资流水 多少钱济宁办理背调银行流水长沙购房银行流水查询惠州代开公司流水阜阳车贷工资流水 代办江门自存银行流水代开太原薪资流水多少钱青岛房贷工资流水 模板衡阳签证银行流水 图片上饶打工作收入证明常德工资流水账单代做广州背调银行流水报价漳州打印转账流水柳州办理流水单盐城代办消费贷流水宜昌办理薪资流水单荆州银行流水账模板中山贷款银行流水制作曲靖查银行流水西安流水香港通过《维护国家安全条例》两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”19岁小伙救下5人后溺亡 多方发声卫健委通报少年有偿捐血浆16次猝死汪小菲曝离婚始末何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言男子被猫抓伤后确诊“猫抓病”周杰伦一审败诉网易中国拥有亿元资产的家庭达13.3万户315晚会后胖东来又人满为患了高校汽车撞人致3死16伤 司机系学生张家界的山上“长”满了韩国人?张立群任西安交通大学校长手机成瘾是影响睡眠质量重要因素网友洛杉矶偶遇贾玲“重生之我在北大当嫡校长”单亲妈妈陷入热恋 14岁儿子报警倪萍分享减重40斤方法杨倩无缘巴黎奥运考生莫言也上北大硕士复试名单了许家印被限制高消费奥巴马现身唐宁街 黑色着装引猜测专访95后高颜值猪保姆男孩8年未见母亲被告知被遗忘七年后宇文玥被薅头发捞上岸郑州一火锅店爆改成麻辣烫店西双版纳热带植物园回应蜉蝣大爆发沉迷短剧的人就像掉进了杀猪盘当地回应沈阳致3死车祸车主疑毒驾开除党籍5年后 原水城县长再被查凯特王妃现身!外出购物视频曝光初中生遭15人围殴自卫刺伤3人判无罪事业单位女子向同事水杯投不明物质男子被流浪猫绊倒 投喂者赔24万外国人感慨凌晨的中国很安全路边卖淀粉肠阿姨主动出示声明书胖东来员工每周单休无小长假王树国卸任西安交大校长 师生送别小米汽车超级工厂正式揭幕黑马情侣提车了妈妈回应孩子在校撞护栏坠楼校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变老人退休金被冒领16年 金额超20万西藏招商引资投资者子女可当地高考特朗普无法缴纳4.54亿美元罚金浙江一高校内汽车冲撞行人 多人受伤

代做工资流水公司 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化