Sunteți pe pagina 1din 31

Creating a Applet Program

Problems from previous class


Int [] arr={1,2,3,4,5};
• Write a program which takes marks of five
subject and find the average of given
marks
• Calculate salary as if basic is between
5000 to 6000 then DA 10%,HRA 15% of
basic ,if basic is greater then 6000 to 8000
then DA 15%,HRA 20% of basic and if
basic is greater then 8000 then DA 20%
and HRA 25% calculate for salary five
employees
Creating a Applet Program

About Applets
– You can use applets to add dynamic aspects,
such as, animation and sound to a Web page
– Applets reduce load on the Web-server by
executing locally
Creating a Applet Program

Problem
ABC Co. wants to create programs that
are Web-based. The interface application
that was created to access customer
details now needs to be deployed on the
Internet. Change the existing application
to make it Web-based.
Creating a Applet Program

Identify the method for deploying a Java


Applet
• The JApplet Class:
– The javax.swing package contains the
JApplet class, which has over 20 methods
that are used to display images, play audio
files, and respond when you interact with it
– An applet runs in a Web page that is loaded in
a Web browser
– The life cycle of an applet is implemented
using the methods: init(), start(),
stop(), and destroy()
Creating a Applet Program

Identify the mechanism for initializing


controls in a Java Applet.
• The init() method is called the first time
an applet is loaded in the memory of a
computer
• The start() method is called
immediately after the init() method and
every time an applet receives focus
• The stop() method is called every time
an applet loses its focus
• The destroy() method is called when a
user moves to another Web page
Creating a Applet Program

Life cycle of an applet:

A user opens the


Web page for the init
first time.
When the applet receives
the focus.
start
After
init.

stop

When the applet When the user moves


loses its focus. to another Web page.
destroy
Creating a Applet Program
The java.awt.Graphics Class
– It is an abstract class that represents the
display area of an applet
– It is used for drawing on to the display area of
an applet
– It provides methods such as:
• drawString(String test, int x, int y)
• drawLine(int x1, int y1, int x2, int y2)
• drawRect(int x1, int y1, int width, int
height)
• drawOval(int x1, int y1, int width, int
height)
Creating a Applet Program
● The update() Method
● It takes a Graphics class object as a parameter
● Invoked whenever the applet area needs to be
redrawn
● It clears the screen and invokes the paint()
method
● The paint() Method
● Draws all the graphics of the applet in the drawing
area
● Automatically invoked the first time the applet is
displayed on the screen and every time the applet
receives focus
Creating a Applet Program
The paint() Method
● Painting can be triggered by invoking the
repaint() method
● Example
import javax.swing.*;
import java.awt.*;
public class DisplayApplet extends JApplet {
public void paint(Graphics g)
{
g.drawString(“This is displayed by
the paint method”, 20, 20);
}
}
Creating a Applet Program
The paint()
● Output
Creating a Applet Program

● As init() is the first method to be executed, this


method can be used to initialize the controls in a
Web-based Java program
Creating a Applet Program
Modifying the existing application code
import javax.swing.*;
public class Customer extends JApplet
{
JPanel panelObject;
/* Variables for labels and data entry
controls */
public void init() {
panelObject=new JPanel();
getContentPane().add(panelObject);
/* Initialise and add the controls to
the panel */
}
}
Creating a Applet Program

Then save and compile the program


Creating a Applet Program

Code the HTML file


• The Applet Tag:
– The APPLET tag is written within the BODY tag
of an HTML document inside a class
– Elements of the Applet tag:
• CODE="name of the .class file”
• CODEBASE="path of the .class file”
• HEIGHT=“maximum height of the applet, in pixels”
• WIDTH=“maximum width of the applet, in pixels”
Creating a Applet Program

• VSPACE=“vertical space between the applet and the rest of


