Documente Academic
Documente Profesional
Documente Cultură
Creating Applets.
java.applet.Applet
destroy()
init()
start()
stop()
2
The Life-Cycle of
Applet
init()
Called exactly once in an applets life.
Called when applet is first loaded.
Used to read applet parameters, start downloading
any other images or media files, or setup the user
interface, etc.
start()
Called by the browser or applet viewer to inform this
applet that it should start its execution.
It is called after the init method
Applet Life-Cycle
(Cont.)
stop()
Called by the browser or applet viewer to inform
this applet that it should stop its execution.
It is called when the Web page that contains this
applet has been replaced by another page, and
also just before the applet is to be destroyed.
destroy()
Called exactly once.
Called when the browser unloads the applet.
Used to perform any final clean-up.
Applet Life-Cycle
(Cont.)
init
start
stop
destroy
start
class.
This class is in the java.applet package.
Graphics
TheGraphicsclass is the abstract base class for all
void
drawOval(intx,
inty,
intwidth,
intheight)
public void fillOval(intx, inty, intwidth, intheight)
public void drawString(String str, intx, inty)
Drawing rectangles
There are two ways to draw rectangles:
g.drawRect( left , top , width , height );
10
Color
The java.awt package defines a class named Color.
There are 13 predefined colors:
Color.BLACK
Color.DARK_GRAY
Color.GRAY
Color.LIGHT_GRAY
Color.WHITE
Color.PINK
Color.RED
Color.ORANGE
Color.YELLOW
Color.MAGENTA
Color.GREEN
Color.CYAN
Color.BLUE
11
(50, 0)
(0, 20)
(50, 20)
(w-1, h-1)
arcAngle);
g.drawString(string, x, y);
13
14
TrivialApplet.java
import java.applet.Applet;
public class TrivialApplet extends Applet { }
TrivialApplet.html
<applet
code="TrivialApplet.class
width=200 height=100>
</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>
javac HelloWorld.java
If no errors, bytecodes stored in HelloWorld.class
To execute an applet
appletviewer HelloWorld.html
18
Displaying Numerical
values
import java.awt.*;
import java.applet.Applet;
public class ab extends 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