Professional Documents
Culture Documents
Stand-alone Application
Giao din dng lnh (console): Tng tc vi ngi dng thng qua cc dng k t. Giao din ho (GUI): Tng tc vi ngi dng bng nhiu cch khc nhau nh hnh nh, nt nhn, biu tng Vic x l ng dng da trn cc s kin.
3
Th vin AWT
AWT (Abstract Windowing Toolkit)
L b th vin dng xy dng cc ng dng giao din ho (GUI) Cc thnh phn giao din ho nm trong gi java.awt
Button
TextComponent
Container
MenuComponent
...
TextField
TextArea
Panel
Window
Menu
MenuBar
MenuItem
Applet
Frame
Dialog
Thc n (Menu) Nt chn (Radio button) Danh sch (List) La chn (Choice)
Xy dng cc applet
Lp Applet
Java c lp java.applet.Applet k tha t lp java.awt.Component cho php to ra cc applet trong Web. Mi lp applet do ngi dng to ra u phi k tha t lp Applet.
Xy dng cc applet
V d 1: To file TestApplet.java
import java.applet.Applet; import java.awt.Graphics; public class TestApplet extends Applet { public void paint( Graphics g) { g.drawString(Hello world!, 50, 25); } }
Xy dng cc applet
Thc thi applet
Cch 1: To file TestApplet.html c ni dung nh sau:
<APPLET CODE=TestApplet.class WIDTH=500 HEIGHT=500 </APPLET>
M file ny bng trnh duyt WEB Cch 2: Dng cng c appletviewer. G lnh:
appletviewer TestApplet.html
10
Khung ca mt applet c bn
import java.applet.Applet; import java.awt.Graphics; public class TestApplet extends Applet { public void init() {} public void start() {} public void stop() {} public void destroy {} public void paint( Graphics g) {} }
11
Hot ng ca Applet
init(): khi to applet start(): applet bt u hot ng stop(): applet chm dt hot ng destroy(): gii phng applet Ch :
paint() khng phi l phng thc ca Applet m l ca Component. paint() c gi mi khi ca s c v li.
12
Hot ng ca Applet
Vng i ca mt Applet
Np mt applet: applet c khi to v thc thi Chuyn hoc tr v trang Web: Cc phng thc stop v start s c gi Np li applet: nh qu trnh np applet Thot khi trnh duyt: phng thc stop v destroy s c gi
13
Lp Graphics
java.awt.Graphics l lp cung cp cc phng thc v ho c bn:
ng thng (Line) ng oval (Oval) Hnh ch nht (Rectangle) a gic (Polygon) Vn bn (Text) Hnh nh (Image) ...
14
Lp Graphics
H to
x (0,0) Mn hnh
15
Lp Graphics
V ng thng
public void drawLine(int x1, int y1, int x2, int y2);
V hnh ch nht
public void drawRect(int x, int y, int width, int height);
T mt hnh ch nht
public void fillRect(int x, int y, int width, int height);
Xo mt vng ch nht
public void clearRect(int x, int y, int width, int height);
V a gic
public void drawPolygon(int[] x, int[] y, int numPoint); public void drawPolygon(Polygon p);
16
Lp Graphics
import java.applet.Applet; import java.awt.Graphics; Demo public class DemoRect extends Applet { public void init() { System.out.println("Demonstration of some simple figures"); } public void paint(Graphics g) { g.drawLine(70, 300, 400, 250); g.drawRect(100, 50, 130, 170); g.fillRect(120, 70, 70, 70); int[] x = { 280, 310, 330, 430, 370 }; int[] y = { 2p0, 140, 170, 70, 90 }; g.drawPolygon(x, y, x.length);
17
Lp Graphics
18
Lp Graphics
V ng trn/elip
public void drawOval(int x, int y, int width, int height);
T ng trn/elip
public void fillOval(int x, int y, int width, int height);
V cung trn
public void drawArc(int x, int y, int width, int height,
V xu k t
public void drawString(String str, int x, int y);
V nh
public void drawImage(Image img, int x, int y,...);
19
Lp Graphics
import java.applet.Applet; import java.awt.Graphics; Demo public class DemoOval extends Applet { public void init() { System.out.println("Demonstration of some simple figures"); } public void paint(Graphics g) { int xstart = 70, ystart = 40, size = 100; g.drawOval(xstart, ystart, size, size); g.drawOval(xstart + (size*3)/4, ystart, size, size); g.drawOval(xstart + size/2, ystart + size/2, size, size); g.drawArc(xstart, ystart, 300, 200, 0, -90); g.drawString("good morning !", xstart + 265, ystart + 90); } } 20
Lp Graphics
21
Lp Graphics
import java.applet.Applet; Demo import java.awt.Graphics; import java.awt.Image; public class DemoImage extends Applet { public void init() { System.out.println("Demonstration of imaging"); } public void paint(Graphics g) { Image image = getToolkit().getImage("summer.jpg"); g.drawImage(image, 0, 0, this); } }
22
Lp Graphics
23
Cc lp tin ch khc
Lp Point: biu din im trn mn hnh Lp Dimension: biu din kch thc v chiu rng v chiu cao ca mt i tng Lp Rectangle: biu din hnh ch nht Lp Polygon: biu din a gic Lp Color: biu din mu sc
24
Cc lp tin ch khc
import java.applet.Applet; Demo import java.awt.*; public class DemoColor extends Applet { public void paint(Graphics g) { Dimension size = getSize(); g.setColor(Color.orange); g.fillRect(0, 0, size.width, size.height); Color color = new Color(10, 150, 20); g.setColor(color); g.drawString("I am a colorful string", size.width/2 - 50, size.height/2);
25
Cc lp tin ch khc
26
X l font v
import java.applet.Applet; Datjava.awt.*; import font public class DemoFont extends Applet { public void paint(Graphics g) { Font font = new Font("Arial", Font.BOLD, 30); g.setFont(font); g.drawString("I am font Arial, bold, size 30", 50, 50); } }
27
Bi tp ti lp
Bi 1: Sinh 100 ng thng c to ngu nhin v mu ngu nhin
28
Bi tp ti lp
Bi 2: V s thay i ca mu xanh l cy
29
Bi tp ti lp
Bi 3: Tnh ton to v v ra mn hnh cc hnh sau.
30
32
Khc phc:
V hnh vo mt khung hnh ph trong b nh thay v v trc tip ra mn hnh. Khi v xong, khung hnh ph c hin th ra mn hnh trong phng thc paint(). Np chng update() trnh vic xo ni dung ca s.
33
Applet
Kh nng ca Applet
Applet c t ti mt Server trn mng Applet c chuyn ti my Client theo mt trang HTML no Khi mt trnh duyt (tng thch vi Java) nhn c trang web ny, n s ti m ca Applet v thc thi trn my client
36
Applet
Gii hn ca Applet
Khng c np cc th vin hay cc phng thc s dng m gc (native code). Khng c c v ghi ln cc tp tin ca my ang chy chng. Khng c khi ng bt k chng trnh no trn my ang chy. Khng c c bt k tnh cht no ca h thng.
37
Bi tp
1. Xy dng mt applet v ra mt khun mt nh hnh v sau:
38
Bi tp
2. V th hm s y = 3*x*x - 6*x+ 1 3. V th hm s y = sin(x)
39
Bi tp
4. Dng k thut khung hnh ph v mt qu bng chuyn ng t do trong mt applet. 5. M rng bi ton vi 2 qu bng. 6. M rng bi ton vi nn applet l mt hnh nh. 7. M rng bi ton vi qu bng l mt hnh nh v c nhiu qu bng chuyn ng ng thi.
40