Introduction To Java Applets
Introduction To Java Applets
APPLETS
Applets
• An applet is a special kind of Java program, that a browser enabled
with Java technology can download from the internet and run.
• Java applets are also analyzed class by class as they are loaded by the
run-time environment in your browser.
Hello Java
<app=
“Hello”> The Internet
Hello
How Applets Differ from
Applications
• Although both the Applets and stand-alone applications
are Java programs, there are certain restrictions are
imposed on Applets due to security concerns:
– Applets don’t use the main() method, but when they are load,
automatically call certain methods (init, start, paint, stop,
destroy).
– They are embedded inside a web page and executed in
browsers.
– They cannot read from or write to the files on local computer.
– They cannot communicate with other servers on the network.
– They cannot run any programs from the local computer.
– They are restricted from using libraries from other languages.
• The above restrictions ensures that an Applet cannot do
any damage to the local system.
Applet Life Cycle
• Every applet inherits a set of default behaviours from the
Applet class. As a result, when an applet is loaded, it
undergoes a series of changes in its state.
Running Idle
destroy()
paint() start()
Dead End
• public class Applet extends Panel
java.lang.Object
|
+----java.awt.Component
|
+----java.awt.Container
|
+----java.awt.Panel
|
+----java.applet.Applet
• Painting on a component is accomplished by making calls to a
graphics context, which is an instance of the Graphics class. A
graphics context knows how to render onto a single target. The
three media a graphics context can render onto are:
– Components
– Images
– Printers
– Applet
– Canvas
– Frame
– Panel
– Selecting a color
– Selecting a font
– Drawing and filling
– Clipping
–
Applet menu in the appletviewer:
• Applet class gets “free” version of each of these methods from class
JApplet when you specify extends JApplet in the first line of the
class declaration. If you don’t declare these methods, the applet
container calls the inherited version, which bodies do not contain
any statements and the super class method paint does not draw
anything on the applet.
5. super.paint (g)
• Calls the version of method paint from
super class JApplet. For now this should
be the first statement in every paint
method declaration.
• 6. g.drawString(“Welcome to Java Programming”, 25,25);
• The ‘g.’ at the beginning of the statement includes that paint should use
the Graphics object that the applet container passes to paint.
• The first argument to drawString is string to draw.
• The last two arguments in the list – 25 and 25- are the x-y
coordinates as which the bottom-left corner of the string should
applet.
• Drawing methods from class Graphics require coordinates to specify
where to draw.
0, 0
+x
X axis
x, y
Y axis
• Compiling and Executing WelcomeApplet:
• Many HTML elements are delimited by the pairs of tags. All HTML
tags begins with a left angle bracket.
• HTML code:
– <html>
– <applet code = "WelcomeApplet.class" width = "300" height = "45">
– </applet>
– </html>
• Save the file with the filename.html (WelcomeApplet.html)
• The applet viewer understands only the <applet> and </applet> HTML tags,
so it is sometimes referred to as the “minimal browser”. The appletviewer is
and ideal place to test an applet and ensure that it executes properly. Once
the applet;s execution is verified, you can add the applets’ HTML tags to
and HTML document that will be viewed by people browsing the Internet.
• appletviewer WelcomeApplet.html
• The applet viewer uses and HTML document to load an applet. This is
different from the java interpreter for application, which requires only the
class name of the application class. Also, the receding command must be
issued from the directory in which the HTML document and applet’s .class
file are located.
Drawing Strings and Lines:
• Let us consider another applet. An applet can draw
Welcome to Java Programming several ways. For
example, an applet can call method drawString twice in
method paint to display multiple lines of text. The HTML
document to load this applet into an applet container.
• Example:
• File Name: Lines.java
import java.awt.Graphics;
import javax.swing.JApplet;
public class Lines extends JApplet
{
public void paint(Graphics g)
{
super.paint(g); // Call super class version of paint
g.drawLine(15,10,210,10);
g.drawLine(15,30,210,30);
g.drawString("Welcome to Applet ",25,25);
}
}
File-Name SimpleApplet.java
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.pink);
g.drawLine(10,10,150,15);
g.drawString("A Simple Applet", 20, 20);
}
}