Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Java Applet Programming
Rohan K. Gajre.
Outline
• Introduction to java Programming
• Applet Overview
• Applet Skeleton
• Steps to Write Applet Code
• Compilation Process
• Java Applet E.g. with HTML Code
What is Java
• Java is Simplified version of C++
• Java is a programming language that is object Oriented and specifically
designed to have as few implementation dependencies as possible
• Java Programs can be easily moved form one computer to another
• Almost everything in Java is an Object and Classes
• Java has the ability to share both data and program
• Java does not use pointer and header files
• A class is collection of Object of similar type
What is Applet
• Applet are Java Program that are used for Internet Computing
• Applet can be transported over the internet from one computer to
another
• It can perform logical and arithmetic operations as well as accept user
input
• Applet are event driven
• If you want to write applet code so you need to import two packages
that is awt(Abstract Window Toolkit) and applet
Cont.
• Execution of applet code does not begin with main()function
• Types of Applet
Local Applet :- Developed & stored in Local System
Remote Applet :- Developed & stored on a Remote computer
connected to the internet
• Once an applet has been compiled, it is included in an HTML file
using Applet tag
Cont.
• Applets are not executed by a console based java run-time interpreter,
but they are executed by either a java enabled web browser or an
applet viewer.
• You need two notepad one for java(Applet)code and another for
HTML code having Applet Tag in that
An Applet Skeleton
• Applet skeleton includes different methods for writing applet
• When an applet begins, the AWT calls the following methods, in this
sequence:
init( )
start( )
paint( )
• When an applet is terminated, the following sequence of method calls
takes place:
Cont.
• stop( )
• destroy( )
• Above all methods are in Applet Life cycle
• Explanation about above is as follows:
• init( ):- init() is the first method to be called. This is where you should
initialize variables. This method is called only once during run time of
your applet
Cont.
• start( ):- start() called after init(), also called to restart applet after it
has been stopped. start() is called each time an applet’s HTML
document is displayed on screen.
• paint( ):- paint() is called each time your applets output must be
redrawn. also called when the applet begins execution. This method
has one parameter, Graphics.
• stop( ):- stop()is called when a web browser leaves the HTML
document containing the applet- when it goes to another page.
Cont.
• destroy( ) :- destroy() is called when environment determines that
your applet needs to be removed completely from memory. The stop()
method is always called before destroy().
Steps to write Applet code
• Writing an applet code in java file & save with class name (.java file)
• Compile it & create (.class file)
• Design a page using HTML tags
• Write a <APPLET> Tag in that
• Save it same as a class name with .HTML extension
• Testing
Compilation Process
• Open command prompt then go to directory using CD command
• E.g. c:Users>d:
• I am in D Drive now going to my directory using CD command
• D:> cd Ram
• I am in my directory that is Ram
• Compile source file D:Ram> Javac Classname.java
• E.g. D:Ram> Javac Demo.java
Cont.
• Open file in appletviewer
• Then Run HTML code
• D:Ram> appletviewer classname.html
• E.g. D:Ram> appletviewer Demo.html
Following Program shows a simple java
applet
import java.applet.*;
import java.awt.*;
public class DemoApp extends Applet{
public void paint (Graphics g) {
g.drawString (“Welcome in Applet World", 25, 50);
}
}
Cont.
• Save source file with name DemoApp.java
• Compile source file D:Ram> javac DemoApp.java
• Write HTML File and save it DemoApp.html
HTML Code
<html>
<title> Welcome in Applet World </title>
<hr>
<applet code=“DemoApp.class" width="320" height="120">
</applet>
</hr>
</html>
Cont.
• Open file in appletviewer
• Then Run HTML code
• D:Ram> appletviewer DemoApp.html
Java applet

More Related Content

Java applet

  • 2. Outline • Introduction to java Programming • Applet Overview • Applet Skeleton • Steps to Write Applet Code • Compilation Process • Java Applet E.g. with HTML Code
  • 3. What is Java • Java is Simplified version of C++ • Java is a programming language that is object Oriented and specifically designed to have as few implementation dependencies as possible • Java Programs can be easily moved form one computer to another • Almost everything in Java is an Object and Classes • Java has the ability to share both data and program • Java does not use pointer and header files • A class is collection of Object of similar type
  • 4. What is Applet • Applet are Java Program that are used for Internet Computing • Applet can be transported over the internet from one computer to another • It can perform logical and arithmetic operations as well as accept user input • Applet are event driven • If you want to write applet code so you need to import two packages that is awt(Abstract Window Toolkit) and applet
  • 5. Cont. • Execution of applet code does not begin with main()function • Types of Applet Local Applet :- Developed & stored in Local System Remote Applet :- Developed & stored on a Remote computer connected to the internet • Once an applet has been compiled, it is included in an HTML file using Applet tag
  • 6. Cont. • Applets are not executed by a console based java run-time interpreter, but they are executed by either a java enabled web browser or an applet viewer. • You need two notepad one for java(Applet)code and another for HTML code having Applet Tag in that
  • 7. An Applet Skeleton • Applet skeleton includes different methods for writing applet • When an applet begins, the AWT calls the following methods, in this sequence: init( ) start( ) paint( ) • When an applet is terminated, the following sequence of method calls takes place:
  • 8. Cont. • stop( ) • destroy( ) • Above all methods are in Applet Life cycle • Explanation about above is as follows: • init( ):- init() is the first method to be called. This is where you should initialize variables. This method is called only once during run time of your applet
  • 9. Cont. • start( ):- start() called after init(), also called to restart applet after it has been stopped. start() is called each time an applet’s HTML document is displayed on screen. • paint( ):- paint() is called each time your applets output must be redrawn. also called when the applet begins execution. This method has one parameter, Graphics. • stop( ):- stop()is called when a web browser leaves the HTML document containing the applet- when it goes to another page.
  • 10. Cont. • destroy( ) :- destroy() is called when environment determines that your applet needs to be removed completely from memory. The stop() method is always called before destroy().
  • 11. Steps to write Applet code • Writing an applet code in java file & save with class name (.java file) • Compile it & create (.class file) • Design a page using HTML tags • Write a <APPLET> Tag in that • Save it same as a class name with .HTML extension • Testing
  • 12. Compilation Process • Open command prompt then go to directory using CD command • E.g. c:Users>d: • I am in D Drive now going to my directory using CD command • D:> cd Ram • I am in my directory that is Ram • Compile source file D:Ram> Javac Classname.java • E.g. D:Ram> Javac Demo.java
  • 13. Cont. • Open file in appletviewer • Then Run HTML code • D:Ram> appletviewer classname.html • E.g. D:Ram> appletviewer Demo.html
  • 14. Following Program shows a simple java applet import java.applet.*; import java.awt.*; public class DemoApp extends Applet{ public void paint (Graphics g) { g.drawString (“Welcome in Applet World", 25, 50); } }
  • 15. Cont. • Save source file with name DemoApp.java • Compile source file D:Ram> javac DemoApp.java • Write HTML File and save it DemoApp.html
  • 16. HTML Code <html> <title> Welcome in Applet World </title> <hr> <applet code=“DemoApp.class" width="320" height="120"> </applet> </hr> </html>
  • 17. Cont. • Open file in appletviewer • Then Run HTML code • D:Ram> appletviewer DemoApp.html