Upload
noi-that-to-deo
View
15
Download
1
Embed Size (px)
DESCRIPTION
Giao trinh Bach Khoa
Citation preview
1
B MN CNG NGH PHN MM
ViN CNG NGH THNG TIN V TRUYN THNG
TRNG I HC BCH KHOA H NI
LP TRNH HNG I TNG
Bi 9. Applet
Nguyn Th Thu Trang
Ni dung
1. Tng quan v Applet
2. Vng i ca Applet
3. Lp Graphics
4. Cac phng thc ca Applet
2
1. Tng quan v Applet
Applet l chng trnh Java c nhng trong m ngun HTML.
C th chy trn trnh duyt Web c ci JVM (JRE) sau khi ti v.
Nh cc applet, trang Web c thm c sc sng v applet c th th hin multimedia, to c s tng tc vi user nh hi-tr li
C th nguy him, khng m bo tnh an ton
3
1. Tng quan v Applet
c th chy applet, trnh duyt ca ngi dung phi bt chc nng Java Plug-in
4
1. Tng quan v Applet
to 1 applet, xy dng 1 lp con ca lp Applet hoc lp JApplet. import java.applet.Applet; class MyApplet extends Applet
{...}
hoc
import javax.swing.JApplet; class MyApplet extends JApplet
{...}
Thng t tn lp applet ny trng tn vi tn file.html.
5
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
AppletTemplate
import java.applet.Applet;
import java.awt.*;
public class AppletTemplate extends Applet
{
// Khai bao bien
public void init() {
// Khoi tao cac bien, tai cac hinh anh...
}
public void paint(Graphics g) {
// Cac thao tac ve (draw)
}
}
6
To 1 ti liu HTML cha applet
thc thi mt applet, to mt tp tin HTML c s dng th applet hoc th object Th applet/object c hai thuc tnh:
Width Height
V d:
7
Applet HTML Template
A Template for Loading Applets
A Template for Loading Applets
Error! You must use a Java-enabled browser.
8
Chng trnh Applet u tin
import java.applet.Applet;
import java.awt.Graphics;
public class FirstApplet extends Applet {
public void paint(Graphics g) { g.drawString("This is my first applet!",20,100);
}
}
9
appletviewer FirstApplet.java
hoc Ctrl + 3 (Textpad)
Chng trnh Applet u tin (2)
K tha t lp Applet
ha t Abstract Window Toolkit (AWT)
Chy sau khi bin dch: Trn cmd: appletviewer FirstApplet.html hoc
appletviewer FirstApplet.java
Textpad: Ctrl + 3
10
S dung .html
First Applet
First Applet
Sorry, this example
requires Java.
11
Mozilla Firefox
12
Ni dung
1. Tng quan v Applet
2. Vng i ca Applet
3. Lp Graphics
4. Cac phng thc ca Applet
13
2. Vng i ca Applet
init(): Ch c gi khi applet bt u thc thi
start(): c gi sau init() hoc khi applet c tr li
paint(): c gi sau khi trinh duyt init va start, va c gi li khi trinh duyt c ve li
stop(): c gi khi applet c thu nh
destroy(): c gi khi applet kt thc (tt trinh duyt)
14
Vi du
import java.applet.Applet;
import java.awt.Graphics;
public class AppletLifeCycle extends Applet {
String str = "";
public void init() {
str += "init; ";
}
public void start() {
str += "start; ";
}
public void stop() {
str += "stop; ";
}
public void destroy() {
System.out.println("destroy");
}
public void paint(Graphics g){
g.drawString(str, 10, 25);
}
}
15
Ni dung
1. Tng quan v Applet
2. Vng i ca Applet
3. Lp Graphics
4. Cac phng thc ca Applet
16
Cac phng thc trong Graphics
drawString(string, left, bottom) Ve mt xu vi font ch va mau hin ti vi goc trai (left)
va ay (bottom) ca xu c ch ra
drawRect(left, top, width, height) Ve ng vin hinh ch nht (1-pixel border) vi mau
hin ti
fillRect(left, top, width, height) Ve hinh ch nht c vi mau hin ti
drawLine(x1, y1, x2, y2) Ve mt ng vi day 1 pixel t ta (x1, y1) n ta
(x2, y2)
17
Cac phng thc trong Graphics
drawOval, fillOval Ve hinh elip (vin/c) vi cac tham s ch ra la hinh ch nht bao
quanh elip (oval)
drawPolygon, fillPolygon Ve hinh a giac (vin/c) vi cac im c xac nh bi cac mng
hoc mt i tng Polygon (mt lp lu mt lot cac im) c ch ra trong tham s
Mc nh hinh a giac c ve khep kin, ve hinh a giac m s dng phng thc drawPolyline
drawImage Ve mt hinh nh Hinh nh co th co nh dng JPEG hoc GIF (gm GIF89A)
18
import java.applet.Applet;
import java.awt.Graphics;
public class DrawTest extends Applet{
public void paint(Graphics g){
//Ve hcn xq vung hien thi applet
g.drawRect(0, 0,
getWidth()-1,getHeight()-1);
//Ve xau trong hcn
g.drawString("Ve Applet", 5, 15);
g.fillOval(30, 30, 120, 160);
g.drawLine(30, 30, 150, 30);
}
}
19
Cc phng thc trong Graphics drawArc(int x, int y, int w, int h, int degreesO, int degrees1)
fillArc(int x, int y, int w, int h, int degree0, int degree1)
drawPolygon(int x[], int u[], int n)
fillPolygon(int x[], int y[], int n)
drawPolyline(int x[], int y[], int n)
Color getColor()
Font getFont()
setFont(Font f)
20
Vi du
import java.applet.Applet;
import java.awt.Graphics;
public class DrawArc extends Applet {
public void paint(Graphics g) {
g.drawArc(20, 20, 160, 160, 0, 135);
}
}
21
Graphics Font
setFont, getFont Ch ra font c s dng ve vn bn Xac nh kich thc ca ky t thng qua FontMetrics (trong Java 2 s dng LineMetrics)
Thit lp font cho i tng Graphics khng tn ti qua cac li gi k tip ca paint
Thit lp font ca window (vi d gi phng thc setFont) cho vic thay i lu dai cho i tng Graphics.
22
Graphics Color
setColor, getColor Ch ra mau mt trc (foreground color) cho cac thao tac
ve Mc nh, i tng Graphics nhn mau mt trc t mau
ca window. AWT nh nghia 16 mau (Color.red, Color.blue...)
hoc bn co th to ra mau ca ring minh bng new Color(r, g, b)
Thay i mau ca i tng Graphics ch nh hng n i tng Graphics tng ng thay i mau lu dai, gi phng thc setForeground ca
applet
23
Vi du
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Font;
public class BlueString extends Applet {
public void paint(Graphics g) {
g.setFont(new Font("SansSerif", Font.BOLD, 30));
g.setColor(Color.blue);
g.drawString("Blue String", 100, 50);
}
}
24
Tai mt hinh anh
ng ky Image (trong phng thc init)
Image image = getImage(getCodeBase(), "file");
Image image = getImage(url);
Nu URL la tuyt i, cn s dng try/catch
Ve mt hinh nh (trong phng thc paint) g.drawImage(image, x, y, window);
g.drawImage(image, x, y, w, h, window);
Co th ve mt phn ca hinh nh S dng applet (this) cho tham s window
25
Tai mt hinh anh t URL tng i
import java.applet.Applet;
import java.awt.*;
/** An applet that loads an image
from a relative URL. */
public class JavaMan1 extends Applet {
private Image javaMan;
public void init() {
javaMan = getImage(getCodeBase(),
"images/Java-Man.gif");
}
public void paint(Graphics g) {
g.drawImage(javaMan, 0, 0, this);
}
}
26
Tai mt hinh anh t URL tuyt i import java.applet.Applet;
import java.awt.*;
import java.net.*;
...
private Image javaMan;
public void init() {
try {
URL imageFile =
new URL("http://www.corewebprogramming.com" +
"/images/Java-Man.gif");
javaMan = getImage(imageFile);
} catch(MalformedURLException mue) {
showStatus("Bogus image URL.");
System.out.println("Bogus URL");
}
}
27
Ni dung
1. Tng quan v Applet
2. Vng i ca Applet
3. Lp Graphics
4. Cac phng thc ca Applet
28
Cac phng thc khac cua Applet
getCodeBase, getDocumentBase Ly v URL ca:
Applet file - getCodeBase HTML file - getDocumentBase
getParameter(String name) Ly v gia tr t phn t PARAM trong HTML file
getSize() Tr v Dimension (width, height) ca applet
getGraphics() Ly v i tng Graphics hin ti ca applet i tng Graphics khng tn ti gia cac ln gi paint()
29
import java.applet.*;
import java.awt.*;
public class Circle extends Applet{
public void paint(Graphics g) {
Dimension d = getSize();
int xc = d.width / 2;
int yc = d.height / 2;
int radius = (int)((d.width < d.height) ?
0.4 * d.width : 0.4 * d.height);
g.drawOval(xc - radius, yc - radius,
2 * radius, 2 * radius);
}
}
30
Cac phng thc khac (2)
showDocument () getAppletContext().showDocument(java.net.URL url)
getAppletContext().showDocument(java.net.URL url, String
targetWindow)
Hin th mt tai liu trn trinh duyt
showStatus(String str) Hin th mt xu trong dong trng thai cui trinh duyt
Cursor getCursor(), setCursor(Cursor cur) Ly v hoc thit lp Cursor cho chut, vi d nh CROSSHAIR_CURSOR, HAND_CURSOR, WAIT_CURSOR
31
V d import java.applet.Applet;
import java.awt.Graphics;
public class TestApplet extends Applet {
public void paint(Graphics g) {
g.drawString("This is my first
applet!",
20, 100);
showStatus("Applet is running!");
g.drawString("Context: " +
getAppletContext(), 20, 120);
}
}
32
Cac phng thc khac (3)
getAudioClip, play Ly v tp m thanh t mt v tri t xa va chi no JDK 1.1 ch h tr .au. Java 2 con h tr thm MIDI, .aiff
va .wav getBackground, setBackground
Ly v/thit lp mau nn cho applet Lp SystemColor cung cp truy cp n cac mau ca
man hinh nn getForeground, setForeground
Ly v/thit lp mau ve ca applet (mau mc nh cho cac thao tac ve - draw)
33
Vi du
import java.applet.*;
import java.awt.*;
public class BackgroundForeground extends Applet {
public void paint(Graphics g) {
setBackground(Color.yellow);
setForeground(Color.blue);
g.drawLine(0, 0, 200, 200);
g.fillRect(100, 40, 50, 50);
}
}
34
Phn t APPLET trong HTML file
...
Cac thuc tinh bt buc CODE
Ch ra tn ca lp Java cn ti Tn tp c thng dch vi th mc ca trang HTML hin ti (mc
nh) tr khi CODEBASE c cung cp
WIDTH va HEIGHT Ch ra vung ma applet se hin th Cac gia tr c a ra theo pixel hoc theo phn trm ca ca s
trinh duyt
35
Phn t APPLET trong HTML file (2)
Cac thuc tinh khac ALIGN, HSPACE, va VSPACE
Kim soat v tri va khong cach vi vin theo pixel
ARCHIVE Ch ra cac tp JAR (tp zip vi phn mi rng .jar) cha tt c cac
lp va hinh nh c s dng bi applet
Tit kim ang k thi gian ti v nhiu tp class
NAME Tn ca applet dung cho vic lin kt vi cac applet khac hoc vi
JavaScript
MAYSCRIPT (khng chun) Cho phep JavaScript iu khin applet
36
Vi du: Thit lp thuc tinh Param
Customizable HelloWWW Applet
Error! You must use a Java-enabled browser.
Error! You must use a Java-enabled browser.
Error! You must use a Java-enabled browser.
37
Applet HelloWWW2 oc cac tham s S dng getParameter(name) ly v gia tr ca
phn t PARAM, tn ca tham s co phn bit ch hoa ch thng
38
import java.applet.Applet;
import java.awt.*;
public class HelloWWW2 extends Applet {
public void init() {
setFont(new Font("SansSerif", Font.BOLD, 30));
Color background = Color.GRAY;
Color foreground = Color.DARK_GRAY;
String backgroundType = getParameter("BACKGROUND");
Applet HelloWWW2 oc cac tham s 39
if (backgroundType != null) {
if (backgroundType.equalsIgnoreCase("LIGHT")){
background = Color.WHITE;
foreground = Color.BLACK;
} else if (backgroundType.equalsIgnoreCase("DARK")){
background = Color.BLACK;
foreground = Color.WHITE;
}
}
setBackground(background);
setForeground(foreground);
}
public void paint(Graphics g) {
g.drawString("Hello, World Wide Web.", 5, 35);
}
}
Ly tham s t HTML Kt qua
40
S dung JavaJump.html
Jumping Java
Jumping Java
import java.applet.Applet;
import java.awt.*;
/** An applet that draws an image. */
public class JavaJump extends Applet{
private Image jumpingJava;
public void init() {
setBackground(Color.white);
setFont(new Font("SansSerif", Font.BOLD, 18));
jumpingJava = getImage(getDocumentBase(), getParameter(image));
add(new Label("Great Jumping Java!"));
System.out.println("Yow! I'm jiving with Java.");
}
public void paint(Graphics g) {
g.drawImage(jumpingJava, 0, 50, this);
}
}
42
JavaJump.java