Download - Advanced Java Lecture-6
-
8/10/2019 Advanced Java Lecture-6
1/39
-
8/10/2019 Advanced Java Lecture-6
2/39
Applets are small java programs that are primarily used in
internet computing
They can be transported over the internet from one
computer to another and run usingApplet Viewer or any
web browser that supports java
-
8/10/2019 Advanced Java Lecture-6
3/39
When to use applets??
When we need something dynamic to be
included in the display of a web page
When we require some flash outputs
When we want to create a program and make it
available on internet for use by others on their
computers
-
8/10/2019 Advanced Java Lecture-6
4/39
Types
Local applet
Remote applet
-
8/10/2019 Advanced Java Lecture-6
5/39
Local Applet
Internet
Remote appletLocal Computer
(client)
Remote Computer
(server)
Local Computer
-
8/10/2019 Advanced Java Lecture-6
6/39
We can embed applets into web pages in 2 ways:
We can write our own applets and embed them
into web pages
We can download an applet from a remote
computer system and then embed it into a web
page
-
8/10/2019 Advanced Java Lecture-6
7/39
-
8/10/2019 Advanced Java Lecture-6
8/39
Applet Security
For security reasons the applets that are loaded
over the network have several restrictions:
An applet cannot ordinarily read or write fileson the computer that its executing on
An applet cannot make network connections
except to the host that it came from
-
8/10/2019 Advanced Java Lecture-6
9/39
Java.lang.Object
Java.awt.Component
Java.awt.Panel
Java.awt.Container
Java.applet.AppletChain of classes inherited by
Applet class
-
8/10/2019 Advanced Java Lecture-6
10/39
The Applet CLASS: Every applet is an extension of thejava.applet.Applet class.
The base Applet class provides methods that a derived Applet
class may call to obtain information and services from the
browser context. These include methods that do the
following:
Get applet parameters
Get the network location of the HTML file that contains the
applet
Get the network location of the applet class directory
Print a status message in the browser
Fetch an image
Fetch an audio clip
Play an audio clip
Resize the applet
-
8/10/2019 Advanced Java Lecture-6
11/39
-
8/10/2019 Advanced Java Lecture-6
12/39
12
Useful Applet Methods
getCodeBase, getDocumentBase
The URL of the:
Applet file- getCodeBase
HTML file- getDocumentBase
getParameter Retrieves the value from the associated HTML PARAMelement
getSize
Returns the Dimension(width, height) of the applet
showStatus
Displays a string in the status line at the bottom of the browser
-
8/10/2019 Advanced Java Lecture-6
13/39
13
Useful Applet Methods,
(Continued)
getBackground, setBackground
Gets/sets the background color of the applet
SystemColorclass provides access to desktop colors
getForeground, setForeground
Gets/sets foreground color of applet (default color of drawing
operations)
-
8/10/2019 Advanced Java Lecture-6
14/39
14
Useful Graphics Methods
drawString(string, left, bottom)
Draws a string in the current font and color with the bottom leftcornerof the string at the specified location
One of the few methods where the y coordinate refers to the bottomof shape, not the top. But y values are still with respect to the top left
corner of the applet window
drawRect(left, top, width, height)
Draws the outline of a rectangle (1-pixel border) in the current color
fillRect(left, top, width, height)
Draws a solid rectangle in the current color
drawLine(x1, y1, x2, y2)
Draws a 1-pixel-thick line from (x1, y1) to (x2, y2)
-
8/10/2019 Advanced Java Lecture-6
15/39
15
Useful Graphics Methods,
continued drawOval, fillOval
Draws an outlined and solid oval, where the arguments describe a
rectangle that bounds the oval
drawPolygon, fillPolygon
Draws an outlined and solid polygon whose points are defined by
arrays or a Polygon(a class that stores a series of points)
By default, polygon is closed; to make an open polygon use the
drawPolyline method
drawImage Draws an image
Images can be in JPEG or GIF (including GIF89A) format
-
8/10/2019 Advanced Java Lecture-6
16/39
16
Graphics Color
setColor, getColor
Specifies the foreground colorprior to drawing operation
By default, the graphics object receives the foregroundcolor of the window
AWT has 16 predefined colors (Color.red,Color.blue, etc.) or create your own color,new Color(r, g, b)
Changing the color of the Graphicsobject affects only
the drawing that explicitly uses that Graphicsobject To make permanent changes, call the appletssetForeground
method.
-
8/10/2019 Advanced Java Lecture-6
17/39
17
Graphics Font
setFont, getFont
Specifies the font to be used for drawing text
Set the font of the window (I.e., call the appletssetFontmethod)
for permanent changes to the Graphicsobject
In JDK 1.1, only 5 fontsare available: Serif (akaTimesRoman),SansSerif (aka Helvetica), Monospaced (akaCourier),
Dialog, and DialogInput
-
8/10/2019 Advanced Java Lecture-6
18/39
Applet life cycle
Born
IdleRunning
Dead
stop()
start()
destroy()
destroyedEnd
Exit of browser
start()
InitializationBegin
(Load Applet)
paint()
Display
stopped
-
8/10/2019 Advanced Java Lecture-6
19/39
Initialization state
Applet enters this state when it is first loaded.We may do following at this stage:
create objects needed by applet
set up initial valuesload images or fonts
set up colors
public void init()
{
(action)
}
-
8/10/2019 Advanced Java Lecture-6
20/39
Running state
Applet enters this state when the system calls start()
method of Appletclass
This method is automatically called after the browser
calls the init method. It is also called whenever the user
returns to the page containing the applet after havinggone off to other pages
public void start()
{ ....
.. (action)
}
-
8/10/2019 Advanced Java Lecture-6
21/39
Idle or stopped state
This method is automatically called when the user
moves off the page on which the applet sits. It can,
therefore, be called repeatedly in the same applet.
public void stop()
{
(action)
}
-
8/10/2019 Advanced Java Lecture-6
22/39
Dead state
An applet is said to be dead when it is removed frommemory.
This method is only called when the browser shuts downnormally. Because applets are meant to live on an HTMLpage, you should not normally leave resources behind
after a user leaves the page that contains the applet.
public void destroy()
{
(action)
}
-
8/10/2019 Advanced Java Lecture-6
23/39
Display state
Applet moves to this state whenever it has to perform
some output operations on the screen
Invoked immediately after the start() method, and alsoany time the applet needs to repaint itself in the browser.The paint() method is actually inherited from the
java.awt.
public void paint (Graphics g)
{
....
.. (display statements)
}
-
8/10/2019 Advanced Java Lecture-6
24/39
24
Graphics Behavior
Browser calls repaintmethod to request redrawing ofapplet
Called when applet first drawn or applet is hidden by anotherwindow and then re-exposed
repaint()
update(Graphics g)
paint(Graphics g)
sets flag
Clears screen, Calls paint
-
8/10/2019 Advanced Java Lecture-6
25/39
Steps involved in developing and
testing an applet
Building an applet code (.java file)
Creating an executable applet (. Class file)
Designing a web page using HTML tags
Preparing tag
Incorporating tag into the web page
Creating HTML file
Testing the applet code
-
8/10/2019 Advanced Java Lecture-6
26/39
1. Building applet code
Import java.awt.*;
Import java.applet.*;.
public class appletclassname extends Applet
{
..
..public void paint(Graphics g)
{
..
..
}
}
-
8/10/2019 Advanced Java Lecture-6
27/39
Example
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void paint(Graphics g)
{
g.drawString(hello world, 10,100);}
}
-
8/10/2019 Advanced Java Lecture-6
28/39
2. Creating an executable applet
Move to the directory containing the source code
and type following command
javac HelloJava.java Compiled output file called HelloJava.class is placed
in same directory as source
If any error msg is received, then we must check for
errors, correct them and compile the applet again
3 D i i b
-
8/10/2019 Advanced Java Lecture-6
29/39
3. Designing a web page
>
Title Tag
Applet Tag
Comment
section
Head
section
Body
section
-
8/10/2019 Advanced Java Lecture-6
30/39
welcome to world of applets
-
8/10/2019 Advanced Java Lecture-6
31/39
4. Applet tag
CODE=hellojava.class
WIDTH=400HEIGHT=200>
ddi l fil
-
8/10/2019 Advanced Java Lecture-6
32/39
5. Adding applet to HTML file
welcome to java applets
welcome to world of applet
CODE=hellojav.class
WIDTH=400HEIGHT=200>
-
8/10/2019 Advanced Java Lecture-6
33/39
More about Applet tag
Code= AppletFileName.class
Codebase=codebase_url
Width=pixels
Height=pixels
Name= applet_instance_name
Align=Alignment
Hspace=pixels
Vspace=pixels
-
8/10/2019 Advanced Java Lecture-6
34/39
Passing parameters to applets
Eg.
-
8/10/2019 Advanced Java Lecture-6
35/39
import java.awt.*;
import java.applet.*;//
public class HelloJavaParam extends Applet{
String str;
public void init()
{
str=getParameter(string); // receiving parameter value
if(str==null)
str=java;
str=hello + str; // using the value
}public void paint(Graphics g)
{
g.drawString(str,10,100);
}
}
Di l i i l l
-
8/10/2019 Advanced Java Lecture-6
36/39
Displaying numerical valuesimport java.awt.*;
import java.applet.*;public class NumValues extends Applet
{
public void paint(Graphics g)
{ int value1=10;
int value2=20;
int sum=value1+value2;
String s=sum: + String.valueOf(sum);g.drawString(s,10,100);
}
}
Getting input from user
-
8/10/2019 Advanced Java Lecture-6
37/39
Getting input from user
import java.awt.*;
import java.applet.*;public class UserIn extends Applet
{
TextField text1,text2;
public void init()
{
text1=new TextField(8);
text2=new TextField(8);
add(text1);
add(text2);text1.setText(0);
text2.setText(0);
}
Contd
-
8/10/2019 Advanced Java Lecture-6
38/39
Contdpublic void paint(Graphics g)
{
int x=0,y=0,z=0;
String s1,s2,s;g.drawString ( input a no in each box , 10,50);
try
{
s1=text1.getText();
x=Integer.parseInt(s1);
s2=text2.getText();
y=Integer.parseInt(s2);
}
catch(Exception e) { }
z=x+y;
s=String.valueOf(z);g.drawString(the sum is: , 10,75);
g.drawString(s,100,75);
}
}
-
8/10/2019 Advanced Java Lecture-6
39/39