Applet ppt for higher understanding education

BhanuPriya93439 6 views 20 slides Jul 08, 2024
Slide 1
Slide 1 of 20
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20

About This Presentation

Ppt on applet


Slide Content

Applet Life Cycle,
Creating Applets.

The Class Applet destroy()
init()
start()
stop()
java.applet.Applet
java.awt.Panel
2

3
The Life-Cycle of Applet
init()
Calledexactlyonceinanapplet’slife.
Calledwhenappletisfirstloaded.
Usedtoreadappletparameters,startdownloading
anyotherimagesormediafiles,orsetuptheuser
interface,etc.
start()
Called by the browser or applet viewer to inform
this applet that it should start its execution.
It is called after theinitmethod

4
Applet Life-Cycle (Cont.)
stop()
Calledbythebrowserorappletviewertoinform
thisappletthatitshouldstopitsexecution.
ItiscalledwhentheWebpagethatcontainsthis
applethasbeenreplacedbyanotherpage,and
alsojustbeforetheappletistobedestroyed.
destroy()
Calledexactlyonce.
Calledwhenthebrowserunloadstheapplet.
Usedtoperformanyfinalclean-up.

5
Applet Life-Cycle (Cont.)
init
destroystart
stop
start

Applets and applications
AnappletisaJavaprogramthatrunsonawebpage.
AJavaappletisaspecialkindofJavaprogramthatabrowser
enabledwithJavatechnologycanrun.
Anappletistypicallyembeddedinsideawebpage:
Fortestingappletatdevelopmenttime,appletviewercanbeused
torunapplet.
AnapplicationisaJavaprogramthatrunsallbyitself.
main()methodisnotinvokedonanapplet,andanappletclass
willnotdefinemain().
AppletsaredesignedtobeembeddedwithinanHTMLpage.
WhenauserviewsanHTMLpagethatcontainsanapplet,the
codefortheappletisdownloadedtotheuser'smachine.
6

The Applet class
Tocreateanapplet,wemustimporttheAppletclass.
Thisclassisinthejava.appletpackage.
TheAppletclasscontainscodethatworkswitha
browsertocreateadisplaywindow.
TheonlywaytomakeanappletistoextendApplet.
Example:
import java.applet.Applet;
public class Drawing extends Applet {
…we still need to put some code in here...
}
7

The paint method
paintdoesn’treturnanyresult.
import java.applet.Applet;
import java.awt.*;
public class Drawing extends Applet {
public void paint(Graphics g) {
…we still need to put some code in here…
}
}
8

Graphics
TheGraphicsclassistheabstractbaseclassforall
graphicscontextsthatallowanapplicationtodraw
ontocomponents.
Methods
publicvoiddrawLine(intx1,inty1,intx2,inty2)
publicvoidfillRect(intx,inty,intwidth,intheight)
publicvoiddrawRect(intx,inty,intwidth,intheight)
publicvoiddrawOval(intx,inty,intwidth,intheight)
publicvoidfillOval(intx,inty,intwidth,intheight)
publicvoiddrawString(Stringstr,intx,inty)

10
Drawing rectangles
There are two ways to draw rectangles:
g.drawRect(left,top,width,height);
g.fillRect(left,top,width,height);

11
Color
Thejava.awtpackagedefinesaclassnamedColor.
Thereare13predefinedcolors:
Javaalsoallowscolornamesinlowercase:
Color.black,Color.darkGray,etc.
Color.BLACK Color.PINK Color.GREEN
Color.DARK_GRAY Color.RED Color.CYAN
Color.GRAY Color.ORANGE Color.BLUE
Color.LIGHT_GRAY Color.YELLOW
Color.WHITE Color.MAGENTA

Java’s coordinate system
Java uses an (x, y) coordinate system
(0, 0) is the top left corner
(50, 0) is 50 pixels to the right of (0, 0)
(0, 20) is 20 pixels down from (0, 0)
(w -1, h -1) is just inside the bottom right corner,
where w is the width of the window and h is its height
12
(0, 0)
(0, 20)
(50, 0)
(50, 20)
(w-1, h-1)

Some more java.awt methods
g.drawLine(x1, y1, x2, y2);
g.drawOval(left, top, width, height);
g.fillOval(left, top, width, height);
g.drawRoundRect(left, top, width, height);
g.fillRoundRect(left, top, width, height);
g.drawArc(left, top, width, height, startAngle, arcAngle);
g.drawString(string, x, y);
13

14
The complete applet
import java.applet.Applet;
import java.awt.*;
public class Drawing extends Applet {
public void paint(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(20, 20, 50, 30);
g.setColor(Color.RED);
g.fillRect(50, 30, 50, 30);
}
}

The HTML page
You can only run an applet in an HTML page.
The HTML looks something like this:
<html>
<body>
<h1>DrawingAppletApplet</h1>
<applet code="Drawing.class"
width="250" height="200">
</applet>
</body>
</html>
15

The simplest possible applet
import java.applet.Applet;
public class TrivialApplet extends Applet { }
<applet
code="TrivialApplet.class”
width=200 height=100>
</applet>
TrivialApplet.java
TrivialApplet.html

The simplest reasonable applet
import java.awt.*;
import java.applet.Applet;
public class HelloWorld extends Applet {
public void paint( Graphics g ) {
g.drawString( "Hello World!", 30, 30 );
}
}
<html>
<applet code ="HelloWorld.class"width =“200" height = “100">
</applet>
</html>

Running the applet
Compile
javac HelloWorld.java
If no errors, bytecodes stored in HelloWorld.class
Create an HTML file
ends in .htmor .html(HelloWorld.html)
To execute an applet
appletviewer HelloWorld.html
18

Displaying Numerical values
import java.awt.*;
import java.applet.Applet;
public class abextends Applet{
public void paint(Graphics g){
int a=10, b=20;
int d=a+b;
String s= “sum:“+String.valueOf(d);
g.drawString(s, 100, 100);
}
}

Thank You
Tags