บทท�� 19
รู้��จักและใช้�งาน Applet
รู้��จักและใช้�งาน Applet โปรู้แกรู้มท��เขี�ยนด้�วยภาษา Java ถู�กออกแบบให้�ปรู้ะมวล
ผลผ"านเว#บบรู้าวเซอรู้%ได้� โด้ยสรู้�างคลาสท��ส)บทอด้ค*ณสมบติ-มาจัากคลาส Applet
ซ.�งอย�"ในแพ็#คแกจัjava.applet และก0าห้นด้ access modifier ให้�เป1นแบบ public
เพ็)�อให้�เรู้�ยกใช้�งานผ"านเว#บบรู้าวเซอรู้%ได้� ม�รู้�ปแบบการู้ใช้�งานด้งน�2import java.applet.*; public class AppletName extends
Applet {}โด้ยท�� AppletName เป1นช้)�อคลาส 2
การู้ท0างานขีอง Applet
3
กรู้ะบวนการู้ห้ลกขีองการู้สรู้�าง Applet
4
การู้สรู้�าง Applet เรู้-�มติ�นสรู้�างจัากคลาส Applet ซ.�งม�ส"วนขีองโปรู้แกรู้มเห้ม)อนกบการู้เขี�ยน Java Application ท�วไป
จัากน2นเรู้�ยกใช้� Applet ท��สรู้�างขี.2นน�2 โด้ยน0าช้)�อไฟล%ไปแทรู้กในส"วนขีองค0าส�ง HTML
การู้ส"งค"าติวแปรู้จัากไฟล% HTML เขี�าไปใช้�ในคลาส Applet สามารู้ถูส"งไปพ็รู้�อมกบการู้เรู้�ยกใช้�ห้รู้)อปรู้ะมวลผลในคลาส
Applet ท��ติ�องการู้ได้� โด้ยติวแปรู้ด้งกล"าวจัะติ�องถู�กเขี�ยนแทรู้กอย�"ใน tag ขีอง <applet> และ </applet> ซ.�งม�รู้�ปแบบด้งน�2<applet code="AppletName.class" width="
widthValue" height="heightValue"><param name=”paramName1”
value=”paramValue1”><param name=”paramName2”
value=”paramValue2”></applet>โด้ยท�� AppletName เป1นช้)�อคลาสparamName1 เป1นช้)�อติวแปรู้ท��ใช้�ส"งค"าติวท�� 1 paramName2 เป1นช้)�อติวแปรู้ท��ใช้�ส"งค"าติวท�� 2paramValue1 เป1นค"าติวแปรู้ท��ใช้�ส"งค"าติวท�� 1 paramValue2 เป1นค"าติวแปรู้ท��ใช้�ส"งค"าติวท�� 2widthValue เป1นค"าความกว�าง heightValue เป1นค"าความส�ง
ในคลาส Applet จัะรู้บค"าท��ส"งผ"านติวแปรู้ในไฟล% HTML ด้�วยเมธอด้ getParameter(paraname) โด้ย paraname ติ�องเป1นช้)�อท��ติรู้งกบช้)�อติวแปรู้ในไฟล% HTML ด้�วย ด้งติวอย"างติ"อไปน�2
5
โปรู้แกรู้มแสด้งผลขี�อความท��รู้บมาจัากไฟล% HTML
6
เมธอด้วาด้รู้�ปทรู้งติ"างๆ และการู้รู้ะบายส� (1) ก0าห้นด้ให้� g เป1นติวแปรู้ปรู้ะเภท Graphics เมธอด้ drawString() แสด้งขี�อความ s ท��ติ0าแห้น"ง x, y
g.drawString(s, x, y); เมธอด้ drawLine() วาด้เส�นท��จั*ด้เรู้-�มติ�น x1, y1 และจั*ด้ส-2นส*ด้
x2, y2g.drawLine(x1, y1, x2, y2);
เมธอด้ drawRect() วาด้ส��เห้ล��ยมม*มแห้ลมแบบโปรู้"งท��ติ0าแห้น"ง x, y กว�างเท"ากบ w และส�งเท"ากบ h
g.drawRect(x, y, w, h); เมธอด้ drawRoundRect() วาด้ส��เห้ล��ยมม*มมนแบบโปรู้"งท��
ติ0าแห้น"ง x, y กว�างเท"ากบ w และส�งเท"ากบ h โด้ยม�ความกว�างขีองส��เห้ล��ยมท��ก0าห้นด้ขีนาด้ม*มมนเท"ากบ arc_w และความส�งขีองส��เห้ล��ยมท��ก0าห้นด้ขีนาด้ม*มมนเท"ากบ arc_h
g.drawRoundRect(x, y, w, h, arc_w, arc_h);7
เมธอด้วาด้รู้�ปทรู้งติ"างๆ และการู้รู้ะบายส� (2) เมธอด้ drawOval() วาด้วงกลมห้รู้)อวงรู้�แบบโปรู้"งท��ติ0าแห้น"ง x, y กว�าง
เท"ากบ w และส�งเท"ากบ hg.drawOval(x, y, w, h);
เมธอด้ drawArc() วาด้เส�นโค�งแบบโปรู้"งท��ติ0าแห้น"ง x, y กว�างเท"ากบ w และส�งเท"ากบ h โด้ยม�ม*มจั*ด้เรู้-�มขีองส"วนขีองวงกลมห้รู้)อวงรู้�เท"ากบ a1 และขีนาด้ขีองม*มวด้ติ2งแติ"จั*ด้เรู้-�มติ�นขีองส"วนขีองวงกลมห้รู้)อวงรู้�ไปจันถู.งจั*ด้ส-2นส*ด้เท"ากบ a2
g.drawArc(x, y, w, h, a1, a2); เมธอด้ fillRect() วาด้ส��เห้ล��ยมท.บท��ติ0าแห้น"ง x, y ซ.�งม�ความกว�างเท"ากบ w
และความส�งเท"ากบ hg.fillRect(x, y, w, h);
เมธอด้ fillRoundRect() วาด้ส��เห้ล��ยมม*มมนท.บท��ติ0าแห้น"ง x, y กว�างเท"ากบ w และส�งเท"ากบ h โด้ยม�ความกว�างขีองส��เห้ล��ยมท��ก0าห้นด้ขีนาด้ม*มมนเท"ากบ arc_w และความส�งขีองส��เห้ล��ยมท��ก0าห้นด้ขีนาด้ม*มมน เท"ากบ arc_h
g.fillRoundRect(x, y, w, h, arc_w, arc_h); เมธอด้ fillOval() วาด้วงรู้�ท.บท��ติ0าแห้น"ง x, y กว�างเท"ากบ w และส�งเท"ากบ
hg.fillOval(x, y, w, h);
เมธอด้ fillArc() วาด้รู้�ปเส�2ยวท��ติ0าแห้น"ง x, y กว�างเท"ากบ w และส�งเท"ากบ h โด้ยม�ม*มจั*ด้เรู้-�มขีองส"วนขีองวงกลมห้รู้)อวงรู้�เท"ากบ a1 และขีนาด้ขีองม*มวด้ติ2งแติ"จั*ด้เรู้-�มติ�นขีองส"วนขีองวงกลมห้รู้)อวงรู้�ไปจันถู.งจั*ด้ส-2นส*ด้ เท"ากบ a2 g.fillArc(x, y, w, h, a1, a2);
8
การู้ใช้�งานส� RGB
9
ส�ติวเลขีจั0านวนเติ#มขีอง
รู้ะบบ RGB
ด้0า (black) (0, 0, 0)
ขีาว (white)
(255, 255, 255)
แด้ง (red) (255, 0, 0)
น02าเง-น (blue)
(0, 0, 255)
เห้ล)อง (yellow)
(255, 255, 0)
เขี�ยว (green)
(0, 255, 0)
เทา (gray) (128, 128, 128)
ช้มพ็� (pink) (255, 175, 175)
โปรู้แกรู้มวาด้รู้�ปทรู้งและแสด้งผลขี�อความโด้ยการู้ใช้�ส�แบบติ"างๆimport java.applet.*;import java.awt.*; public class ColorfulShape extends Applet { Font fn; Color c; public void init() { setBackground(Color.lightGray); fn = new Font("Tahoma", Font.BOLD, 12); } public void paint(Graphics g) { g.setFont(fn); g.setColor(Color.yellow); g.drawArc(10, 20, 180, 180, 0, 180);
g.drawString("drawArc1", 70, 40); g.setColor(Color.magenta); g.drawRect(50,
50, 100, 15); g.drawString("drawRect", 70, 62);
g.setColor(Color.blue); g.fillRect(50, 70, 100, 15);
g.setColor(Color.white); g.drawString("fillRect", 80, 82);
g.setColor(Color.green);
10
g.drawOval(50, 110, 100, 30); g.drawString("drawOval", 70, 130);
g.setColor(Color.black); g.fillOval(50, 145, 100, 30);
g.setColor(Color.white);g.drawString("fillOval", 80, 165);
g.setColor(Color.red); g.drawArc(10, 28, 180, 180, 180, 180);
g.drawString("drawArc2", 70, 195); c = new Color(0, 78, 152);
g.setColor(c); g.fillOval(200, 75, 90, 90);
g.setColor(Color.white);g.drawString("fillCircle", 222, 115);
g.drawString("(0,78,152)", 215, 135); c = new Color(64, 0, 128);
g.setColor(c); g.fillArc(300, 20, 180, 180, 0, 90);
g.setColor(Color.white);g.drawString("(64,0,128)", 300, 25);
c = new Color(255, 132, 9); g.setColor(c);g.fillArc(300, 20, 180, 180, 90, 90);
g.setColor(Color.white);g.drawString("(255,132,9)", 420, 25);
g.setColor(Color.yellow);g.fillArc(300, 20, 180, 180, 180, 90);
g.setColor(Color.red);g.fillArc(300, 20, 180, 180, 270, 90);
g.setColor(Color.white);g.drawString("fillArc", 370, 215); }}
<html><head> <title>ColorfulShape</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body><applet code="ColorfulShape.class" HEIGHT=220 WIDTH=500></applet></body></html>
ท0างานกบเมาส%และค�ย%บอรู้%ด้
11
import javax.swing.*;import java.applet.*;import java.awt.*;import java.awt.event.*; public class KeyMouseTest extends Applet { JLabel label1, label2; JPasswordField msg; Font fn = new Font("Microsoft Sans Serif", Font.BOLD,
14); public void init() { setBackground(Color.cyan); label1 = new JLabel("Enter Message",
SwingConstants.CENTER); label1.setPreferredSize(new Dimension(120, 25));
msg = new JPasswordField(10); label2 = new JLabel("Show Your Message Here",
SwingConstants.CENTER); label2.setOpaque(true);
label2.setBackground(Color.blue); label2.setForeground(Color.white);
label2.setPreferredSize(new Dimension(200, 25)); label1.setFont(fn); msg.setFont(fn);
label2.setFont(fn); add(label1); add(msg); add(label2);
msg.addActionListener(new TextListener()); label2.addMouseListener(new MyMouseListener());
}
<html> <head> <title>KeyMouseTest</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <applet code="KeyMouseTest.class" width="300" height="70"></applet> </body></html>
private class TextListener implements ActionListener { public void actionPerformed(ActionEvent e) { String ans = new String(msg.getPassword()); label2.setText("Your Message is " + ans); } } private class MyMouseListener implements MouseListener { public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { label2.setBackground(Color.red); } public void mouseExited(MouseEvent e) { label2.setBackground(Color.blue); } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { } }}
Top Related