the HTML, in pixels”
• HSPACE=“horizontal space between the applet and the
rest of the HTML, in
• pixels”ALIGN=“alignment of the applet with respect to the
rest of the Web page”
• ALT="alternate text to be displayed if the browser does not
support applets"
– The most commonly used attributes of the APPLET tag
are CODE, HEIGHT, WIDTH, CODEBASE, and ALT
Creating a Applet Program

Code:

<HTML>
<APPLET
CODE = "Customer.class"
HEIGHT = 300
WIDTH = 300
>

</APPLET>
</HTML>
Creating a Applet Program

– Save the file as <drive:>\Java\Customer.html


Creating a Applet Program

• The appletviewer Tool:


– The appletviewer tool is a program that lets
run applets without the overhead of running a
Web browser
• Action:
– At the command prompt, type AppletViewer
Customer.html
Creating a Applet Program

Problem Statment
• The application that has been created to
capture the details of dealers cannot be
deployed on the local intranet of the
company. Change the existing application
to make it Web-based. A frame with the
dealer details entered, should be
displayed when the applet is closed.
Creating a Applet Program

The logo of the company has to be


displayed on the applet that displays
customer details.
Creating a Applet Program

Identify the type, location, and name of


the image
• An image is a visual imitation or representation of
a person or a thing
• Graphic Interchange Format (GIF) is the de facto
standard for the images on the Web. This format
is used if an image is a line drawing
• The Joint Photographic Experts Group (JPEG)
format is used if an image is a photograph, a
medical image, or a complex photographic
illustration
• Java supports both GIF and JPEG image formats
Creating a Applet Program

Properties of an image are:


– Height
– Width
– Image format
– Location – An image displayed in an applet
can reside either on the local file system or on
the remote file system
– Dimension – The image can be either 2D or
3D
Creating a Applet Program

– As the GIFformat is superior to JPEG, the type


of the image is .GIF
– The location of the image is
<drive:>\S\Groupname\ABC\Images
– The name of the image is ABC.gif
Creating a Applet Program

Identify the permissions to be assigned


to the image
• Permissions in JDK:
– Permissions are the rights provided for any
system resource like a file, the network, and
AWT
– In order to access any resource from an
applet, permission has to be granted explicitly
– The permissions that can be granted are read,
write, execute, and delete
Creating a Applet Program

• The Policy File in Java:


– In Java, a policy object specifies the
permissions available on the resources, such
as read, write, or execute, in the application
environment
– The default policy for a user is kept in the
.java.policy file in the user’s home
directory
• Result:
– Grant the Read and Execute permissions
Creating a Applet Program

The controls required and their variable


names
• Controls Required for Displaying
Images:
– The controls required for displaying an image
are a label and an icon
• The Icon Interface:
– The Icon interface defines the methods that
are implemented by the classes that provide
application icons
• The ImageIcon Class
– The ImageIcon class implements the
methods defined in the Icon interface
Creating a Applet Program

– The ImageIcon constructor takes the name


of an image file as its argument
• So we need
– Icon logoImage
– JLabel logoimagePosition
Creating a Applet Program

Create the policy


• Policies can be created using the
PolicyTool utility of JDK 1.2. Use this
utility to set permissions for resources
• Action
– Open the command prompt window
– Type PolicyTool and press Enter
– In the PolicyTool dialog box, click the Add
Policy Entry button
– Click the Add Permission button
– Click the Permission drop-down list and select
FilePermission
Creating a Applet Program

– Specify the Target Name entry as ABC.gif


– Select read and execute from the Action list
– Click OK
– Click Done
– Select Save As from the File menu
– Save the java.policy file in the
Winnt\Profiles\<Username> directory
Creating a Applet Program

The code to insert an image


import javax.swing.*;
public class Customer extends JApplet {
/* Variables for panel, labels, and
data entry controls */
JLabel logoimagePosition;
public void init()
{
Icon logoImage=new
ImageIcon(“<path>\ABC.gif”);
logoimagePosition=new
JLabel(logoImage);

panelObject.add(logoimagePosition);
}
}
Creating a Applet Program

Save and compile the program


Execute the program

S-ar putea să vă placă și