java applet

Post on 14-Feb-2016

59 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Java applet. ใช้ยังไง. มันรันจากหน้า html ซึ่งหน้านั้นต้องมี tag ที่บอกข้อมูล เอา applet อะไร ( class file อยู่ไหน) จะอยู่ตรงไหนในหน้า browser (size, location) Browser ต้องมี java plug-in นะ ดูตัวอย่างหน้าต่อไปเลย. ต้อง extends ตัวนี้. import java.awt.*; - PowerPoint PPT Presentation

TRANSCRIPT

Java applet

ใชยงไง• มนรนจากหนา html ซงหนานนตองม tag ทบอก

ขอมล– เอา applet อะไร (class file อยไหน)– จะอยตรงไหนในหนา browser (size, location)

• Browser ตองม java plug-in นะ• ดตวอยางหนาตอไปเลย

import java.awt.*;import javax.swing.*;

/** * @version 1.22 2007-06-12 * @author Cay Horstmann */public class NotHelloWorldApplet extends JApplet{ public void init() { EventQueue.invokeLater(new Runnable() { public void run() { JLabel label = new JLabel("Not a Hello, World applet",

SwingConstants.CENTER); add(label); } }); }}

ตอง extends ตวน

ตอจากนตองสราง html ทมแทกดงน

สงการอพเดท user interface ไปท event dispatch thread เพอใหรนขนานกบ main ตอไป

นถาจะเซตอะไรใน GUI กตองทำาในนจะดกวา invokeLater รเทรนทนท

เรยกเมอโหลดครงแรก

<applet code="NotHelloWorldApplet.class" width="300" height="100"></applet>แคนกเอาหนานเปนตวรนโปรแกรมจาวาเรา

ไดแลว

ลองใช appletviewer xxx.html เปดดกได

หรอเขยนแทกนในคลาสไฟลแลวใสคอมเมนกได ซง appletviewer กจะรนจากคลาสได

เลย

หนวยเปนพกเซล

แตตวนมนเหนแค applet ไมเหนสวนอนในหนา browser นน

ถามการคอมไพลใหม ตองปดและเปดบราวเซอรใหมเลย ไมงนโคดใหมไมโหลด

วธเปลยน application เปน applet• สรางหนา html ทโหลดคลาสได• สราง public class ท extends Japplet• เอา main method ทงไป• ไมตองสรางเฟรมหลกให GUI แลว เพราะมนจะอยใน

บราวเซอร• อะไรทเคยเขยนในคอนสตรคเตอรของเฟรมหลก ใหมา

เขยนใน init • ไมตองสราง applet ออบเจกต เพราะบราวเซอรทำาใหและ

บราวเซอรจะเรยก init ใหเอง• ไมตองเรยก setSize อกแลว เพราะทำาจากหนาเวบไง• ไมตองเรยก setDefaultCloseOperation เพราะวาตอนน

เราปดบราวเซอรแทน• ไมตองเรยก setTitle และ setVisible ดวยเชนกน

เมธอดตางๆทเราอาจตอง overridevoid start()รนทกครงทคนเขามาดหนาเวบทมแอพเพลต จากหนาอนvoid stop()รนทกครงทคนออกจากหนาเวบทมแอพเพลต ไปดหนา

อนvoid destroy()รนทกครงทบราวเซอรถกปดvoid resize(int width, int height)ยงทำางานกบบราวเซอรทวไปไมไดตอนน

Attribute ภายใน Tag• ถาม text อยใน applet tag มนจะถกแสดงเมอ

บราวเซอรนนแสดงตวแอพเพลตไมได• width, height อยาลมวา แอพเพลตนน resize ไมได• align บอก alignment ของตวแอพเพลต คาตางๆ

เหมอนคาในแทก img ของเวบ• vspace, hspace บอกจะเวนทในแนวตง (ทงบนและ

ลาง) และแนวนอน (ทงซายและขวา) หนวยเปนพกเซล• code บอกถง .class วาอยไหน ถาม package ตองใส

ใหถกตอง

• codebase url เพอใชหา .class ไฟล ใสชอเตมๆได• archive บอกจารไฟล หรอไฟลทโปรแกรมนใช วาอย

ทไหน ซงไฟลพวกนจะถกโหลดจากเวบเซอรเวอรกอนทแอพเพลตจะถกโหลด

ตวอยาง archive = “a.jar, corejava/corejavaclasses.jar’’

• object ใชบอกชอไฟลทม serialize แอพเพลตออบเจกต คอเราเกบคาของทกฟลดในไฟล ถาใชเมธอดน init จะไมถกเรยกแต start จะถกเรยก ใชกบการจำา state เอาไว

• name ใชตงชอแอพเพลต สำาหรบใหคนเขยนสครปตใชได เชนคนเขยน จาวาสครปต หรอใชใหสองแอพเพลตตดตอกนกได

