0% found this document useful (0 votes)
116 views

Introduction To Java Applets

This document provides an introduction to Java applets. It discusses that applets are programs designed to be embedded in web pages that can be downloaded and run by web browsers. Applets are more secure than applications as they are restricted from accessing resources on the local computer. The document outlines the applet lifecycle including initialization, running, display, idle, and destruction states. It also discusses how applets differ from applications in their restrictions and inability to access local resources for security. Finally, a simple example applet is provided that draws a string to demonstrate basic applet functionality.

Uploaded by

Abdifatah Said
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

Introduction To Java Applets

This document provides an introduction to Java applets. It discusses that applets are programs designed to be embedded in web pages that can be downloaded and run by web browsers. Applets are more secure than applications as they are restricted from accessing resources on the local computer. The document outlines the applet lifecycle including initialization, running, display, idle, and destruction states. It also discusses how applets differ from applications in their restrictions and inability to access local resources for security. Finally, a simple example applet is provided that draws a string to demonstrate basic applet functionality.

Uploaded by

Abdifatah Said
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

INTRODUCTION TO JAVA

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.

• A Java applet is a program designed to be embedded in a Web


page. Applets can be quite complex; they are not limited to simple
animations or single windows. They can access remote databases
or other sources of information load and operate on complex
information on your computer system.

• Java applets are designed to be quite secure, however. They can be


safely downloaded over a network without causing concern that they
might be able to do damage to your computer. They cannot write
any information onto your hard disk unless you specifically give
them access to a directory. They cannot access any other resources
on your network or any other hardware or peripherals on your
computer.
• Java applets are also restricted from writing into memory outside of the
Java address space. This is accomplished mainly because Java has no
memory pointer type and thus a malicious programmer cannot point to
memory he or she might want to attack.

• Java applets are also analyzed class by class as they are loaded by the
run-time environment in your browser.

• Further, applets cannot access resources on any other computer on your


network or elsewhere on the Internet, with one exception; they can open
TCP/IP connections back to the machine running the Web server from
which they were downloaded. To add even more protection and prevent
programmers from spoofing users into giving them confidential information,
all windows that you pop up from an applet have a banner along the bottom
reading “Unsigned Java Applet.”

• The banner serves to prevent hackers from designing an exact copy of a


familiar screen to type confidential information into it.
Java Applications
• By contrast, Java applications are full-featured programs
that run on your computer and have full access to its
resources. They can read, write, create, and delete files
and access any other computer on the network.

• Applications in Java: database viewers, word


processors, spreadsheets, math packages, and file and
network manipulation programs. In fact, one of Java’s
great strengths is that it makes accessing other
computers on your network extremely easy.
Four definitions of applet:
• A small application
• A secure program that runs inside a web browser
• A subclass of java.applet.Applet
• javax.swing.JApplet package also used to extend
Applets.
• The browser that executes an applet is generically
known as the applet container.

• The JDK includes the appletviewer applet container for


testing applets as you develop them and before you
embed them in Web pages.

• We typically demonstrate applets using the


appletviewer. If you would like to execute your applets
in a Web browser, be aware that some Web browsers do
not support J2SE 5.0 by default.
Applet: Making Web Interactive and
Application Delivery Media
1 2 3 4 5
APPLET hello.class Create Accessing The browser
Development AT SUN’S Applet from creates
“hello.java” WEB tag in Your Organisation a new
AT SERVER HTML window and
SUN.COM document a new thread
and
then runs the
code

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.

– The applet states include:

• Initialisation – invokes init()


• Running – invokes start()
• Display – invokes paint()
• Idle – invokes stop()
• Dead/Destroyed State – invokes destroy()
Applet States
• Initialisation – invokes init() – only once
– Invoked when applet is first loaded.

• Running – invokes start() – more than once


– For the first time, it is called automatically by the system after init()
method execution.
– It is also invoked when applet moves from idle/stop() state to active
state. For example, when we return back to the Web page after
temporary visiting other pages.

• Display – invokes paint() - more than once


– It happens immediately after the applet enters into the running state.
It is responsible for displaying output.

• Idle – invokes stop() - more than once


– It is invoked when the applet is stopped from running. For example, it
occurs when we leave a web page.

• Dead/Destroyed State – invokes destroy() - only once


– This occurs automatically by invoking destroy() method when we
quite the browser.
Applet Life Cycle Diagram
init()
Begin Born
stop()
start()

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

• Any kind of component can be associated with a graphics context.


The association is permanent – a context cannot be reassigned to a
new component. Although you can use graphics contexts to paint
onto any kind of component, it is unusual to do so with components
that already have an appearance. Buttons, Choices, checkboxes,
labels, scrollbars, and text components do not generally require
programmer level rendering.
• Classes of Blank component:

• There are four classes of blank components that have no default


appearance and will show up as empty rectangles unless they are
sub-classed and given paint() methods:

– Applet
– Canvas
– Frame
– Panel

• The four major operations provided by the Graphics class are:

– Selecting a color
– Selecting a font
– Drawing and filling
– Clipping

Applet menu in the appletviewer:

Reload the applet to execute it


again

Select Quit to terminate the


