APPLET PROGRAMMING and GRAPHICS

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

APPLET

PROGRAMMING

1
Objectives

• Introduction To Applet

• Web Browser Accessing A Web Page

• Advantages & Disadvantages of Applet

• Introduction To AWT

• Identify various stages of an applet life cycle

• Create Applets in Java

• Identify various graphic methods in Java

2
Introduction to Applet

What is an Applet?

According to Sun:-
"An applet is a small program that is
intended not to be run on its own,
but rather to be embedded inside
another application....

• An applet is a Java program that runs on a web page.

• An applet is compiled on one computer and can run on another


computer through a Java enabled Web browser.

• appletviewer is a program that can run an applet.

3
Web Browsers Accessing a Web Page

Request from the


Internet web browser to web
Web Server server

HTML files,
applets,
images

User enters URL for a web site …………..


4
Web Server Sending Response To Web Browser

Internet
Web Server
HTML file Response from the
web server
HTML file

HTML files,
applets,
images

HTML file embedded with applet tag


sent back from server
5
Web Server Sending Response To Web Browser

Internet
Web Server
HTML file Response from the
web server
HTML file

HTML files,
applets,
images

Applet bytecode sent by the server is


interpreted by the browser
6
Introduction to Applet (Continued)

Advantages of an Applet
• It is simple to make it work across platform.
• It runs in a sandbox, so the user does not need to trust the code, so
it can work without security approval.
• It is supported by most web browsers.
• It will cache in most web browsers, so will quick to load when
returning to a web page.
• The applet can work on all installed versions of Java rather than just
the latest plug – in version only.
• It can improve with use: after a first applet is run, the JVM is already
running and starts quickly, benefiting regular users of Java.

7
Introduction to Applet (Continued)

Disadvantages of an Applet
• It requires the default plug-in, which isn’t available by default on all
web browsers.
• It can’t startup until the JVM is running, and this may have
significant startup time the first time it is used.
• It is considered to difficult to build and design a good user interface
with than with HTML-based technologies.

8
Introduction To AWT

• AWT stands for Abstract Windowing Toolkit. It is a package in Java


that provides support for building GUI.

• The name of the package is java.awt.

• The API of the AWT package consists of a collection of classes and


methods that enables you to design and manage the Graphical User
Interface (GUI) applications.

• The AWT package supports applets, which help in creating containers,


such as frames or panels that run in the GUI environment.

9
Life Cycle of an Applet

• init
 To initialize the applet each time it's loaded (or reloaded).
• start
 To start the applet's execution, such as when the applet's loaded or
when the user revisits a page that contains the applet.
• stop
 To stop the applet's execution, such as when the user leaves the
applet's page or quits the browser.
• destroy
 To perform a final cleanup in preparation for unloading.

Not every applet needs to override every one of these methods.

10
Life Cycle of an Applet (Continued)
Initialization an When the applet is opened for the first time.
Applet using init()

Starting an Applet When the applet receive focus or when it is revisited.


using start()

Stopping an Applet When the applet lost focus or when the user leaves the
using stop() page.

Destroying an
Applet using When an applet is destroyed.
destroy()

11
Security Issues in Context to Applet

• Every browser implements security policies to keep applets from


compromising system security.
• Current browsers impose the following restrictions on any applet that is loaded
over the network:
 An applet cannot load libraries or define native methods.
 It cannot ordinarily read or write files on the host that's executing it.
 It cannot make network connections except to the host that it came from.
 It cannot start any program on the host that's executing it.
 It cannot read certain system properties.
 Windows that an applet brings up look different than windows that an
application brings up.

12
Creating Applet in Java

Steps To Create An Applet


1. Create a Java program that will be extended from java.applet.Applet
class.

2. Compile the java program.

3. Create a web page that contains an applet.

4. Run the applet.

13
Creating Applet in Java (Continued)

Steps To Create An Applet


1. Create a Java program that will be extended from java.applet.Applet
class.

import java.awt.*;
import java.applet.Applet; IsIsa an
method
objectof
that
Graphics
holds
public class MyApplet extends Applet
class,
information
use to
{ about
draw letters
painting
in
public void paint(Graphics g) an applet
It remembers
{ window
what color,
g.drawString(“Welcome To IBM”,50,50); font you are
} using.
}

14
Creating Applet in Java

Steps To Create An Applet


2. Compile the java program

C:\> javac MyApplet.java

