Beruflich Dokumente
Kultur Dokumente
JAVA PROGRAMMING
Prepared by Harsha Chauhan
Java Applet
AJava appletis a special kind ofJava program
java.applet. Appletclass.
Java Applet
Advantage of Applet
There are many advantages of applet. They are as
follows:
It works at client side so less response time.
Secured
It can be executed by browsers running under many
platforms, including Linux, Windows, Mac Os etc.
Drawback of Applet
Plugin is required at client browser to execute applet.
Java Applet
There are some important differences
between an applet and a standalone
Java application, including the following:
An applet is a Java class that extends the
java.applet.Applet class.
A main() method is not invoked on an applet, and
an applet class will not define main().
Applets are designed to be embedded within an
HTML page.
When a user views an HTML page that contains an
applet, the code for the applet is downloaded to
the user's machine.
Cond.
Java Applet
contd.
Local Applets
Local Applets
code="TicTacToe.class"
width=120
height=120>
</applet>
thecodebaseattribute specifies a path name on your
system for the local applet, whereas thecodeattribute
specifies the name of the byte-code file that contains the
applet's code. The path specified in thecodebaseattribute
is relative to the folder containing the HTML document that
references the applet.
Prepared by Harsha Chauhan
Remote Applet.
A remote applet is one that is located on another
computer system .
This computer system may be located in the
building next door or it may be on the other side of
the world-it makes no difference to your Javacompatible browser.
No matter where the remote applet is located, it's
downloaded onto your computer via the Internet.
Your browser must, of course, be connected to the
Internet at the time it needs to display the remote
applet.
Remote applets are stored on another system and ar
e down-loaded onto your computer via the Internet.
Prepared by Harsha Chauhan
10
Remote Applet.
Contd
11
code="TicTacToe.class"
width=120
height=120> </applet>
thecodebaseattribute specifies a path name on your
system for the local applet, whereas
thecodeattribute specifies the name of the bytecode file that contains the applet's code. The path
specified in thecodebaseattribute is relative to the
folder containing the HTML document that references
the applet.
Prepared by Harsha Chauhan
12
Application
Small Program
Large Program
contd
Application
Example:
importjava.awt.*;
importjava.applet.*;
public class MyClass
public
{
classMyclassextends
public static void
Applet
main(String args[])
{ publicvoidinit(){ }
{
publicvoidstart(){ }
}
publicvoidstop(){}
}
publicvoiddestroy()
{}
Prepared by Harsha Chauhan
publicvoidpaint(Grap
14
15
16
17
18
contd..
19
20
init():
The applet's voyage starts here. In this
method, the applet object is created by the
browser. Because this method is called before
all the other methods, programmer can utilize
this method to instantiate objects, initialize
variables, setting background and foreground
colors in GUI etc.; the place of a constructor in
an application. It is equivalent toborn state
of a thread.
Prepared by Harsha Chauhan
21
22
23
24
25
26
//First.java
importjava.applet.Applet;
importjava.awt.Graphics;
publicclassFirstextendsApplet{
publicvoidpaint(Graphicsg){
g.drawString("welcome",150,150);
}
}
Prepared by Harsha Chauhan
27
28
29
30
31
32
33
34
Applet methods
public void init ()
public void start ()
public void stop ()
public void destroy ()
public void paint (Graphics)
Also:
public void repaint()
public void update (Graphics)
public void showStatus(String)
public String getParameter(String)
Prepared by Harsha Chauhan
35
36
37
38
39
40
41
42
repaint( )
Call repaint( ) when you have
changed something and want your
changes to show up on the screen
repaint( ) is a request--it might not
happen
When you call repaint( ), Java
schedules a call to update(Graphics g)
Prepared by Harsha Chauhan
43
update( )
When you call repaint( ), Java
schedules a call to update(Graphics g)
Here's what update does:
public void update(Graphics g) {
// Fills applet with background color,
then
paint(g);
}
44
45
publicclassGraphicsDemoextendsApplet{
publicvoidpaint(Graphicsg){
g.setColor(Color.red);
g.drawString("Welcome",50,50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
}}
Prepared by Harsha Chauhan
48
myapplet.html
<html>
<body>
<appletcode="GraphicsDemo.
class"width="300"height="30
0">
</applet>
</body>
</html>
Prepared by Harsha Chauhan
49
Parameter in Applet
We can get any information from the
HTML file as a parameter. For this
purpose, Applet class provides a
method named getParameter().
Syntax:
publicStringgetParameter(Stringparam
eterName)
50
publicclassUseParamextendsApplet
{
publicvoidpaint(Graphicsg){
Stringstr=getParameter("msg");
g.drawString(str,50,50);
}
}
51
myapplet.html
<html>
<body>
<appletcode="UseParam.class"width=
"300"height="300">
<paramname="msg"value="Welcome
toapplet">
</applet>
</body>
</html>
52
Thank you
55