Java Chap3
Java Chap3
• APPLET
• TYPES OF APPLET
• DIFFERENCE BETWEEN JSAP AND
APPLET
• APPLET LIFE CYCLE
• CLASSES: GRAPHICS, LABEL,
TEXTFIELD, BUTTON, SCROLLBAR,
CHECKBOX, AND THEIR EVENTS
• EVENT HANDLERS
• HTML TAGS: <HTML>, <HEAD>,
<TITLE> , <BODY>, <P>, <BR> ,
<HR>, <U>, <B>,<I>, <A>, <FONT>,
<IMG>,
• <INPUT TYPE>: TEXT,RADIO,
PASSWORD, CHECKBOX, SUBMIT,
RESET,EXAMPLES USING TAGS.
[Document subtitle]
Admin
Divya.D.Parande
UNIT-3 APPLETS
Java Applet
Applet is a special type of program that is embedded in the webpage to generate the
dynamic content. It runs inside the browser and works at client side.
Advantage of Applet: There are many advantages of applet. They are as follows:
Hierarchy of Applet
As displayed in the above diagram, Applet class extends Panel. Panel class extends
Container which is the subclass of Component.
1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
1
Divya.D.Parande
UNIT-3 APPLETS
java.applet.Applet class
For creating any applet java.applet.Applet class must be inherited. It provides 4 life
cycle methods of applet.00:00/06:36
1. public void init(): is used to initialized the Applet. It is invoked only once.
2. public void start(): is invoked after the init() method or browser is maximized.
It is used to start the Applet.
3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop
or browser is minimized.
4. public void destroy(): is used to destroy the Applet. It is invoked only once.
java.awt.Component class
The Component class provides 1 life cycle method of applet.
1. public void paint(Graphics g): is used to paint the Applet. It provides Graphics
class object that can be used for drawing oval, rectangle, arc etc.
1. By html file.
2. By appletViewer tool (for testing purpose).
2
Divya.D.Parande
UNIT-3 APPLETS
/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);
}
}
Note: class must be public because its object is created by Java Plugin software that
resides on the browser.
myapplet.html
<html>
<body>
<applet code="First.class" width="300" height="300">
</applet>
</body>
</html>
//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
3
Divya.D.Parande
UNIT-3 APPLETS
1. Local Applet
2. Remote Applet
Local Applet
Local Applet is written on our own, and then we will embed it into web pages. Local
Applet is developed locally and stored in the local system. A web page doesn't need
the get the information from the internet when it finds the local Applet in the system.
It is specified or defined by the file name or pathname. There are two attributes used
in defining an applet, i.e., the codebase that specifies the path name and code that
defined the name of the file that contains Applet's code.
1. <applet
2. codebase = "tictactoe"
3. code = "FaceApplet.class"
4. width = 120
5. height = 120>
6. </applet>
Let's take an example of Local applet to understand how we can create it and
embedded it into web page.
4
Divya.D.Parande
UNIT-3 APPLETS
FaceApplet.java
Remote Applet
A remote applet is designed and developed by another developer. It is located or
available on a remote computer that is connected to the internet. In order to run the
applet stored in the remote computer, our system is connected to the internet then
we can download run it. In order to locate and load a remote applet, we must know
the applet's address on the web that is referred to as Uniform Recourse Locator(URL).
5
Divya.D.Parande
UNIT-3 APPLETS
1. <applet
2. codebase = "http://www.myconnect.com/applets/"
3. code = "FaceApplet.class"
4. width = 120
5. height =120>
6. </applet>
6
Divya.D.Parande
UNIT-3 APPLETS
Difference between a Java Application and a Java Applet:
Java Application is just like a Java program that runs on an underlying operating
system with the support of a virtual machine. It is also known as an application
program. The graphical user interface is not necessary to execute the java
applications, it can be run with or without it.
Java Applet is an applet is a Java program that can be embedded into a web page. It
runs inside the web browser and works on the client-side. An applet is embedded in
an HTML page using the APPLET or OBJECT tag and hosted on a web server.
Applets are used to make the website more dynamic and entertaining.
The difference between Application and Applet:
7
Divya.D.Parande
UNIT-3 APPLETS
There are five methods of an applet life cycle, and they are:
o init(): The init() method is the first method to run that initializes the applet. It
can be invoked only once at the time of initialization. The web browser creates
the initialized objects, i.e., the web browser (after checking the security settings)
runs the init() method within the applet.
o start(): The start() method contains the actual code of the applet and starts the
applet. It is invoked immediately after the init() method is invoked. Every time
the browser is loaded or refreshed, the start() method is invoked. It is also
invoked whenever the applet is maximized, restored, or moving from one tab
to another in the browser. It is in an inactive state until the init() method is
invoked.
o stop(): The stop() method stops the execution of the applet. The stop () method
is invoked whenever the applet is stopped, minimized, or moving from one tab
to another in the browser, the stop() method is invoked. When we go back to
that page, the start() method is invoked again.
8
Divya.D.Parande
UNIT-3 APPLETS
o destroy(): The destroy() method destroys the applet after its work is done. It is
invoked when the applet window is closed or when the tab containing the
webpage is closed. It removes the applet object from memory and is executed
only once. We cannot start the applet once it is destroyed.
o paint(): The paint() method belongs to the Graphics class in Java. It is used to
draw shapes like circle, square, trapezium, etc., in the applet. It is executed after
the start() method and when the browser or applet windows are resized.
1. init()
2. start()
3. paint()
1. stop()
2. destroy()
9
Divya.D.Parande
UNIT-3 APPLETS
10
Divya.D.Parande
UNIT-3 APPLETS
11
Divya.D.Parande
UNIT-3 APPLETS
1. import java.applet.Applet;
2. import java.awt.*;
3.
4. public class GraphicsDemo extends Applet{
5.
6. public void paint(Graphics g){
7. g.setColor(Color.red);
8. g.drawString("Welcome",50, 50);
9. g.drawLine(20,30,20,300);
10. g.drawRect(70,100,30,30);
11. g.fillRect(170,100,30,30);
12. g.drawOval(70,200,30,30);
13.
14. g.setColor(Color.pink);
15. g.fillOval(170,200,30,30);
16. g.drawArc(90,150,30,30,30,270);
17. g.fillArc(270,150,30,30,0,180);
18.
19. }
20. }
myapplet.html
1. <html>
2. <body>
3. <applet code="GraphicsDemo.class" width="300" height="300">
4. </applet>
5. </body>
6. </html>
12
Divya.D.Parande
UNIT-3 APPLETS
13
Divya.D.Parande
UNIT-3 APPLETS
14
Divya.D.Parande
UNIT-3 APPLETS
Checkbox checkbox2 = new Checkbox("Java", true);
f.add(checkbox1);
f.add(checkbox2);
// set the location of label
l1.setBounds(50, 100, 100, 30);
l2.setBounds(50, 150, 100, 30);
// adding labels to the frame
f.add(l1);
f.add(l2);
// set the position for the button in frame
b.setBounds(50,100,80,30);
// add button to the frame
f.add(b);
t1 = new TextField("Welcome to Javatpoint.");
t1.setBounds(50, 100, 200, 30);
f.add(t1);
// setting size, layout and visibility of frame
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
15
Divya.D.Parande
UNIT-3 APPLETS
The Key event is triggered when the character is
KeyEvent KeyListener
entered using the keyboard.
An event that indicates whether an item was
ItemEvent ItemListener
selected or not.
when the value of a textarea or text field is
TextEvent TextListener
changed
MouseWheelEvent generated when the mouse wheel is rotated MouseWheelListener
o Button
o public void addActionListener(ActionListener a){}
o MenuItem
o public void addActionListener(ActionListener a){}
o TextField
o public void addActionListener(ActionListener a){}
o public void addTextListener(TextListener a){}
o TextArea
o public void addTextListener(TextListener a){}
16
Divya.D.Parande
UNIT-3 APPLETS
o Checkbox
o public void addItemListener(ItemListener a){}
o Choice
o public void addItemListener(ItemListener a){}
o List
o public void addActionListener(ActionListener a){}
o public void addItemListener(ItemListener a){}
1. Within class
2. Other class
3. Anonymous class
1. import java.awt.*;
2. import java.awt.event.*;
3. class AEvent extends Frame implements ActionListener{
4. TextField tf;
5. AEvent(){
6.
7. //create components
8. tf=new TextField();
9. tf.setBounds(60,50,170,20);
10. Button b=new Button("click me");
11. b.setBounds(100,120,80,30);
12.
13. //register listener
14. b.addActionListener(this);//passing current instance
15.
16. //add components and set size, layout and visibility
17. add(b);add(tf);
17
Divya.D.Parande
UNIT-3 APPLETS
18. setSize(300,300);
19. setLayout(null);
20. setVisible(true);
21. }
22. public void actionPerformed(ActionEvent e){
23. tf.setText("Welcome");
24. }
25. public static void main(String args[]){
26. new AEvent();
27. }
28. }
public void setBounds(int xaxis, int yaxis, int width, int height); have been used
in the above example that sets the position of the component it may be button,
textfield etc.
18
Divya.D.Parande
UNIT-3 APPLETS
HTML
HTML is an acronym which stands for Hyper Text Markup Language which is used for
creating
web pages and web applications. Let's see what is meant by Hypertext Markup
Language, and Web page.
Hyper Text: HyperText simply means "Text within Text." A text has a link within it, is a
hypertext.
Whenever you click on a link which brings you to a new webpage, you have clicked on
a hypertext.
HyperText is a way to link two or more web pages (HTML documents) with each other.
Markup language: A markup language is a computer language that is used to apply
layout and
formatting conventions to a text document. Markup language makes text more
interactive and dynamic.
It can turn text into images, tables, links, etc.
Web Page: A web page is a document which is commonly written in HTML and
translated by a web browser.
A web page can be identified by entering an URL. A Web page can be of the static or
dynamic type.
With the help of HTML only, we can create static web pages.
58.1MHence, HTML is a markup language which is used for creating attractive web
pages with the help of styling,
and which looks in a nice format on a web browser. An HTML document is made of
many HTML tags and
each HTML tag contains different content.
Let's see a simple example of HTML.
<!DOCTYPE>
<html>
<head>
<title>Web page title</title>
</head>
<body>
<h1>Write Your First Heading</h1>
<p>Write Your First Paragraph.</p>
</body>
</html>
<html > :This tag informs the browser that it is an HTML document. Text between html
tag describes the web document. It is a container for all other elements of HTML except
<!DOCTYPE>
<head>: It should be the first element inside the <html> element, which contains the
metadata
(information about the document). It must be closed before the body tag opens.
19
Divya.D.Parande
UNIT-3 APPLETS
<title>: As its name suggested, it is used to add title of that HTML page which appears
at the top of the browser window. It must be placed inside the head tag and should
close immediately. (Optional)
<body> : Text between body tag describes the body content of the page that is visible
to the end user.
This tag contains the main content of the HTML document.
<h1> : Text between <h1> tag describes the first level heading of the webpage.
<p> : Text between <p> tag describes the paragraph of the webpage.
Features of HTML
1) It is a very easy and simple language. It can be easily understood and modified.
2) It is very easy to make an effective presentation with HTML because it has a lot of
formatting tags.
3) It is a markup language, so it provides a flexible way to design web pages along
with the text.
4) It facilitates programmers to add a link on the web pages (by html anchor tag), so it
enhances the
interest of browsing of the user.
5) It is platform-independent because it can be displayed on any platform like
Windows, Linux,
and Macintosh, etc.
6) It facilitates the programmer to add Graphics, Videos, and Sound to the web pages
which makes
it more attractive and interactive.
7) HTML is a case-insensitive language, which means we can use tags either in lower-
case or upper-case.
NOTE: It is recommended to write all tags in lower-case for consistency,
readability, etc.
HTML Anchor
The HTML anchor tag defines a hyperlink that links one page to another page. It can
create hyperlink to other web page as well as files, location, or any URL. The "href"
attribute is the most important attribute of the HTML a tag. and which links to destination
page or URL.
20
Divya.D.Parande
UNIT-3 APPLETS
The use of Java applet is also deprecated, and most browsers do not support the use
of plugins.
Syntax
Example:
<applet code="Shapes.class" align="right" height="200" width="300">
</applet>
HTML Image
HTML img tag is used to display image on the web page. HTML img tag is an empty
tag that contains attributes only, closing tags are not used in HTML image element.
21
Divya.D.Parande
UNIT-3 APPLETS
You have learnt about how to insert an image in your web page, now if we want to
give some height and width to display image according to our requirement, then we
can set it with height and width attributes of image.
Example:
<img src="animal.jpg" height="180" width="300" alt="animal image">
Points to remember: 1) Use the input element within the form element to declare input
control that allow user to enter data.
2) The input element is empty. It contains attributes only. There is no need of an end tag in
HTML.
3) If you want to define labels for input element, use the label element with each input tag.
22
Divya.D.Parande
UNIT-3 APPLETS
button Defines a simple push button, which can be programmed to perform a task on an
event.
23
Divya.D.Parande
UNIT-3 APPLETS
datetime- Defines an input field for entering a date without time zone.
local
month Defines a control with month and year, without time zone.
week Defines a field to enter the date with week-year, without time zone.
search Defines a single line text field for entering a search string.
24
Divya.D.Parande