15
Creating Applet in Java

Steps To Create An Applet


3. Create a web page that contains an applet

C:\> edit MyPage.htm

16
Creating Applet in Java

Steps To Create An Applet


3. Create a web page that contains an applet

<html>
<body>
<applet code=“MyApplet.class” width=200 height=200></applet>
</body>
</html>

17
Creating Applet in Java

Steps To Create An Applet


4. Run the applet.

C:\> appletviewer MyPage.htm

18
Output in Appletviewer window

19
Output in Web Browser window

20
Attributes of Java Applet Tag
<HTML> <HEAD> </HEAD>
<BODY>
<APPLET
CODE
CODEBASE
HEIGHT
WIDTH
VSPACE
HSPACE
ALIGN
ALT
<PARAM NAME=“parameter_name” VALUE=“value of the parameter”>
</APPLET>
</BODY>
</HTML>

21
Attributes of Java Applet Tag

• CODE and CODEBASE

 The CODE attribute is used to indicate the name of the class file
that holds the current Java applet.
 The CODE attribute is used when both the .class file and the
.html file are located in the same directory.
 The CODEBASE attribute indicates the pathname where the
.class file is stored.
 The CODEBASE attribute is used, if you store a java file in a
directory different from an HTML file.

22
Attributes of Java Applet Tag
<HTML> <HEAD> </HEAD>
<BODY>
<APPLET
CODE
CODEBASE
HEIGHT
WIDTH
VSPACE
HSPACE
ALIGN
ALT
<PARAM NAME=“parameter_name” VALUE=“value of the parameter”>
</APPLET>
</BODY>
</HTML>

23
Attributes of Java Applet Tag

• HEIGHT
 Use to specify the height of the applet display area.
 The value may be given in pixels or as a percentage of the
parent element's height.

• WIDTH
 Use to specify the width of the applet display area.
 The value may be given in pixels or as a percentage of the
parent element's width.

24
Attributes of Java Applet Tag
<HTML> <HEAD> </HEAD>
<BODY>
<APPLET
CODE
CODEBASE
HEIGHT
WIDTH
VSPACE
HSPACE
ALIGN
ALT
<PARAM NAME=“parameter_name” VALUE=“value of the parameter”>
</APPLET>
</BODY>
</HTML>

25
Attributes of Java Applet Tag

• HSPACE
 The HSPACE attribute specify the horizontal space between an
applet and the text.
 The HSPACE attribute controls the space to the left and right
side of an applet.

• VSPACE
 The VSPACE attribute specify the vertical space between an
applet and the text.
 The VSPACE attribute controls the space above and below an
applet.

26
Attributes of Java Applet Tag
<HTML> <HEAD> </HEAD>
<BODY>
<APPLET
CODE
CODEBASE
HEIGHT
WIDTH
VSPACE
HSPACE
ALIGN
ALT
<PARAM NAME=“parameter_name” VALUE=“value of the parameter”>
</APPLET>
</BODY>
</HTML>

27
Attributes of Java Applet Tag

• ALIGN
 The ALIGN attribute specifies the alignment of an applet.
 You can specify various values to the ALIGN attribute, such as
LEFT, RIGHT, BOTTOM, TOP, BASELINE, MIDDLE, TEXTTOP,
ABSBOTTOM, and ABSMIDDLE.

28
Attributes of Java Applet Tag
<HTML> <HEAD> </HEAD>
<BODY>
<APPLET
CODE
CODEBASE
HEIGHT
WIDTH
VSPACE
HSPACE
ALIGN
ALT
<PARAM NAME=“parameter_name” VALUE=“value of the parameter”>
</APPLET>
</BODY>
</HTML>

29
Attributes of Java Applet Tag

• The ALT attribute specifies the alternate text to be displayed if the


browser does not support the applet.

30
Attributes of Java Applet Tag
<HTML> <HEAD> </HEAD>
<BODY>
<APPLET
CODE
CODEBASE
HEIGHT
WIDTH
VSPACE
HSPACE
ALIGN
ALT
<PARAM NAME=“parameter_name” VALUE=“value of the parameter”>
</APPLET>
</BODY>
</HTML>

31
Attributes of Java Applet Tag
• Passing Parameters to Applets

 You pass parameters to an applet by using the <PARAM> tag. The


PARAM tag contains the NAME and VALUE attributes.

 The NAME attribute specifies the name of the parameter passed to an


