Java applet import java awt import javax swing
- Slides: 33
Java applet
import java. awt. *; import javax. swing. *; /** ตอง extends ตวน * @version 1. 22 2007 -06 -12 * @author Cay Horstmann */ public class Not. Hello. World. Applet extends JApplet เรยกเมอโหลดครงแร { ก public void init() สงการอพเดท user interface ไปท event { dispatch thread เพอใหรนขนานกบ main Event. Queue. invoke. Later(new Runnable() ตอไปนถาจะเซตอะไรใน GUI { กตองทำในนจะดกวา public void run() invoke. Later รเทรนทนท { JLabel label = new JLabel("Not a Hello, World applet", Swing. Constants. CENTER); add(label); }); } ตอจากนตองสราง html ทมแทกดงน }
สำหรบ • ไมใช applet tag แลว html 4. 0 ใช object tag แทน <object> codetype=“application/java” classid=“java: My. Applet. class” width=“ 100” height=“ 150” ม codebase ดวย ทำงานเหมอนเดมเลย
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 สองอนในหนาเวบเดยวได ดวย แคใสพารามเตอรคนละช ดกน
import javax. swing. *; Code ของ applet barchart public class Chart extends JApplet { public void init() { Event. Queue. invoke. Later(new Runnable() { public void run() { String v = get. Parameter("values"); if (v == null) return; int n = Integer. parse. Int(v); double[] values = new double[n]; String[] names = new String[n]; for (int i = 0; i < n; i++) { values[i] = Double. parse. Double(get. Parameter("value. " + (i + 1))); names[i] = get. Parameter("name. " + (i + 1)); } add(new Chart. Component(values, names, get. Parameter("title"))); }); } }
class Chart. Component extends JComponent { /** * Constructs a Chart. Component. * @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 Chart. Component(double[] v, String[] n, String t) { values = v; names = n; title = t; } public void paint. Component(Graphics g) { Graphics 2 D g 2 = (Graphics 2 D) g; // compute the minimum and maximum values if (values == null) return; double min. Value = 0; double max. Value = 0;
for (double v : values) { if (min. Value > v) min. Value = v; if (max. Value < v) max. Value = v; } if (max. Value == min. Value) return; int panel. Width = get. Width(); int panel. Height = get. Height(); Font title. Font = new Font("Sans. Serif", Font. BOLD, 20); Font label. Font = new Font("Sans. Serif", Font. PLAIN, 10); ขอมลตางๆ วาฮารดแวรจะวาดฟอนตอยางไร เกบไวใชในการวาดฟอนตทหลง // compute the extent of the title Font. Render. Context context = g 2. get. Font. Render. Context(); Rectangle 2 D title. Bounds = title. Font. get. String. Bounds(title, context); double title. Width = title. Bounds. get. Width(); double top = title. Bounds. get. Height(); Returns the logical bounds of the specified String in the specified Font. Render. Context
Y coordinate of the upper-left corner // draw the title double y = -title. Bounds. get. Y(); // ascent double x = (panel. Width - title. Width) / 2; g 2. set. Font(title. Font); g 2. draw. String(title, (float) x, (float) y); The baseline of the first character is at position (x, y) // compute the extent of the bar labels Line. Metrics label. Metrics = label. Font. get. Line. Metrics("", context); double bottom = label. Metrics. get. Height(); encapsulates the measurement information associated with a run of y = panel. Height - label. Metrics. get. Descent(); text g 2. set. Font(label. Font); distance from the baseline to the descender line. ->the line beneath the lowercase ‘j’ and ‘y. ’ // get the scale factor and width for the bars double scale = (panel. Height - top - bottom) / (max. Value - min. Value); int bar. Width = panel. Width / values. length;
// draw the bars for (int i = 0; i < values. length; i++) { // get the coordinates of the bar rectangle double x 1 = i * bar. Width + 1; double y 1 = top; double height = values[i] * scale; if (values[i] >= 0) y 1 += (max. Value - values[i]) * scale; else { y 1 += max. Value * scale; height = -height; } // fill the bar and draw the bar outline Rectangle 2 D rect = new Rectangle 2 D. Double(x 1, y 1, bar. Width - 2, height); g 2. set. Paint(Color. RED); g 2. fill(rect); g 2. set. Paint(Color. BLACK); g 2. draw(rect); // draw the centered label below the bar Rectangle 2 D label. Bounds = label. Font. get. String. Bounds(names[i], context); double label. Width = label. Bounds. get. Width(); x = x 1 + (bar. Width - label. Width) / 2; g 2. draw. String(names[i], (float) x, (float) y); } } // จบ paint. Conponent
private double[] values; private String[] names; private String title; } // จบคลาส
ตวอยางการเอาไฟลภาพกบเสยงมาใช Image cat = get. Image(get. Code. Base(), “images/cat. gif”); Audio. Clip meaw = get. Audio. Clip(get. Code. Base(), “audio/meaw. au”); เลนเสยง โดย play(get. Code. Base() , “audio/meaw. au”)
ตวอยาง เอาทกแอพเพลตออกมาปรน Enumeration<Applet> e = get. Applet. Context(). get. Applets()l While(e. has. More. Elements()) { Applet a = e. next. Element(); System. out. println(a. get. Class(). get. Name());
Two in one เปนทง application และ applet • สราง Jframe ทในเฟรมม pane applet อยใน content public class Applet. Frame extends JFrame implements Applet. Stub, Applet. Context { public Applet. Frame(Applet an. Applet) { applet = an. Applet; add(applet); ……
• ใน main ใหเรา show Applet. Frame public class Applet. Application extends Not. Hello. World. Applet { public static void main(String[] args) { Event. Queue. invoke. Later(new Runnable() { public void run() { Applet. Frame frame = new Applet. Frame(new Not. Hello. World. Applet()); frame. set. Title("Not. Hello. World. Applet"); frame. set. Size(DEFAULT_WIDTH, DEFAULT_HEIGHT); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); frame. set. Visible(true); }); }
• เปลยน set. Visible ของ Applet. Frame ใหเรยก public void set. Visible(boolean b) { if (b) { applet. init(); super. set. Visible(true); applet. start(); } else { applet. stop(); super. set. Visible(false); applet. destroy(); } } init กบ start
- Import java.awt.* import java.awt.event.*
- Swing vs awt
- Java swing
- 자바 사각형 그리기
- G.draw
- Import java.applet.applet
- Applet class
- Java applet adalah
- Import java.applet.applet
- Import javax
- Import javax.swing.*
- Import javax.swing.*
- Java layout 종류
- Import java
- Apa perbedaan antara java swing dengan java awt
- Java import javax
- Java awt swing
- Java awt vs swing
- Extends jframe java
- Event
- Import java.awt.*
- Import java.awt.color;
- Import java.awt.*
- Import java.awt
- Import java.awt.*;
- Import java.awt
- Cracus
- Import java.io.* import java.util.*
- Difference between applet and awt
- Import java.applet.*
- Difference between local and remote applet
- Import javax.servlet
- Import javax.jws.webservice;
- Swing vs awt