Download Applets

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Applets
For a tutorial, see
http://java.sun.com/docs/books/tutorial/applet/overview/index.html
Applets
Special Java programs (without a “main”) callable
from HTML and executed in a graphic context.
They can be executed by:
a Java enabled Web Browser;
ad-hoc programs (e.g. Sun AppletViewer).
1
1
Applets
Every applet is implemented by creating a
subclass of the Applet class.
The hierarchy determines much of what an
applet can do and how.
Applet Lifecycle
An applet can react to major events in the following
ways:
It can initialize itself.
init()
It can start running.
start()
It can draw some graphics. paint()
It can respond to user-generated events (Mouse,
keyboard, menus…). handleEvent()
It can stop running.
stop()
It can perform a final cleanup, in preparation for
being unloaded.
destroy()
2
2
Applet Lifecycle
init()
Multithreading!
start()
handleEvent()
stop()
destroy()
Whenever it’s needed,
at lower priority
paint()
Actually, more threads are active
behind the scenes.
handleEvent()
This code is part of the AWT (1.0 Event Model)
public boolean handleEvent(Event evt) {
switch (evt.id) {
case Event.MOUSE_ENTER: return mouseEnter(evt, evt.x, evt.y);
case Event.MOUSE_EXIT:
return mouseExit(evt, evt.x, evt.y);
case Event.MOUSE_MOVE: return mouseMove(evt, evt.x, evt.y);
case Event.MOUSE_DOWN: return mouseDown(evt, evt.x, evt.y);
case Event.MOUSE_DRAG: return mouseDrag(evt, evt.x, evt.y);
case Event.MOUSE_UP:
return mouseUp(evt, evt.x, evt.y);
3
3
handleEvent()
case
case
case
case
Event.KEY_PRESS:
Event.KEY_ACTION:
return keyDown(evt, evt.key);
Event.KEY_RELEASE:
Event.KEY_ACTION_RELEASE: return keyUp(evt, evt.key);
case Event.ACTION_EVENT:
case Event.GOT_FOCUS:
return action(evt, evt.arg);
return gotFocus(evt, evt.arg);
case Event.LOST_FOCUS:
return lostFocus(evt, evt.arg);
}
return false;
}
Applets-Event handling
To react to an event, an applet must override either the
appropriate event-specific method or the
handleEvent method.
For example, adding the following code to the Simple
applet makes it respond to mouse clicks.
import java.awt.Event;
...
public boolean mouseDown(Event event, int x, int y) { addItem("click!... ");
return true;
}
4
4
HTML 3.2: Java Applet Support
<applet code=“Name.class”height=“150” width=“300”>
<param name=“time” value=“100”>
<param name=“color” value=“red”>
Questo Browser non supporta le applets
</Applet>
ted
a
c
re
p
e
D
in
4.0
L
HT M
Objects (HTML 4.0)
The OBJECT tag was introduced to give the browser
useful info to load an visualize types that are
not natively supported utili a caricare o visualizzare tipi di dati
(applets, plugins, Active-X controls ecc.)
An optional tag PARAM within the OBJECT tag
Allows to pass parameters to the plug-in
<OBJECT HEIGHT=… WIDTH=… CLASSID=…>
<PARAM NAME=… VALUE=…>
</OBJECT>
5
5
Applet example
package demoApplet;
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
String var0;
StringBuffer buffer;
//Construct the applet
public SimpleApplet() {
}
//Initialize the applet
public void init() {
buffer = new StringBuffer();
addItem("initializing... ");
}
Applet example
//Start the applet
public void start() {
addItem("starting... ");
addItem("Parameter is: "+getParameter("param0"));
}
//Stop the applet
public void stop() {
addItem("stopping... ");
}
//Destroy the applet
public void destroy() {
addItem("destroying... ");
}
void addItem(String newWord) {
System.out.println(newWord);
buffer.append(newWord);
repaint();
}
6
6
Applet example
public void paint(Graphics g) {
int red = (int)(Math.random() * 255);
int green = (int)(Math.random() * 255);
int blue = (int)(Math.random() * 255);
g.setColor(new Color(red, green, blue));
System.out.println(red+green+blue);
//Draw a Rectangle around the applet's display area.
g.fillRect(0, 0, size().width - 1, size().height - 1);
g.setColor(new Color(0, 0, 0));
//Draw the current string inside the rectangle.
g.drawString(buffer.toString(), 5, 15);
}
}
HTML page (tag Applet)
<head>
<title>
HTML Test Page
</title>
</head>
<body>
demoApplet.SimpleApplet will appear below in a Java enabled browser.<br>
<applet
codebase = "."
code = "demoApplet.SimpleApplet.class"
name
= "TestApplet"
width = "400"
height = "300"
hspace = "0"
vspace = "0"
align = "middle"
>
Your browser cannot show applets
<param name = "param0" value = "hello">
</applet>
</body>
</html>
7
7
HTML page (tag Object)
<head>
<title>
HTML Test Page
</title>
</head>
<body>
demoApplet.SimpleApplet will appear below in a Java enabled browser.<br>
<object classid="java:demoApplet.SimpleApplet.class">
<param name = "param0" value = "hello" valuetype="data">
Your browser cannot show applets
<param name = "param0" value = "hello">
</object>
</body>
</html>
8
8