SlideShare a Scribd company logo
PROGRAMMING IN JAVA
A. SIVASANKARI
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER APPLICATION
SHANMUGA INDUSTRIES ARTS AND SCIENCE
COLLEGE,
TIRUVANNAMALAI. 606601.
Email: sivasankaridkm@gmail.com
PROGRAMMING IN JAVA
UNIT – 4
PART I
 APPLET
 APPLETS - GUI
COMPONENTS
 APPLET PARAMETERS
 LIFE CYCLE OF AN APPLET
 APPLICATION
CONVERSION TO APPLETS
 AWT AND AWT HIERARCHY
 SWING COMPONENTS
A. SIVASANKARI - SIASC-TVM UNIT-4
PROGRAMMING IN JAVA
APPLET BASICS
• An applet is a Java program that runs in a Web browser. An applet can be a fully functional
Java application because it has the entire Java API at its disposal.
• There are some important differences between an applet and a standalone Java application,
including the following
• An applet is a Java class that extends the java.applet.Applet class.
• A main() method is not invoked on an applet, and an applet class will not define main().
• Applets are designed to be embedded within an HTML page.
• When a user views an HTML page that contains an applet, the code for the applet is
downloaded to the user's machine.
• A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or
a separate runtime environment.
• The JVM on the user's machine creates an instance of the applet class and invokes various
methods during the applet's lifetime.
• Applets have strict security rules that are enforced by the Web browser. The security of an
applet is often referred to as sandbox security, comparing the applet to a child playing in a
sandbox with various rules that must be followed.
• Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
APPLETS (GUI COMPONENTS)
• Applets are small java programs that run inside of a web browser.
• It is also stored as part of webpage.
• The applet classes contained in the java applet package.
• It also defines three interfaces.
1. Applet context
2. Audio clip
3. Applet stub
• It will be executed within HTML tag.
• It is an intelligent program.
• It is an application designed to be transmitted over internet and executed by a java compatible
with browser.
SYNTAX
• <applet code= “ program name” width=200 height=200> </applet>
APPLET INITIALIZATION AND TERMINATION
When started
• Init( )
• Start( )
• Paint( )
When Applet is terminated
• Stop( )
• Destroy( )
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
Another method is overriding update( )
EXAMPLE
• Public void paint (Graphics g)
• {
• Update(g);
• }
APPLET CLASSES OR METHODS
APPLET METHODS DESCRIPTION
void destroy( ) It called by the browser just before an applet is
terminated.
String getAppletinfo( ) It should return String.
URL getcodeBase( ) It returns URL
image getimage(URL url) It returns image object.
void init( ) It begins execution.
void start( ) It should starts of execution.
Void stop( ) Browser to suspend execution of applets.
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
EXAMPLE
• import java.awt.*;
• import java.applet.*;
• /*<applet code=”sample” width=300 height=200></applet>
• public class sample extends Applet{
• Public void init( ){
• setBackground(color.cyan)}
• Public void paint( Graphics g){
• g.drawString(“This is sample”,10,20);
• show status(“This is status window”);}}
HTMLAPPLET TAGS
• <APPLET[CODE BASE=code base URL] name of file CODE=applet file
• [ALT= alternate text] [NAME= applet instance name]name of attributes
• [WIDTH=pixels, HEIGHT=PIXELS]height and width of window
• [ALIGN= alignment]top, bottom, up, down, middle
• [VSPACE=Pixels]vertical space [HSPACE=Pixels]>horizontal space
• [<param name=attribute name VALUE=attribute value>]
• </APPLET>
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
LIFE CYCLE OF AN APPLET
• Four methods in the Applet class gives you the framework on which you build any
serious applet
• init − This method is intended for whatever initialization is needed for your applet.
It is called after the param tags inside the applet tag have been processed.
• start − This method is automatically called after the browser calls the init method.
It is also called whenever the user returns to the page containing the applet after
having gone off to other pages.
• stop − This method is automatically called when the user moves off the page on
which the applet sits. It can, therefore, be called repeatedly in the same applet.
• destroy − This method is only called when the browser shuts down normally.
Because applets are meant to live on an HTML page, you should not normally leave
resources behind after a user leaves the page that contains the applet.
• paint − Invoked immediately after the start() method, and also any time the applet
needs to repaint itself in the browser. The paint() method is actually inherited from
the java.awt.
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
A "Hello, World" Applet
Following is a simple applet named HelloWorldApplet.java
• import java.applet.*;
• import java.awt.*;
• public class HelloWorldApplet extends Applet {
• public void paint (Graphics g) {
• g.drawString ("Hello World", 25, 50);
• }
• }
These import statements bring the classes into the scope of our applet class
• java.applet.Applet
• java.awt.Graphics
• Without those import statements, the Java compiler would not recognize the classes Applet
and Graphics, which the applet class refers to.
The Applet Class
• 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.
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
These include methods that do the following
• Get applet parameters
• Get the network location of the HTML file that contains the applet
• Get the network location of the applet class directory
• Print a status message in the browser
• Fetch an image
• Fetch an audio clip
• Play an audio clip
• Resize the applet
• 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
• Request information about the author, version, and copyright of the applet
Request a description of the parameters the applet recognizes
1. Initialize the applet
2. Destroy the applet
3. Start the applet's execution
4. Stop the applet's execution
• The Applet class provides default implementations of each of these methods. Those
implementations may be overridden as necessary.
• The "Hello, World" applet is complete as it stands. The only method overridden is the paint
method.
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
Invoking an Applet
• An applet may be invoked by embedding directives in an HTML file and viewing the file
through an applet viewer or Java-enabled browser.
• The <applet> tag is the basis for embedding an applet in an HTML file. Following is an
example that invokes the "Hello, World" applet
• <html>
• <title>The Hello, World Applet</title>
• <hr>
• <applet code = "HelloWorldApplet.class" width = "320" height = "120">
• If your browser was Java-enabled, a "Hello, World" message would appear here.
• </applet>
• <hr>
• </html>
• The code attribute of the <applet> tag is required. It specifies the Applet class to run. Width
and height are also required to specify the initial size of the panel in which an applet runs. The
applet directive must be closed with an </applet> tag.
• If an applet takes parameters, values may be passed for the parameters by adding <param>
tags between <applet> and </applet>. The browser ignores text and other tags between the
applet tags.
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
GETTING APPLET PARAMETERS
• The following example demonstrates how to make an applet respond to setup
parameters specified in the document. This applet displays a checkerboard pattern
of black and a second color.
• The second color and the size of each square may be specified as parameters to the
applet within the document.
• CheckerApplet gets its parameters in the init() method. It may also get its
parameters in the paint() method. However, getting the values and saving the
settings once at the start of the applet, instead of at every refresh, is convenient and
efficient.
• The applet viewer or browser calls the init() method of each applet it runs. The
viewer calls init() once, immediately after loading the applet. (Applet.init() is
implemented to do nothing.) Override the default implementation to insert custom
initialization code.
• The Applet.getParameter() method fetches a parameter given the parameter's name
(the value of a parameter is always a string). If the value is numeric or other non-
character data, the string must be parsed.
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
The following is a skeleton of CheckerApplet.java
• import java.applet.*;
• import java.awt.*;
• public class CheckerApplet extends Applet {
• int squareSize = 50; // initialized to default size
• public void init() {}
• private void parseSquareSize (String param) {}
• private Color parseColor (String param) {}
• public void paint (Graphics g) {}}
Here are CheckerApplet's init() and private parseSquareSize() methods
• public void init () {
• String squareSizeParam = getParameter ("squareSize");
• parseSquareSize (squareSizeParam);
• String colorParam = getParameter ("color");
• Color fg = parseColor (colorParam);
• setBackground (Color.black); setForeground (fg);
• } private void parseSquareSize (String param) {
• if (param == null) return;
• try { squareSize = Integer.parseInt (param); }
• catch (Exception e) { // Let default value remain }}
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
• The applet calls parseSquareSize() to parse the squareSize parameter. parseSquareSize() calls
the library method Integer.parseInt(), which parses a string and returns an integer.
Integer.parseInt() throws an exception whenever its argument is invalid.
• Therefore, parseSquareSize() catches exceptions, rather than allowing the applet to fail on bad
input.
• The applet calls parseColor() to parse the color parameter into a Color value. parseColor()
does a series of string comparisons to match the parameter value to the name of a predefined
color.
• We need to implement these methods to make this applet work.
• Specifying Applet Parameters
The following is an example of an HTML file with a CheckerApplet embedded in it.
• The HTML file specifies both parameters to the applet by means of the <param> tag.
• <html>
• <title>Checkerboard Applet</title>
• <hr> <applet code = "CheckerApplet.class" width = "480" height = "320">
• <param name = "color" value = "blue">
• <param name = "squaresize" value = "30">
• </applet>
• <hr>
• </html>
• Note − Parameter names are not case sensitive.
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
APPLICATION CONVERSION TO APPLETS
• It is easy to convert a graphical Java application (that is, an application that uses the AWT and
that you can start with the Java program launcher) into an applet that you can embed in a web
page.
Following are the specific steps for converting an application to an applet.
• Make an HTML page with the appropriate tag to load the applet code.
• Supply a subclass of the JApplet class. Make this class public. Otherwise, the applet cannot be
loaded.
• Eliminate the main method in the application. Do not construct a frame window for the
application. Your application will be displayed inside the browser.
• Move any initialization code from the frame window constructor to the init method of the
applet. You don't need to explicitly construct the applet object. The browser instantiates it for
you and calls the init method.
• Remove the call to setSize; for applets, sizing is done with the width and height parameters in
the HTML file.
• Remove the call to setDefaultCloseOperation. An applet cannot be closed; it terminates
when the browser exits.
• If the application calls setTitle, eliminate the call to the method. Applets cannot have title
bars. (You can, of course, title the web page itself, using the HTML title tag.)
• Don't call setVisible(true). The applet is displayed automatically
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
AWT AND AWT HIERARCHY
• Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based
applications in java.
• Java AWT components are platform-dependent i.e. components are displayed according to the
view of operating system. AWT is heavyweight i.e. its components are using the resources of
OS.
• The java.awt package provides classes for AWT api such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc.
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
SWING COMPONENTS
• It is a set of classes that provides more powerful and flexible components that are possible
with the awt.
• Swing components are also called Java Foundation classes(JFC)
METHOD NAME COMMANDS
Abstract button Abstract super class for swing buttons.
Button group Encapsulates a mutually exclusive set of buttons.
Image Icon Encapsulates an icon.
JApplet The swing version of applet.
JButton The swing push button class.
JCheckBox Check box.
JComboBox The swing combination of dropdown list and text field.
JLabel Label.
JRadioButton Swing version of Radio Button.
JScrollpane Encapsulates a scrollable window.
JTabbedWindow Encapsulates a tabbed window.
JTable Encapsulates a table based control.
JTextField Text Box.
JTree Tree based Control.
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
• JAPPLETS:
• Swing must be subclasses of JApplet. Use add( ) method.
• Syntax:
• Container getContentpane( )
• Example:
• void add( components)
• ICONS AND LABELS:
• Syntax:
• ImageIcon( String filename)
• Example:
• ImageIcon(URL url)
• Methods:
• int getIconHeight( )
• int getIconWidth( )
• void paintIcon(Component comp, Graphics g, int x, int y)
• JLABEL:
• JLabel(Icon i)
• JLabel(String s)
• JLabel(String s, Icon I, int align)
• JTEXT FIELDS:
• JText component are JTextFields.
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
• Example:
• JTextField( )
• JTextField(int cols)
• JTextField(String s, int cols)
• JBUTTONS:
• Abstract button is a super class for push buttons, check boxes and radio buttons.
• Example:
• JButton(Icon i)
• JButton(String s)
• JButton(String s, Icon i)
• JCHECKBOX:
• Example:
• JCheckbox(Icon i)
• JCheckbox(Icon i, Boolean State)
• JCheckbox(String s)
• JCheckbox(String s, Icon i, Boolean State)
• Example:
• Void setselected(Boolean State)
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
• JRadioButton:
• Example 1:
• JRadioButton(Icon i)
• JRadioButton(Icon i, String s)
• JRadioButton(String s)
• JRadioButton(String s, Icon i, Boolean State)
• Example 2:
• JRadioButton jb=new JRadioButton(“SAI”);
• JCOMBOBOX:
• Example 1:
• JCombobox( )
• JComboBox(vector v)
• Example 2:
• Void additem(Object obj);
• Example 3:
• JComboBox jb=new JComboBox( );
• jb.addItem(“INDIA”);
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
• JTABBED PANE:
• Tabbed pane is a component that appears as a group of folders in a file cabinet.
• Example:
• void addtab(String str, Component comp)
• Here str is titile for the tab, comp is a add to tabs.
• JSCROLLPANE :
• Example 1:
• Jscrollpane(Component comp)
• Jscrollpane(int vsb, int hsb)
• Jscrollpane(Component comp, int vsb, int hsb)
• Example 2:
• JPanel jp=new JPanel( );
• JScrollpane jsb=new JScrollpane(jp,v,h);
• JTREES:
• It is a hierarchical view of data.
• Syntax:
• JTree( hash table ht)
• JTree(vector v)
A. SIVASANKARI - SIASC-TVM
PROGRAMMING IN JAVA
METHODS
• Add
• Remove
• Example:
• Void add( )
• Void remove( )
• JTABLES:
• It is a combination of rows and columns.
• Syntax:
• JTable(object data[][], object colsheads[])
A. SIVASANKARI - SIASC-TVM
A. SIVASANKARI - SIASC-TVM

More Related Content

PPTX
PROGRAMMING IN JAVA -unit 5 -part I
PPTX
PROGRAMMING IN JAVA-unit 3-part II
PPTX
PROGRAMMING IN JAVA- unit 5-part II
PPTX
PROGRAMMING IN JAVA
PPTX
PROGRAMMING IN JAVA
PPTX
Java unit1 a- History of Java to string
PPTX
PROGRAMMING IN JAVA- unit 4-part II
PPTX
3 jdbc api
PROGRAMMING IN JAVA -unit 5 -part I
PROGRAMMING IN JAVA-unit 3-part II
PROGRAMMING IN JAVA- unit 5-part II
PROGRAMMING IN JAVA
PROGRAMMING IN JAVA
Java unit1 a- History of Java to string
PROGRAMMING IN JAVA- unit 4-part II
3 jdbc api

What's hot (20)

PPTX
Advance Java Topics (J2EE)
PPTX
4 jdbc step1
PPT
PDF
S313265 - Advanced Java API for RESTful Web Services at JavaOne Brazil 2010
PDF
Java 8 features
PPTX
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
PPTX
Java 8 Features
PDF
JAVA BOOK BY SIVASANKARI
PDF
System Integration with Akka and Apache Camel
PDF
Jdbc basic features
DOCX
Colloquium Report
PPTX
Dao example
PDF
PPTX
Java SE 8 - New Features
PPTX
Functional programming with_jdk8-s_ritter
PPTX
Session 38 - Core Java (New Features) - Part 1
PDF
Java 8 in Anger, Devoxx France
PDF
Short intro to scala and the play framework
PPTX
Java 101
PPTX
Turbocharge SQL Performance in PL/SQL with Bulk Processing
Advance Java Topics (J2EE)
4 jdbc step1
S313265 - Advanced Java API for RESTful Web Services at JavaOne Brazil 2010
Java 8 features
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
Java 8 Features
JAVA BOOK BY SIVASANKARI
System Integration with Akka and Apache Camel
Jdbc basic features
Colloquium Report
Dao example
Java SE 8 - New Features
Functional programming with_jdk8-s_ritter
Session 38 - Core Java (New Features) - Part 1
Java 8 in Anger, Devoxx France
Short intro to scala and the play framework
Java 101
Turbocharge SQL Performance in PL/SQL with Bulk Processing
Ad

Similar to PROGRAMMING IN JAVA- unit 4-part I (20)

PPTX
MSBTE Computer Engineering Java applet.pptx
PPTX
applet.pptx
PDF
Java applet basics
PPTX
Applet.pptx
PDF
Java basics notes
PDF
Java basics notes
PDF
Java programming basics notes for beginners(java programming tutorials)
PDF
Java basics notes
PPTX
Applets
PDF
27 applet programming
PPTX
Applet intro
PDF
6applets And Graphics
PPT
Applet and graphics programming
PPTX
Java applet
PDF
Java applet-basics
DOCX
Java applet-basics
PPTX
APPLET.pptx
PPT
Advanced Programming, Java Programming, Applets.ppt
PPTX
Java Apple dndkorksnsbsjdkkdjejdjrdndjdj
PPTX
oops with java modules iii & iv.pptx
MSBTE Computer Engineering Java applet.pptx
applet.pptx
Java applet basics
Applet.pptx
Java basics notes
Java basics notes
Java programming basics notes for beginners(java programming tutorials)
Java basics notes
Applets
27 applet programming
Applet intro
6applets And Graphics
Applet and graphics programming
Java applet
Java applet-basics
Java applet-basics
APPLET.pptx
Advanced Programming, Java Programming, Applets.ppt
Java Apple dndkorksnsbsjdkkdjejdjrdndjdj
oops with java modules iii & iv.pptx
Ad

More from SivaSankari36 (6)

PDF
DATA STRUCTURE BY SIVASANKARI
PDF
CLOUD COMPUTING BY SIVASANKARI
PDF
MOBILE APPLICATIONS DEVELOPMENT -ANDROID BY SIVASANKARI
PDF
MOBILE COMPUTING BY SIVASANKARI
PPTX
Functional MRI using Apache Spark in Big Data Application
PPTX
Java unit1 b- Java Operators to Methods
DATA STRUCTURE BY SIVASANKARI
CLOUD COMPUTING BY SIVASANKARI
MOBILE APPLICATIONS DEVELOPMENT -ANDROID BY SIVASANKARI
MOBILE COMPUTING BY SIVASANKARI
Functional MRI using Apache Spark in Big Data Application
Java unit1 b- Java Operators to Methods

Recently uploaded (20)

PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Onica Farming 24rsclub profitable farm business
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
English Language Teaching from Post-.pdf
PDF
Business Ethics Teaching Materials for college
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Microbial diseases, their pathogenesis and prophylaxis
STATICS OF THE RIGID BODIES Hibbelers.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pharma ospi slides which help in ospi learning
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Introduction and Scope of Bichemistry.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Onica Farming 24rsclub profitable farm business
Pharmacology of Heart Failure /Pharmacotherapy of CHF
The Final Stretch: How to Release a Game and Not Die in the Process.
102 student loan defaulters named and shamed – Is someone you know on the list?
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
English Language Teaching from Post-.pdf
Business Ethics Teaching Materials for college
Renaissance Architecture: A Journey from Faith to Humanism

PROGRAMMING IN JAVA- unit 4-part I

  • 1. PROGRAMMING IN JAVA A. SIVASANKARI ASSISTANT PROFESSOR DEPARTMENT OF COMPUTER APPLICATION SHANMUGA INDUSTRIES ARTS AND SCIENCE COLLEGE, TIRUVANNAMALAI. 606601. Email: [email protected]
  • 2. PROGRAMMING IN JAVA UNIT – 4 PART I  APPLET  APPLETS - GUI COMPONENTS  APPLET PARAMETERS  LIFE CYCLE OF AN APPLET  APPLICATION CONVERSION TO APPLETS  AWT AND AWT HIERARCHY  SWING COMPONENTS A. SIVASANKARI - SIASC-TVM UNIT-4
  • 3. PROGRAMMING IN JAVA APPLET BASICS • An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal. • There are some important differences between an applet and a standalone Java application, including the following • An applet is a Java class that extends the java.applet.Applet class. • A main() method is not invoked on an applet, and an applet class will not define main(). • Applets are designed to be embedded within an HTML page. • When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine. • A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment. • The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime. • Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed. • Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file. A. SIVASANKARI - SIASC-TVM
  • 4. PROGRAMMING IN JAVA APPLETS (GUI COMPONENTS) • Applets are small java programs that run inside of a web browser. • It is also stored as part of webpage. • The applet classes contained in the java applet package. • It also defines three interfaces. 1. Applet context 2. Audio clip 3. Applet stub • It will be executed within HTML tag. • It is an intelligent program. • It is an application designed to be transmitted over internet and executed by a java compatible with browser. SYNTAX • <applet code= “ program name” width=200 height=200> </applet> APPLET INITIALIZATION AND TERMINATION When started • Init( ) • Start( ) • Paint( ) When Applet is terminated • Stop( ) • Destroy( ) A. SIVASANKARI - SIASC-TVM
  • 5. PROGRAMMING IN JAVA Another method is overriding update( ) EXAMPLE • Public void paint (Graphics g) • { • Update(g); • } APPLET CLASSES OR METHODS APPLET METHODS DESCRIPTION void destroy( ) It called by the browser just before an applet is terminated. String getAppletinfo( ) It should return String. URL getcodeBase( ) It returns URL image getimage(URL url) It returns image object. void init( ) It begins execution. void start( ) It should starts of execution. Void stop( ) Browser to suspend execution of applets. A. SIVASANKARI - SIASC-TVM
  • 6. PROGRAMMING IN JAVA A. SIVASANKARI - SIASC-TVM
  • 7. PROGRAMMING IN JAVA EXAMPLE • import java.awt.*; • import java.applet.*; • /*<applet code=”sample” width=300 height=200></applet> • public class sample extends Applet{ • Public void init( ){ • setBackground(color.cyan)} • Public void paint( Graphics g){ • g.drawString(“This is sample”,10,20); • show status(“This is status window”);}} HTMLAPPLET TAGS • <APPLET[CODE BASE=code base URL] name of file CODE=applet file • [ALT= alternate text] [NAME= applet instance name]name of attributes • [WIDTH=pixels, HEIGHT=PIXELS]height and width of window • [ALIGN= alignment]top, bottom, up, down, middle • [VSPACE=Pixels]vertical space [HSPACE=Pixels]>horizontal space • [<param name=attribute name VALUE=attribute value>] • </APPLET> A. SIVASANKARI - SIASC-TVM
  • 8. PROGRAMMING IN JAVA LIFE CYCLE OF AN APPLET • Four methods in the Applet class gives you the framework on which you build any serious applet • init − This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed. • start − This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages. • stop − This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet. • destroy − This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet. • paint − Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt. A. SIVASANKARI - SIASC-TVM
  • 9. PROGRAMMING IN JAVA A "Hello, World" Applet Following is a simple applet named HelloWorldApplet.java • import java.applet.*; • import java.awt.*; • public class HelloWorldApplet extends Applet { • public void paint (Graphics g) { • g.drawString ("Hello World", 25, 50); • } • } These import statements bring the classes into the scope of our applet class • java.applet.Applet • java.awt.Graphics • Without those import statements, the Java compiler would not recognize the classes Applet and Graphics, which the applet class refers to. The Applet Class • 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. A. SIVASANKARI - SIASC-TVM
  • 10. PROGRAMMING IN JAVA These include methods that do the following • Get applet parameters • Get the network location of the HTML file that contains the applet • Get the network location of the applet class directory • Print a status message in the browser • Fetch an image • Fetch an audio clip • Play an audio clip • Resize the applet • 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 • Request information about the author, version, and copyright of the applet Request a description of the parameters the applet recognizes 1. Initialize the applet 2. Destroy the applet 3. Start the applet's execution 4. Stop the applet's execution • The Applet class provides default implementations of each of these methods. Those implementations may be overridden as necessary. • The "Hello, World" applet is complete as it stands. The only method overridden is the paint method. A. SIVASANKARI - SIASC-TVM
  • 11. PROGRAMMING IN JAVA Invoking an Applet • An applet may be invoked by embedding directives in an HTML file and viewing the file through an applet viewer or Java-enabled browser. • The <applet> tag is the basis for embedding an applet in an HTML file. Following is an example that invokes the "Hello, World" applet • <html> • <title>The Hello, World Applet</title> • <hr> • <applet code = "HelloWorldApplet.class" width = "320" height = "120"> • If your browser was Java-enabled, a "Hello, World" message would appear here. • </applet> • <hr> • </html> • The code attribute of the <applet> tag is required. It specifies the Applet class to run. Width and height are also required to specify the initial size of the panel in which an applet runs. The applet directive must be closed with an </applet> tag. • If an applet takes parameters, values may be passed for the parameters by adding <param> tags between <applet> and </applet>. The browser ignores text and other tags between the applet tags. A. SIVASANKARI - SIASC-TVM
  • 12. PROGRAMMING IN JAVA GETTING APPLET PARAMETERS • The following example demonstrates how to make an applet respond to setup parameters specified in the document. This applet displays a checkerboard pattern of black and a second color. • The second color and the size of each square may be specified as parameters to the applet within the document. • CheckerApplet gets its parameters in the init() method. It may also get its parameters in the paint() method. However, getting the values and saving the settings once at the start of the applet, instead of at every refresh, is convenient and efficient. • The applet viewer or browser calls the init() method of each applet it runs. The viewer calls init() once, immediately after loading the applet. (Applet.init() is implemented to do nothing.) Override the default implementation to insert custom initialization code. • The Applet.getParameter() method fetches a parameter given the parameter's name (the value of a parameter is always a string). If the value is numeric or other non- character data, the string must be parsed. A. SIVASANKARI - SIASC-TVM
  • 13. PROGRAMMING IN JAVA The following is a skeleton of CheckerApplet.java • import java.applet.*; • import java.awt.*; • public class CheckerApplet extends Applet { • int squareSize = 50; // initialized to default size • public void init() {} • private void parseSquareSize (String param) {} • private Color parseColor (String param) {} • public void paint (Graphics g) {}} Here are CheckerApplet's init() and private parseSquareSize() methods • public void init () { • String squareSizeParam = getParameter ("squareSize"); • parseSquareSize (squareSizeParam); • String colorParam = getParameter ("color"); • Color fg = parseColor (colorParam); • setBackground (Color.black); setForeground (fg); • } private void parseSquareSize (String param) { • if (param == null) return; • try { squareSize = Integer.parseInt (param); } • catch (Exception e) { // Let default value remain }} A. SIVASANKARI - SIASC-TVM
  • 14. PROGRAMMING IN JAVA • The applet calls parseSquareSize() to parse the squareSize parameter. parseSquareSize() calls the library method Integer.parseInt(), which parses a string and returns an integer. Integer.parseInt() throws an exception whenever its argument is invalid. • Therefore, parseSquareSize() catches exceptions, rather than allowing the applet to fail on bad input. • The applet calls parseColor() to parse the color parameter into a Color value. parseColor() does a series of string comparisons to match the parameter value to the name of a predefined color. • We need to implement these methods to make this applet work. • Specifying Applet Parameters The following is an example of an HTML file with a CheckerApplet embedded in it. • The HTML file specifies both parameters to the applet by means of the <param> tag. • <html> • <title>Checkerboard Applet</title> • <hr> <applet code = "CheckerApplet.class" width = "480" height = "320"> • <param name = "color" value = "blue"> • <param name = "squaresize" value = "30"> • </applet> • <hr> • </html> • Note − Parameter names are not case sensitive. A. SIVASANKARI - SIASC-TVM
  • 15. PROGRAMMING IN JAVA APPLICATION CONVERSION TO APPLETS • It is easy to convert a graphical Java application (that is, an application that uses the AWT and that you can start with the Java program launcher) into an applet that you can embed in a web page. Following are the specific steps for converting an application to an applet. • Make an HTML page with the appropriate tag to load the applet code. • Supply a subclass of the JApplet class. Make this class public. Otherwise, the applet cannot be loaded. • Eliminate the main method in the application. Do not construct a frame window for the application. Your application will be displayed inside the browser. • Move any initialization code from the frame window constructor to the init method of the applet. You don't need to explicitly construct the applet object. The browser instantiates it for you and calls the init method. • Remove the call to setSize; for applets, sizing is done with the width and height parameters in the HTML file. • Remove the call to setDefaultCloseOperation. An applet cannot be closed; it terminates when the browser exits. • If the application calls setTitle, eliminate the call to the method. Applets cannot have title bars. (You can, of course, title the web page itself, using the HTML title tag.) • Don't call setVisible(true). The applet is displayed automatically A. SIVASANKARI - SIASC-TVM
  • 16. PROGRAMMING IN JAVA AWT AND AWT HIERARCHY • Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based applications in java. • Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e. its components are using the resources of OS. • The java.awt package provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc. A. SIVASANKARI - SIASC-TVM
  • 17. PROGRAMMING IN JAVA SWING COMPONENTS • It is a set of classes that provides more powerful and flexible components that are possible with the awt. • Swing components are also called Java Foundation classes(JFC) METHOD NAME COMMANDS Abstract button Abstract super class for swing buttons. Button group Encapsulates a mutually exclusive set of buttons. Image Icon Encapsulates an icon. JApplet The swing version of applet. JButton The swing push button class. JCheckBox Check box. JComboBox The swing combination of dropdown list and text field. JLabel Label. JRadioButton Swing version of Radio Button. JScrollpane Encapsulates a scrollable window. JTabbedWindow Encapsulates a tabbed window. JTable Encapsulates a table based control. JTextField Text Box. JTree Tree based Control. A. SIVASANKARI - SIASC-TVM
  • 18. PROGRAMMING IN JAVA • JAPPLETS: • Swing must be subclasses of JApplet. Use add( ) method. • Syntax: • Container getContentpane( ) • Example: • void add( components) • ICONS AND LABELS: • Syntax: • ImageIcon( String filename) • Example: • ImageIcon(URL url) • Methods: • int getIconHeight( ) • int getIconWidth( ) • void paintIcon(Component comp, Graphics g, int x, int y) • JLABEL: • JLabel(Icon i) • JLabel(String s) • JLabel(String s, Icon I, int align) • JTEXT FIELDS: • JText component are JTextFields. A. SIVASANKARI - SIASC-TVM
  • 19. PROGRAMMING IN JAVA • Example: • JTextField( ) • JTextField(int cols) • JTextField(String s, int cols) • JBUTTONS: • Abstract button is a super class for push buttons, check boxes and radio buttons. • Example: • JButton(Icon i) • JButton(String s) • JButton(String s, Icon i) • JCHECKBOX: • Example: • JCheckbox(Icon i) • JCheckbox(Icon i, Boolean State) • JCheckbox(String s) • JCheckbox(String s, Icon i, Boolean State) • Example: • Void setselected(Boolean State) A. SIVASANKARI - SIASC-TVM
  • 20. PROGRAMMING IN JAVA • JRadioButton: • Example 1: • JRadioButton(Icon i) • JRadioButton(Icon i, String s) • JRadioButton(String s) • JRadioButton(String s, Icon i, Boolean State) • Example 2: • JRadioButton jb=new JRadioButton(“SAI”); • JCOMBOBOX: • Example 1: • JCombobox( ) • JComboBox(vector v) • Example 2: • Void additem(Object obj); • Example 3: • JComboBox jb=new JComboBox( ); • jb.addItem(“INDIA”); A. SIVASANKARI - SIASC-TVM
  • 21. PROGRAMMING IN JAVA • JTABBED PANE: • Tabbed pane is a component that appears as a group of folders in a file cabinet. • Example: • void addtab(String str, Component comp) • Here str is titile for the tab, comp is a add to tabs. • JSCROLLPANE : • Example 1: • Jscrollpane(Component comp) • Jscrollpane(int vsb, int hsb) • Jscrollpane(Component comp, int vsb, int hsb) • Example 2: • JPanel jp=new JPanel( ); • JScrollpane jsb=new JScrollpane(jp,v,h); • JTREES: • It is a hierarchical view of data. • Syntax: • JTree( hash table ht) • JTree(vector v) A. SIVASANKARI - SIASC-TVM
  • 22. PROGRAMMING IN JAVA METHODS • Add • Remove • Example: • Void add( ) • Void remove( ) • JTABLES: • It is a combination of rows and columns. • Syntax: • JTable(object data[][], object colsheads[]) A. SIVASANKARI - SIASC-TVM
  • 23. A. SIVASANKARI - SIASC-TVM