applet, and the VALUE attribute specifies the value of the variable to be
passed.

 The following syntax sets the value of the color as red:


<APPLET CODE=”MyFirstApplet.class” HEIGHT = 20 WIDTH = 20>
<PARAM NAME= “color” VALUE= “Red”>
</APPLET>

32
Applet Retrieving Data From A HTML File

• Getting Parameters in Applets


 You retrieve the parameters passed to an applet by using the
getParameter() method in the Java file.

 This method accepts a String argument that indicates the


name of the parameter.

 The getParameter() method returns a String value that


specifies the corresponding value of that parameter.

33
Java’s coordinate system
Java uses (x,y) coordinate system
(0,0) is the top left corner (50, 0)

(0,20) is 20 pixels down from (0,0)

(50,0) is 50 pixels to the right of (0,0)

(20,50) is 20 pixels down from (0,0)


and 50 pixels to the right of (0,0)

(w -1, h -1) is just inside the bottom


right corner, where w is the the width
of the window and h is its height

34
More on java.awt.Graphics class

Graphics Class Methods

1.
drawLine(x1,y1,x2,y2)
Used to draw lines.

(x1,y1) (x2,y2)

35
More on java.awt.Graphics class

Graphics Class Methods (Continued)

2. drawOval(x,y,width,height)

Used to draw oval or circle shapes.

(starting x
coordinate, starting y height
coordinate)

width

36
More on java.awt.Graphics class

Graphics Class Methods (Continued)

3. fillOval(x,y,width,height)

Used to draw oval or circle shapes with the current color.

(starting x
coordinate, starting y height
coordinate)

width

37
More on java.awt.Graphics class

Graphics Class Methods (Continued)

4. drawRect(x,y,width,height)

Used to draw rectangle or square shapes.

(starting x
oordinate, starting y
coordinate) height

width

38
More on java.awt.Graphics class

Graphics Class Methods (Continued)

5. fillRect(x,y,width,height)

Used to draw filled rectangle or square shapes with the current color.

(starting x
coordinate, starting y
coordinate)
height

width

39
More on java.awt.Graphics class

Setting a Color

• setColor() – is used to set a color for the graphics object.


• The Graphics class object will remember the color setting until
and unless you are changing it.
import java.applet.Applet;
import java.awt.*;
public class MyApplet extends Applet { Color is a class
Sample having some
public void paint(Graphics g) {
program static fields like
using
g.setColor(Color.BLUE); blue, green,
setColor()
g.drawString(“Welcome To IBM”,50,50); red etc.
method
g.setColor(new Color(255,0,0));
g.drawLine(50,52,142,52);
}
}
40
More on java.awt.Graphics class

Setting a Font

• Font class is used to define the properties of a font.


• Font object has a:
Font Name – Arial, Times New Roman, SansSerif
Font Style – Normal, Bold, Italic
Font Size - 12 point = normal size
• setFont() – is used to set a font for the graphics object.
• The Graphics class object will remember the font setting until
and unless you are changing it.

41
More on java.awt.Graphics class

Setting a Font Example

Sample
program
using
import java.applet.Applet;
setFont()
import java.awt.*; method
public class MyApplet extends Applet {
public void paint(Graphics g) {
g.setFont(new Font(“Arial”,Font.BOLD,15);
g.drawString(“Welcome To IBM”,50,50);
}
}

42
Exercise
Write a program in Applet to draw the shapes as given below?

43
Summary

• An Applet is a Java program that you can embed in a HTML page


and access on a Web browser.
• Java Application Program Interface (API) includes a package
java.applet, which contains the Applet class.
• Applet has limited access to system resources and prohibited access
to other systems on the network.
• All the applets are subclasses of the Applet class.
• The life cycle of an applet consists of the following methods:
 init()
 start()
 stop()
 destroy()
• You can run an applet in an appletviewer or a Web browser.

44
Summary (Continued)
• To run an applet in appletviewer, include an HTML tag as a comment.
Compile the applet using javac command and run the applet giving the
following command:
appletviewer className.java
• The parameters of an applet HTML tag are:
 CODE, CODEBASE
 WIDTH, HEIGHT
 HSPACE, VSPACE
 ALT
 <PARAM> tag
• The various drawing methods in Java are:
 drawString()
 drawLine()
 drawRect(
 drawOval()
 fillOval()
45

You might also like