Sie sind auf Seite 1von 12

Applet

ETL LABS PVT LTD – JAVA PROGRAMMING 176


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
• It works at client side so less response
4 time.
• Secured
• It can be executed by browsers
running under many plateforms,
including Linux, Windows, Mac Os
etc.
Drawback of Applet
• Plugin is required at client browser to
execute applet.

ETL LABS PVT LTD – JAVA PROGRAMMING 177


Lifecycle of Applet

initialized started painted

stopped destroyed

ETL LABS PVT LTD – JAVA PROGRAMMING 178


Lifecycle methods for Applet
For creating any applet java.applet.Applet class
must be inherited. It provides 4 life cycle
methods of applet.

1) public void init(): is used to initialized the


Applet. It is invoked only once.
2) public void start(): is invoked after the 3
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.

ETL LABS PVT LTD – JAVA PROGRAMMING 179


Graphics in Applet
java.awt.Graphics class provides many
methods for graphics programming.

4 Commonly used methods of Graphics


class:
public abstract void drawString(String str,
int x, int y): is used to draw the specified
string, and many more.

ETL LABS PVT LTD – JAVA PROGRAMMING 180


Displaying Image in Applet
Applet is mostly used in games and animation.
For this purpose image is required to be
displayed. The java.awt.Graphics class provide a
method drawImage() to display the image.

Syntax of drawImage() method:


public abstract boolean drawImage(Image img,
int x, int y, ImageObserver observer): is used
draw the specified image.

Syntax of the object of Image:


public Image getImage(URL u, String
image){}

ETL LABS PVT LTD – JAVA PROGRAMMING 181


Animation in Applet
4 Applet is mostly used in games and
animation. For this purpose image is
required to be moved.

ETL LABS PVT LTD – JAVA PROGRAMMING 182


EventHandling in Applet

As we perform event handling in AWT or


Swing, we can perform it in applet also.
Let's see the simple example of event
handling in applet that prints a message
by click on the button.

ETL LABS PVT LTD – JAVA PROGRAMMING 183


JApplet class in
Applet
As we prefer Swing to AWT. Now we can
use JApplet that can have all the
controls of swing. The JApplet class
extends the Applet class.

ETL LABS PVT LTD – JAVA PROGRAMMING 184


Painting in Applet

We can perform painting operation in


applet by the mouseDragged() method of
MouseMotionListener.

ETL LABS PVT LTD – JAVA PROGRAMMING 185


Parameter in Applet
Applet class provides a method named
getParameter().
4
Syntax:
public String getParameter(String
parameterName)

ETL LABS PVT LTD – JAVA PROGRAMMING 186


Applet Communication

java.applet.AppletContext class
provides the facility of communication
between applets. It provides
getApplet() method that returns the
object of Applet.

Syntax:
public Applet getApplet(String
name){}

ETL LABS PVT LTD – JAVA PROGRAMMING 187

Das könnte Ihnen auch gefallen