Sunteți pe pagina 1din 22

Applets

Part of the AWT Class Hierarchy


Object

Component

Button

Checkbox
TextComponent

TextArea

TextField

Choice

List
Container

Panel

Window

ScrollPane

Applet
Dialog

Label
Canvas
ScrollBar

Frame

Netscape(JVM)
Loads HTML file
Loads Java Applet

TCP/IP
(socket)

TCP/IP
(HTTP)

Client

Internet

Java Security Model

TCP/IP
(HTTP)
TCP/IP
(socket)

Sambar Server
HTML files(Applet Tags)
Applets

Web Server

Java Security Model


Restricted Access to Local File System
Restricted Access to Outside Servers
Restricted Access to Threads

Java Applet

public class className extends java.applet.Applet


{
public void init() {}
public void paint(Graphics g) {}
}

Java Applet
Applets will have the following methods:
public void init ( ) {

//in here you will

//set fonts may be done in paint instead


//set initial background and foreground collors
//initialize attributes (if other than their defaults)
//add components and/or event listeners
}
public void actionListenerType (EventType e) {
//needed
for event driven applets may occur in a component subclass
//
//do some action that will change the applet display
repaint();
}
public void paint(Graphics g) {
//only implicitly called by repaint
}

Additional Applet Methods


public void start ( ) {
//called after init and before the first invocation of paint
//if applet is stopped and restarted, start is called
//Netscape calls start and stop when the browser window is resized
//IE and appletviewer dont
}
public void stop() {
//called when user leaves page containing the applet
}
public void destroy() {
//called when applet is about to be permanently destroyed
//when browser shuts down or when ir removes the applet from memory
//Called by IE when user leaves the page containing the applet
}

HelloWorldApplet1.java
import
import
import
import

java.awt.Graphics;
java.awt.Font;
java.awt.Color;
java.applet.Applet;

public class HelloWorldApplet1 extends Applet {


Font f = new Font("TimesRoman", Font.BOLD, 36);
String name,greeting;
public void init() {
name = Roger L. Norton";
greeting = new String("Hello " + name + "!");
}
public void paint(Graphics g) {
g.setFont(f);
g.setColor(Color.red);
g.drawString(greeting, 5, 40);
}
}

Java HTML APPLET Tag


<APPLET CODE= WIDTH = xxx HEIGHT= yyy >

.
</APPLET>
Options:
CODEBASE

Designates the base URL

ALIGN

Designates the applet alignment choice (Left, Right, Top, Bottom, Middle)

HSPACE

Designates the empty space at the right and left of the applet

VSPACE

Designates the empty space above and below the applet

NAME

Designates the name of the applet

ARCHIVE

Designates an archive of classes to preload

<HTML>
<HEAD>
<TITLE>
Hello World Example!
</TITLE>
</HEAD>
<BODY>
<H2>
Event Driven Software Java Lecture 2
</H2>
<P>
<APPLET code="HelloWorldApplet1.class" width="500" height="50">
Hello World!
</APPLET>
</P>
<P>
<A href="HelloWorldApplet1.java">The Source</A>
</P>
</BODY>
</HTML>

Java HTML APPLET Tag


<APPLET CODE= WIDTH = xxx HEIGHT= yyy >
Some text to be displayed if browser does not support java
<PARAM NAME = VALUE = >
<PARAM NAME = VALUE = >
</APPLET>

HellowWorldApplet2.html
<HTML>
<HEAD>
<TITLE>
Hello World Example!
</TITLE>
</HEAD>
<BODY>
<H2>
Event Driven Software Java Lecture 1
</H2>
<P>
<APPLET code="HelloWorldApplet.class" width="500" height="50">
<PARAM name="name" value="Roger L. Norton">
Hello World!
</APPLET>
</P>
<P>
<A href="HelloWorldApplet.java">The Source</A>
</P>
</BODY>
</HTML>