สำาหรบ html 4.0

• ไมใช applet tag แลว ใช object tag แทน<object>

codetype=“application/java”classid=“java:MyApplet.class”width=“100” height=“150”

ม codebase ดวย ทำางานเหมอนเดมเลย

ถา applet ตองใชพารามเตอร• เราตองใสคาพารามเตอรลงใน html นนแหละ

ตวอยางเชน ให html เปนตวกำาหนด font ท applet จะใช

<applet code=“FontParamApplet.class” width=“100” height=“150”>

<param name=“font” value=“Helvetica”/>

</applet>

สวนการให applet อานพารามเตอรจากหนา html นน ใชเมธอด getParameter

public class FontParamApplet extends JApplet{public void init(){

…String fontname = getParameter(“font”);…

}…}

พารามเตอรนนเปนสตรงเสมอ ดงนนเราตองเปลยนไทปเองนะ

เชน int fontSize = Integet.parseInt(getParameter(“size”));

• จรงๆ คนทำาเวบอาจจะใหพารามเตอรมาไมครบ ดงนนในโคดเราจะตองดกวาเปน null หรอไมดวย

• ดตวอยาง applet ทวาดบารชารต

Html ของ applet barchart<applet code="Chart.class" width=400 height=300><param name="title" value="Diameters of the Planets"/><param name="values" value="9"/><param name="name.1" value="Mercury"/><param name="name.2" value="Venus"/><param name="name.3" value="Earth"/><param name="name.4" value="Mars"/><param name="name.5" value="Jupiter"/><param name="name.6" value="Saturn"/><param name="name.7" value="Uranus"/><param name="name.8" value="Neptune"/><param name="name.9" value="Pluto"/><param name="value.1" value="3100"/><param name="value.2" value="7500"/><param name="value.3" value="8000"/><param name="value.4" value="4200"/><param name="value.5" value="88000"/><param name="value.6" value="71000"/><param name="value.7" value="32000"/><param name="value.8" value="30600"/><param name="value.9" value="1430"/></applet>

จรงๆจะสรางอารเรยขนมาแลวใหทำางานอยในโคดกได แตแบบนจะเปลยนกราฟงายกวา

โดยไมตองคอมไพลโคดใหม และกยงทำา applet สองอนในหนาเวบเดยว

ไดดวย แคใสพารามเตอรคนละชดกน

รปราง ตอนรนออกมาแลวจะเปนแบบน

Code ของ applet barchartimport javax.swing.*;

public class Chart extends JApplet{ public void init() { EventQueue.invokeLater(new Runnable() { public void run() { String v = getParameter("values"); if (v == null) return; int n = Integer.parseInt(v); double[] values = new double[n]; String[] names = new String[n]; for (int i = 0; i < n; i++) { values[i] = Double.parseDouble(getParameter("value." + (i + 1))); names[i] = getParameter("name." + (i + 1)); }

add(new ChartComponent(values, names, getParameter("title"))); } }); }}

class ChartComponent extends JComponent{ /** * Constructs a ChartComponent. * @param v the array of values for the chart * @param n the array of names for the values * @param t the title of the chart */ public ChartComponent(double[] v, String[] n, String t) { values = v; names = n; title = t; }

public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g;

// compute the minimum and maximum values if (values == null) return; double minValue = 0; double maxValue = 0;

for (double v : values) { if (minValue > v) minValue = v; if (maxValue < v) maxValue = v; } if (maxValue == minValue) return;

int panelWidth = getWidth(); int panelHeight = getHeight();

Font titleFont = new Font("SansSerif", Font.BOLD, 20); Font labelFont = new Font("SansSerif", Font.PLAIN, 10);

// compute the extent of the title FontRenderContext context = g2.getFontRenderContext(); Rectangle2D titleBounds = titleFont.getStringBounds(title, context); double titleWidth = titleBounds.getWidth(); double top = titleBounds.getHeight();

ขอมลตางๆ วาฮารดแวรจะวาดฟอนตอยางไร เกบไวใชในการวาดฟอนตทหลง

Returns the logical bounds of the specified String in the specified FontRenderContext

// draw the title double y = -titleBounds.getY(); // ascent double x = (panelWidth - titleWidth) / 2; g2.setFont(titleFont); g2.drawString(title, (float) x, (float) y);

// compute the extent of the bar labels LineMetrics labelMetrics = labelFont.getLineMetrics("", context); double bottom = labelMetrics.getHeight();

y = panelHeight - labelMetrics.getDescent(); g2.setFont(labelFont);

// get the scale factor and width for the bars double scale = (panelHeight - top - bottom) / (maxValue -

minValue); int barWidth = panelWidth / values.length;

Y coordinate of the upper-left corner

