Download Java Applet - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java Applet
Introductions
Applet is java program that can be embedded into
HTML pages
Java applets runs on the java enables web browsers
such as mozila and internet explorer.
Applet is designed to run remotely on the client
browser, so there are some restrictions on it.
Applet can't access system resources on the local
computer.
Applets are used to make the web site more dynamic
and entertaining.

Advantages of Applet
* Applets are cross platform and can run on
Windows, Mac OS and Linux platform
* Applets can work all the version of Java Plugin
* Applets runs in a sandbox, so the user does not
need to trust the code, so it can work without security
approval
* Applets are supported by most web browsers
* Applets are cached in most web browsers, so will
be quick to load when returning to a web page
* User can also have full access to the machine if
user allows
Disadvantages of Java Applet
* Java plug-in is required to run applet
* Java applet requires JVM so first time it takes
significant startup time
* If applet is not already cached in the machine, it
will be downloaded from internet and will take time
* Its difficult to desing and build good user interface
in applets compared to HTML technology
The Applet class
To create an applet, you must import the Applet
class
This class is in the java.applet package
The Applet class contains code that works with a
browser to create a display window
Capitalization matters!
applet and Applet are different names

Importing the Applet class
Here is the directive that you need:

import java.applet.Applet;
import is a keyword
java.applet is the name of the package
A dot ( . ) separates the package from the class
Applet is the name of the class
There is a semicolon ( ; ) at the end

Applet versus Application
Applets are small programs while applications are
larger programs.
 Applets don't have the main method while in an
application execution starts with the main method.
Applets can run in our browser's window or in an
appletviewer.
 To run the applet in an appletviewer will be an
advantage for debugging.
Applets are designed for the client site programming
purpose while the applications don't have such type of
criteria.

The Life cycle of An Applet
init(): This method is called to initialized an applet
start(): This method is called after the initialization of
the applet.
stop(): This method can be called multiple times in the
life cycle of an Applet.
destroy(): This method is called only once in the life
cycle of the applet when applet is destroyed.

The Life cycle of An Applet
import java.awt.*;
import java.applet.*;
class Myclass extends Applet{
public void init(){}
Publicvoidstart(){}
public void stop() {}
public void destroy() {}
public void paint(Graphics g) {}
Applet Tag
<] APPLET
[CODEBASE = codebaseURL]
CODE = appletFile
[ALT = alternateText] [NAME =
appletInstanceName]
WIDTH = pixels
HEIGHT =
pixels
[ALIGN = alignment] [VSPACE = pixels]
[HSPACE = pixels] > [< PARAM NAME =
appletParameter1 VALUE = value >]
[< PARAM NAME =
appletParameter2 VALUE = value >] . . [alternateHTML]
[</]APPLET[>]
[
Creating First Applet Example
import java.applet.*;
import java.awt.*;
public class FirstApplet extends Applet{
public void paint(Graphics g){
g.drawString("Welcome in Java Applet.",40,20);
}
}
HTML code
<HTML>
<HEAD>
</HEAD>
<BODY>
<APPLET ALIGN="CENTER"
CODE="FirstApplet.class" WIDTH="800"
HEIGHT="500"></APPLET>
</BODY>
</HTML>
Drawing Shapes Example in java
Graphics.drawLine() : to draw the line in the applet.
drawLine(int X_from_coordinate, int Y_from_coordinate, int
X_to_coordinate, int Y_to_coordinate);

Graphics.drawString() : draws the given string as the parameter
drawString(String string, int X_coordinate, int Y_coordinate);

Graphics.drawOval() : draws the circle

g.drawOval(int X_coordinate, int Y_coordinate, int Wdth, int height);

Graphics.drawRect() : draws the rectangle. Here is the syntax of the
drawRect() method :

g.drawRect(int X_coordinate, int Y_coordinate, int Wdth, int height)

Drawing Shapes Example using
color in java
Graphics.setColor() : sets the color for the object by
specified color.
setColor(Color.color_name);
Graphics.fillOval() : to fill the color inside the oval by
specified color
g.fillColor(Color.color_name);
Graphics.fillRect() :to fill the color inside the
rectangle by specified color
g.fillRect(int X_coordinate, int Y_coordinate, int
Wdth, int height)

Passing Parameter in Java Applet
The param tag(<parma name="" value=""></param>)
is used to pass the parameters to an applet
To access values
String strParameter = this.getParameter("Message");
Event Listeners Example
To handle the events generated by these buttons you
add action listeners
e.g. object_name.addActionListener(this);.
When the action event occurs, that object's
actionPerformed method is invoked.
actionPerformed(ActionEvent e)
Display image
paint(Graphics g) has used
MediaTracker is a utility class that tracks the status of
a number of media objects
img - image name type of Image.
x
- lower X - Coordinate type of int.
y
- lower Y - Coordinate type of int.
x1
- upper X - Coordinate type of int.
y1
- upper Y - Coordinate type of int.