HelloWorldApplet3.html
<HTML>
<HEAD>
<TITLE>
Hello World Example!
</TITLE>
</HEAD>
<BODY>
<H2>
Event Driven Software Java Lecture 1
</H2>
<P>
<APPLET code="HelloWorldApplet.class" width="500" height="50">
Hello World!
</APPLET>
</P>
<P>
<A href="HelloWorldApplet.java">The Source</A>
</P>
</BODY>
</HTML>

An Interesting Example
import java.awt.Graphics;
import java.awt.Color;
public class ColorBoxes extends java.applet.Applet {
void delay(int time){
for(int i =1;i<=time;i++);
}
public void paint(Graphics g) {
int rval, gval, bval;
String tempDelay = getParameter("delay");
int finalDelay = Integer.parseInt(tempDelay);
for (int k = 1;k<=100;k++){
for (int j = 30; j < (size().height -25); j += 30)
for (int i = 5; i < (size().width -25); i += 30) {
rval = (int)Math.floor(Math.random() * 256);
gval = (int)Math.floor(Math.random() * 256);
bval = (int)Math.floor(Math.random() * 256);
g.setColor(new Color(rval,gval,bval));
g.fillRect(i, j, 25, 25);
g.setColor(Color.black);
g.drawRect(i, j, 25, 25);
};
delay(finalDelay);
};
};
}

ColorBoxes.html
<HTML>
<HEAD>
<TITLE>
Colored Boxes
</TITLE>
</HEAD>
<BODY>
<H2>
Event Driven Software Example #3
</H2>
<H2>
Colored Boxes
</H2>
<P>
<APPLET code="ColorBoxes.class" width="600" height="300">
<PARAM name="delay" value="10000000">
</APPLET>
</P>
<P>
<A href="ColorBoxes.java">The Source</A>
</P>
</BODY>
</HTML>

Events in Java
In Java most objects within the AWT class hierarchy have the ability to observe events
occurring with respect to itself (e.g. a button can observe that it has been pressed, or a
textbox can detect that it has been written to). The object can then send this
information to any other object that has indicated it is interested in knowing about such
happenings. These notified objects can then react to these events.

actionPerformed(ActionEvent)

Button

ButtonPanel
addActionListener(this)

getObjecct()

ActionEvent

buttonPressed

Events in Java

An event source:
can register listener object
can send listeners event objects

A listener object:
implements a listener interface
can be registered as a listener to various event sources

(ActionEvent actionPerformed)

Button

ButtonPanel
addActionListener(this)

getObjecct()

buttonPressed

An event object:
ActionEvent

contains information about the event that occurred


can be queried to receive this information

Event Listeners for Mouse and Keyboard Events


MouseListener
public void mouseEntered(MouseEvent e)
public void mouseExited (MouseEvent e)
public void mousePressed (MouseEvent e)
public void mouseReleased (MouseEvent e)
public void mouseClicked (MouseEvent e)

MouseMotionListener
public void mouseMoved (MouseEvent e)
public void mouseDragged (MouseEvent e)

KeyListener
public void keyPressed(KeyEvent e)
public void keyReleased(keyEvent e)
public void keyTyped(keyEvent e)

Pressed and released at same location

Keyboard and Mouse Events


AWTEvent contains four important methods:
consume //delete the event
isConsumed

//returns boolean true if consumed by another


//listener on same source

getID

//and int represening the event type

getSource

//the Object that the event came from

KeyEvent adds the following methods:


getKeyChar

//returns character typed

setKeyChar

//replace the character with a different one

getKeyCode

//returns an integer value which can be passedto


//getKeyText

isActionKey

//differentiates function and arrow keys from normal key

getModifiers

setModifiers

//retrieve/replace modifier keys

isAltDown,

isShiftDown,

isControlDown

Keyboard and Mouse Events


MouseEvent methods:
methods
getX,

getY

//determine location of the mouse event

getClickCount

//differentiates between single and double clicks

getModifiers

//to determine which button was pressed

Event Handling Examples


Clicks Example 1
Clicks Example 2
Clicks Example 3
Colors Example
Text Example

Testing an Applet
Construct an HTML file that will locate your applet in a region of an html
page.

Run appletviewer with the name of the html file where you call your applet.

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