The baseline of the first character is at position (x, y)

encapsulates the measurement information associated with a run of text

distance from the baseline to the descender line. ->the line beneath the lowercase ‘j’ and ‘y.’

// draw the bars for (int i = 0; i < values.length; i++) { // get the coordinates of the bar rectangle double x1 = i * barWidth + 1; double y1 = top; double height = values[i] * scale; if (values[i] >= 0) y1 += (maxValue - values[i]) * scale; else { y1 += maxValue * scale; height = -height; }

// fill the bar and draw the bar outline Rectangle2D rect = new Rectangle2D.Double(x1, y1, barWidth - 2, height); g2.setPaint(Color.RED); g2.fill(rect); g2.setPaint(Color.BLACK); g2.draw(rect);

// draw the centered label below the bar Rectangle2D labelBounds = labelFont.getStringBounds(names[i], context);

double labelWidth = labelBounds.getWidth(); x = x1 + (barWidth - labelWidth) / 2; g2.drawString(names[i], (float) x, (float) y); } } // จบ paintConponent

private double[] values; private String[] names; private String title;} // จบคลาส

เมธอดอนๆทนาสนใจ เวลาใชตอง override

public String getAppletInfo()รเทรนขอมล ผเขยน เวอรชน ลขสทธ

public String[][] getParameterInfo()รเทรนวา แอพเพลต รบพารามเตอรอะไรไดบาง

แตละrow จะมขอมลName, type และคำาอธบายพารามเตอร

ไฟลภาพกบเสยง• ภาพเปน png, gif, jpeg• เสยงเปน au, aiff, wav, midi• ตองใช location ของไฟลพวกนเปน relative urlgetDocumentBase รเทรน url ของหนาเวบทม

แอพเพลตgetCodeBase รเทรน url ของโฟลเดอร codebase

ของแอพเพลต

ตวอยางการเอาไฟลภาพกบเสยงมาใชImage cat = getImage(getCodeBase(),

“images/cat.gif”);

AudioClip meaw = getAudioClip(getCodeBase(), “audio/meaw.au”);

เลนเสยง โดย play(getCodeBase() ,“audio/meaw.au”)

void play(URL url) เลนไฟลจาก url เลยvoid play(URL url, String name)สตรงจะบอก relative path จากพารามเตอรแรก

AudioClip getAudioClip(URL, String name) กจะมสองเวอรชนเหมอนกน ถาหาไฟลไมเจอมนจะรเทรน null

getImage กเปนเชนเดยวกนน

Applet สงงาน browser ได• ตอง getAppletContext กอน คอเปนการเอาตว

เชอมระหวาง บราวเซอรกบโคดมาใช

สงขอมลระหวางสองแอพเพลต• ถาม <applet code =“Chart.class” width = “100” height=“100”

name=“Chart1”>เราสามารถเรยกพอยตเตอรของแอพเพลตขนมาไดApplet chart1 = getAppletContext().getApplet(“Chart1”);

แลวเรากเรยกเมธอดตางๆ ของchart1 จากอก applet ไดแลว เชน((Chart) chart1).setData(…..);

-

ตวอยาง เอาทกแอพเพลตออกมาปรน

Enumeration<Applet> e = getAppletContext().getApplets()l

While(e.hasMoreElements()){

Applet a = e.nextElement();System.out.println(a.getClass().getName());

• สงบราวเซอรใหพมพสตรงลง status lintshowStatus (”…”);

• สงบราวเซอรใหเปลยนไปดเวบอนURL u = new URL(“http:// ….”);getAppletContext().showDocument(u);

จะใหเปดหนาใหมกได ด showDocument เวอรชนตางๆเอานะ

Two in one เปนทง application และ applet

• สราง Jframe ทในเฟรมม applet อยใน content pane

public class AppletFrame extends JFrame implements AppletStub, AppletContext

{ public AppletFrame(Applet anApplet) { applet = anApplet; add(applet);……

• ใน main ใหเรา show AppletFrame

public class AppletApplication extends NotHelloWorldApplet{ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { AppletFrame frame = new AppletFrame(new

NotHelloWorldApplet()); frame.setTitle("NotHelloWorldApplet"); frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); }

• เปลยน setVisible ของ AppletFrame ใหเรยก init กบ start

public void setVisible(boolean b) { if (b) { applet.init(); super.setVisible(true); applet.start(); } else { applet.stop(); super.setVisible(false); applet.destroy(); } }

• ตองระวง วาเกดไปเรยก getAppletContext().getApplet(“Chart1”) โดยทเปนแอพพลเคชน จะได null ซงทำาโปรแกรมเจงได

• ดงนนเราแกไดโดยใหโคดเราอมพลเมนอนเตอรเฟสอกสองตว แลวมา setStub

• ดโคดเอา มไฟลให

top related