3. Overview
• A Java applet is a small application written in Java and
delivered to users in the form of bytecode.
• A applet is typically embedded in a Web page and can be run
from a browser.
• The user launches the Java applet from a web page and it is
then executed within a Java Virtual Machine (JVM) in
a process separate from the web browser itself.
• A Java applet can appear in a frame of the web page, a new
application window, Sun's AppletViewer or a stand-alone tool
for testing applets.
• Java applets were introduced in the first version of the Java
language in 1995.
4. Applet Support
• Most modern browsers support Java 1.4 if
they have the appropriate plugin
• In the PC labs, Internet Explorer 5.5 has been
updated, but Netscape has not
• The best support isn't a browser, but the
standalone program applet viewer.
• In general you should try to write applets that
can be run with any browser
5. What an applet is
• You write an applet by extending the class
Applet.
• Applet is just a class like any other; you can
even use it in applications if you want.
• When you write an applet, you are only
writing part of a program.
• The browser supplies the main method.
7. The Genealogy Of Applet
java.lang.Object
|
+----java.awt.Component
|
+----java.awt.Container
|
+----java.awt.Panel
|
+----java.applet.Applet
8. The Simplest Possible Applet
• TrivialApplet.java:
import java.applet.Applet;
public class TrivialApplet extends Applet { }
• TrivialApplet.html:
• <applet
code="TrivialApplet.class”
width=150 height=100>
• </applet>
9. The Simplest Reasonable Applet
import java.awt.*;
import java.applet.Applet;
public class HelloWorld extends Applet {
public void paint( Graphics g ) {
g.drawString( "Hello World!", 30, 30 );
}
}
10. Applet methods
public void init ()
public void start ()
public void stop ()
public void destroy ()
public void paint (Graphics)
Also:
public void repaint()
public void update (Graphics)
public void showStatus(String)
public String getParameter(String)
11. Methods are called in this order
• init and destroy are only
called once each
• start and stop are called
whenever the browser
enters and leaves the
page
• do some work is code
called by your listeners
• paint is called when the
applet needs to be
repainted
init()
start()
stop()
destroy()
do some work
12. Examples
import java.applet.*;
import java.awt.*;
public class Main extends Applet{
public void paint(Graphics g){
g.drawString("Welcome in Java Applet.",40,20);
}
}
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}
}