appletviewer
Java Applets: Advantages
• 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 design and build good user interface in


applets compared to HTML technology
Simple Java Applet: Drawing a String

• Every Java applet is a graphical user interface on which you can


place GUI components using the techniques we will demonstrate
drawing on an applet.

• We begin with a simple applet that draws "Welcome to Java


Programming!" on the applet. nFollowing figure shows this applet
executing in two applet containers the appletviewer and the
Microsoft Internet Explorer Web browser.
• File Name: WelcomeApplet.java

import java.awt.Graphics; // import class graphics(Abstract Window Tool kit)


import javax.swing.JApplet; // import class JApplet

public class WelcomeApplet extends JApplet


{
public void paint(Graphics g) // Draw text on Applets background
{

super.paint(g); // Call superclass version of method paint

//Draw a string at x-coordinates 25 and y-coordinate 25

g.drawString("Welcome to Java Programming!", 25,25);

} //End method paint


} //End class Welcome Applet
Line g.drawString("Welcome to Java Programming!", 25,25);

The above line does the real work of the program –


namely, drawing the string “Welcome to Java
Programming” on the screen. But we consider
each line of the program in order.
• 1. import java.awt.Graphics; //import class graphics

– is an import declaration, which indicates that the applet uses


class Graphics from package java.awt. Class Graphics enables
a Java applet to draw graphics such as line, rectangles, ovals
and strings of characters.

• 2. import javax.swing.JApplet; //import class JApplet

– Is an important declaration, which indicates that applet uses


class JApplet from package javax.swing. When you create an
applet in Java2, you import class JApplet.

– As with applications, every java applet contains at least one


public class declaration. A key feature of class declaration is that
programmers rarely create class declarations “from scratch”. In
fact when you create a class declaration, you normally use
pieces of an existing class declaration. Java uses Inheritance to
create new classes from existing class declarations.
• 3. Public class WelcomeApplet exents JApplet {

– Begins a class declaration for class WelcomeApplet. This class’ body


is delimited by the left brace,}. Keyword class introduces the class
declaration. WelcomeApplet is the class name.

– Keyword extends indicates that class WelcomeApplet inherits


existing members (data and methods) from another class. The class,
from which WelcomeApplet inherits, JApplet, appears to the right of
keyword extents.

– In this inheritance relationship, JApplet is called the super class and


WelcomeApplet is called the sub class. Using inheritance here results in
a WelcomeApplet class that has the attributes (data) and behaviors
(methods) of class JApplet as well as new features we are adding in our
WelcomeApplet class declaration (specifically, the ability to draw
Welcome to Java Programming! on the applet).

– Applet containers expect every Java applet to have certain behaviors


(methods). Class JApplet already provides all those behaviors.
• 4. public void paint (Graphics g)

• Begins the declaration of the applet’s paint method, which is on


the three applet methods, that the applet container calls when it
begins executing the applet. In order, these methods are: paint, init,
start.

• 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);

• Instructs the computer to perform an action – namely, to draw the


characters of the string Welcome to Java Programming on the applet. This
statement uses method drawString of class Graphics, which provides
drawing capabilities such as drawing strings of characters, lines,
rectangles, and ovals.

• The statement class drawString using the reference g from paint’s


parameter list followed by the dot (.) separator followed by the method
name drawString. The method name is followed by a set of parenthesis
containing arguments drawString needs to perform its task.

• 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 Java coordinate system. Units are


measures in pixels

Y axis
• Compiling and Executing WelcomeApplet:

• As with application classes, you must compile an


applet class before it can execute. After creating
class WelcomeApplet and saving it in
WelcomeApplet.java, open a command window,
change to the directory in which you saves the
applet class declaration and type the command:
• To compile and execute Applet:
• Steps:
– Go to command prompt.
– Jump into the directory where you saved the program.
– set the java path : type --- set path=C:\j2sdk1.4.2\bin
then press enter.
– Now compile the program by the typing following
command.
– javac filename.java
– When the compilation is success, now run the
program.
• - To compile class WelcomeAppplet. If there is no syntax errors, the
resulting byte codes are stored in the file WelcomeApplet.class

• - Recall that applets are embedded in Web pages for execution in


an applet container (applet viewer or a browser). Before you can
execute the applet, you must create and HTML (Hyper text markup
language) document to load the applet into the applet container.
Typically, and HTML documents ends with and “.html” or “.htm” file-
name extension. Browsers display the contents of documents that
contain text.
Executing an Applet in the appletviewer

• To execute Java applet, and HTML document must indicate which


applet the apple container, should load and execute.

• Many HTML elements are delimited by the pairs of tags. All HTML
tags begins with a left angle bracket.

• Normally the applet and corresponding HTML documents are stored


in the same directory or disk.

• When an applet container encounters and HTML document that


specifies and applet to execute, the applet container automatically
loads the applet’s .class file from the same directory on the
computer in which HTML document resides.
WelcomeApplet.html loads WelcomeApplet into an
applet container.

• 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.

• To execute WelcomeApplet in he appltviewer, open a command window,


change to the directory containing you applet and HTML document, they
type,

• 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);
}
}

You might also like