Sunteți pe pagina 1din 20

Applet Life Cycle,

Creating Applets.

The Class Applet


java.awt.Panel

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

Applets and applications


An applet is a Java program that runs on a web page.
A Java applet is a special kind of Java program that a

browser enabled with Java technology can run.


An applet is typically embedded inside a web page:

For testing applet at development time, appletviewer can

be used to run applet.

An application is a Java program that runs all by itself.


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.
6

The Applet class


To create an applet, we must import the Applet

class.
This class is in the java.applet package.

The Applet class contains code that works with a

browser to create a display window.


The only way to make an applet is to extend Applet.
Example:
import java.applet.Applet;
public class Drawing extends Applet {
we still need to put some code in here...
}
7

The paint method


paint doesnt return any result.
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
}
}

Graphics
TheGraphicsclass is the abstract base class for all

graphics contexts that allow an application to draw


onto components.
Methods
public void drawLine(intx1, inty1, intx2, inty2)
public void fillRect(intx, inty, intwidth, intheight)
public void drawRect(intx, inty, intwidth, intheight)
public

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 );

g.fillRect(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

Java also allows color names in lowercase:


Color.black, Color.darkGray, etc.

11

Javas coordinate system


(0, 0)

(50, 0)

(0, 20)

(50, 20)

(w-1, h-1)

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

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

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);
}
}

14

The HTML page


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

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>

Running the applet


Compile

javac HelloWorld.java
If no errors, bytecodes stored in HelloWorld.class

Create an HTML file

ends in .htm or .html(HelloWorld.html)

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

S-ar putea să vă placă și