JAVA APPLETS
By :- Darshana D Ghadi
What is Java Applet ?
Java Applets are dynamic and interactive programs, were
introduced in the first version of the Java language in 1995.
A Java applet is a special case of the applet, a small program
(or mini application, thus "applet"), they are commonly
embedded within the HTML of a website.
What you need to execute an Applet ?
Browser (IE 4.0 or Netscape Navigator 4.0 or higher)
- using a Java Virtual Machine (JVM)
Applet viewer utility in the JDK.
Execution Platforms
Microsoft Windows, Unix, Mac OS and Linux
Features of an Applet
Provides facility for frames
Event handling facility and user Interaction
GUI
Graphics and multimedia
Example
import java.applet.Applet.*;
import java.awt.*;
// Applet code for the "Hello, world!" example.
// This should be saved in a file named as "HelloWorld.java".
public class HelloWorld extends Applet {
// This method is mandatory, but can be empty (i.e., have no actual
code).
public void init() { }
// This method is mandatory, but can be empty.(i.e.,have no actual code).
public void stop() { }
// Print a message on the screen (x=20, y=10).
public void paint(Graphics g) {
g.drawString("Hello, world!", 20,10);
// Draws a circle on the screen (x=40, y=30).
g.drawArc(40,30,20,20,0,360); } }
Create, Compile & Run
Typein the following code using any text editor or in
DOS editor.
import java.awt.*;
import java.applet.*;
public class hello extends Applet {
public void paint(Graphics g) {
g.drawString("Welcome to Java Applets",20,20); } }
Save the file as hello.java and compile it by using javac.
To
save the program & compile follow the steps :-
Start -> Run -> cmd -> c:jdk.1.5bin -> hello.java
Aftercompilation you will be left with the prompt –
c:jdk.1.5bin ( which indicates the a hello.class file has been created)
For execution type – applet Display viewer hello
Alternative to execute Java Applet
After compilation type in the following HTML code in your editor and
save the file as hello.html
<applet code = "classhello.class" width = 200 height =150 >
</applet>
Execute the HTML file by giving appletviewer hello.html
Life Cycle of an Applet
Methods :-
drawString(String str,int X,int Y)
drawLine(int x1,int y1,int x2,int y2)
drawRect(int x1,int y1,int width,int height)
drawOval(int x1,int y1,int width,int height)
drawArc(int x1,int y1,int width,int height,angle1 ,angle2)
Font Class :-
Font f = new Font("Courier",Font.BOLD+Font.Italic, 16);
g.setFont(f);
Color Class :-
setColor(Color.gray) sets the string color to gray .
setBackground(Color.red) sets the background color to red.
User Interface Components :-
Label, Checkboxes , Button, Text field, Text area , etc.
Layout Managers
Flow Layout
Border Layout
Grid Layout
Card Layout
Applications of Java Applet
Entertainment
Communication
Utilities and Productivity
Educational Tools
Mobile Programs
http://rainbowuniverse.org/id55.htm
Advantages
It is simple
It can work on any platform
No need to support legacy versions
Applet naturally supports the changing user state, such as figure positions
on the chessboard
An untrusted applet has no access to the local machine and can only
access the server it came from.
Java applets are fast
Disadvantages
It requires the Java plug-in.
Security restrictions may make it difficult or even impossible for an
untrusted applet to achieve the desired goals.
Some applets require a specific JRE. This is discouraged.
THANK YOU !