JAVA Programming Language JAVA Applet Java Applet >APPLET< >PARAM< HTML JAR 2
JAVA APPLET HTML HTML <APPLET> main( ). public Applet 3 (HelloWorld.html) <HTML> <HEAD> <!-- Generated by Kawa IDE --> <TITLE>Applet title</title> </HEAD> <BODY> <H1>First Heading</H1> <APPLET CODE=HelloWorld WIDTH=220 HEIGHT=150></APPLET> </BODY> </HTML> 4
(HelloWorld.java) import java.awt.graphics; import java.applet.applet; public class HelloWorldextends Applet { public void paint(graphics g) { g.drawstring("helloworld!!", 70, 30); C:\>javac HelloWorld.java C:\>appletviewer HelloWorld.html C:\> 5 6
(1) public void init( ); public void start( ); public void stop( ); public void destory( ); public void paint(graphics g); 7 (1) 8
(2) (AppletMethodTest.html) <HTML> <HEAD> <!-- Generated by Kawa IDE --> <TITLE>Applet title</title> </HEAD> <BODY> <H1>First Heading</H1> <APPLET CODE=AppletMethodTest WIDTH=150 HEIGHT=150></APPLET> </BODY> </HTML> 9 (3) (AppletMethodTest.java) import java.awt.*; import java.applet.applet; publicclass AppletMethodTest extends Applet { TextArea ta; int count; public AppletMethodTest() { ta = newtextarea(4, 20); add(ta); count=0; public voidinit() { ta.append("init"+(count)+"\n"); System.out.println("init"+(count++)); public voidstart() { ta.append("start"+(count)+"\n"); System.out.println("start"+(count++)); publicvoidstop() { ta.append("stop"+(count)+"\n"); System.out.println("stop"+(count++)); public voiddestroy() { ta.append("destroy"+(count)+"\n"); System.out.println("destroy"+(count++)); public voidpaint(graphics g) { ta.append("paint"+(count)+"\n"); System.out.println("paint"+(count++)); // Display the string g.drawstring("hello World!", 8, 32); 10
(4) HTML APPLET PARAM (Attribute) code - codebase - codebase HTML archive -.class zip jar Netscape 3.0 align - name - vspace - ( : ) hspace - ( : ) width - ( : ) height - ( : ) 11 (4) Graphics drawxxx( ) drawstring(string msg, int x, int y); drawline(int x1, int y1, int x2, int y2); 12
(4) Graphics drawxxx( ) ( ) drawrect(int x, int w, int h); drawoval(int x, int y, int w, int h); 13 (4) Graphics drawxxx( ) ( ) drawroundrect(int x, int y, int w, int h, int rw, int rh); drawarc(int x, int w, int h, int a, int b); 14
(4) fillxxx( ) fillrect(int x, int y, int w, int h); filloval(int x, int y, int w, int h); fillroundrect(int x, int y, int w, int h, int rw, int rh); fillarc(int x, int y, int w, int h, int a, int b); 15 (5) <APPLET> <PARAM> <HTML> <HEAD> <!-- Generated by Kawa IDE --> <TITLE>Applet Test</TITLE> </HEAD> <BODY> <H1>First Heading</H1> <APPLET CODE=ParameterTest WIDTH=300 HEIGHT=300> <PARAM NAME = LINK0 VALUE = "http://www.yahoo.co.kr"> <PARAM NAME = LINK1 VALUE = "http://www.aiit.or.kr"> <PARAM NAME = LINK2 VALUE = "http://www.soongsil.ac.kr"> <PARAM NAME = LINK3 VALUE = "http://www.daum.net"> <PARAM NAME = LINK4 VALUE = "http://www.javastudy.co.kr"> <PARAM NAME = Title VALUE = "Let's go"> </APPLET> </BODY> </HTML> 16
(6) <APPLET> <PARAM> import java.awt.*; import java.awt.event.*; import java.applet.applet; public class ParameterTest extends Applet { List linklist; Label title; public ParameterTest() { setlayout(new BorderLayout()); linklist = new List(2); linklist.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { ); System.out.println(e); title = new Label("No Title"); add("north", title); add("center", linklist); public void init() { int linkcount=0; String param; while((param=getparameter("link"+linkcount))!= null) { System.out.println(param); System.out.println(param); linklist.add(param); linkcount++; if((param=getparameter("title"))!= null) { title.settext(param); 17 URL getcodebase( ); URL getdocumentbase( ); Image getimage(url url); Image getimage(url url, String name); void resize(dimension d); void resize(int width, int height); AudioClip getaudioclip(url url); AudioClip getaudioclip(url url, String name); static AudioClip newaudioclip(url url); void play(url url, String name); void loop( ); void play( ); void stop( ); 18
<HTML> <HEAD> <!-- Generated by Kawa IDE --> <TITLE>Applet title</title> </HEAD> <BODY> <H1>First Heading</H1> <APPLET CODE=ImageSoundTest WIDTH= 300 HEIGHT= 300> </APPLET> </BODY> </HTML> 19 import java.net.url; import java.awt.*; import java.applet.*; public class ImageSoundTest extends Applet { Image image; AudioClip audioclip, startclip; public void init() { MediaTracker tracker; startclip = Applet.newAudioClip( new URL("canyon.mid")); catch(java.net.malformedurlexception e) { System.out.println(e); public void start() { if(audioclip!= null) audioclip.loop(); if(startclip!= null) startclip.loop(); tracker = new MediaTracker(this); image = getimage(getdocumentbase(), "WuChienLien.jpg"); tracker.addimage(image, 0); try { tracker.waitforall(); catch(interruptedexception e) { System.out.println(e); return; resize(image.getwidth(this), image.getheight(this)); audioclip = getaudioclip(getcodebase(), "AudioClip.au"); try { public void stop() { if(audioclip!= null) audioclip.stop(); if(startclip!= null) startclip.stop(); public void paint(graphics g) { g.drawimage(image, 0, 0, this); 20
drawstring( ) 21 MediaTracker init( ). MediaTracker MediaTracker(Component comp); // // addimage(image image, int id); remove(image(image image); // // 22
1. 2. 1. Void showstatus(string msg); // 2. AppletContextgetApplet( ); // void showstatus(string status); // void showdocument(url url); // URL 23 <HTML> <HEAD> <!-- Generated by Kawa IDE --> <TITLE>Applet title</title> </HEAD> <BODY> <H1>First Heading</H1> <APPLET CODE=AppletBrowserCommTest WIDTH=300 HEIGHT=300> </APPLET> </BODY> </HTML> 24
import java.awt.event.*; import java.net.*; import java.applet.*; public class AppletBrowserCommTest extends Applet { private List urls = new List(4, false); private Choice target = new Choice(); private AppletContext appletcontext=null; public void init() { setlayout(new BorderLayout()); try { ); appletcontext.showdocument(new URL(e.getActionCommand()), target.getselecteditem()); catch(malformedurlexception ee) { appletcontext.showstatus("url Exception: " + ee); add("center", urls); urls.additem("http://park.konkuk.ac.kr"); urls.additem("http://www. konkuk.ac.kr"); urls.additem("http://www. aiit.or.kr"); urls.additem("http://www.sun.com"); urls.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { appletcontext = getappletcontext(); System.out.println(e.getActionCommand()); appletcontext.showstatus("my status: " +e.getactioncommand()+", " +target.getselecteditem()); target = new Choice(); target.add("_self"); target.add("_parent"); target.add("_top"); target.add("_blank"); target.add("new"); add("north", target); 25 HTML <HTML> <HEAD> <!-- Generated by Kawa IDE --> <TITLE>Applet title</title> </HEAD> <BODY> <CENTER> <H1>Communication between Two Applet</H1> <APPLET CODE=MyApplet1 WIDTH=250 HEIGHT=250 NAME = MyApplet1> </APPLET> <APPLET CODE=MyApplet2 WIDTH=200 HEIGHT=50 NAME = MyApplet2> </APPLET> </BODY> </HTML> 26
HTML //MyApplet1.java import java.awt.*; import java.applet.*; public class MyApplet1 extends Applet { public static String myname = "My First Applet" ; Label label; public void init() { setsize(250, 250 ); label = new Label(myName); add(label); public String tostring() { return myname ; // MyApplet2.java import java.awt.*; import java.applet.*; public class MyApplet2 extends Applet { Label label; MyApplet1 anapplet; public void init() { setlayout(new BorderLayout()); setsize(200, 50); for(int i=0;i<10;i++) { try { Thread.sleep(200 ); catch (Exception e) { anapplet=(myapplet1)(getappletcontext().getapplet("myapplet1" )); if(anapplet!= null) { break; System.out. println("applet: "+anapplet+", "+anapplet.tostring()); add("north", new Label("My Second Applet")); if(anapplet!= null) { label = newlabel(anapplet.tostring()); else { label = newlabel("unknown Applet"); System.out. println("label: "+label); add("south", label); 27 AppletContext public AppletContext getapplet( ); public String getappletinfo( ); 28
<HTML> <HEAD> <!-- Generated by Kawa IDE --> <TITLE>Applet title</title> </HEAD> <BODY> <H1>First Heading</H1> <APPLET CODE=BrowserIDTest WIDTH=300 HEIGHT=300></APPLET> </BODY> </HTML> 29 import java.lang.*; import java.util.*; import java.awt.*; import java.applet.applet; public void paint(graphics g) { g.drawstring("browser ID: " + browserid, 10, 20); g.drawstring("getappletcontext(): " + getappletcontext(), 10, 40); g.drawstring("getappletcontext().tostring(): " + getappletcontext().tostring(), 10, 60); g.drawstring("getappletinfo(): " + getappletinfo(), 10, 80); public class BrowserIDTest extends Applet { private String browserid = "Application"; public void init() { String appletcontext = getappletcontext().tostring(); if(appletcontext.startswith( "sun.applet.appletviewer")) { browserid = "Appletviewer"; else if(appletcontext.startswith("netscape.applet.")) { browserid = "Communicator"; else if(appletcontext.startswith("com.ms.applet.")) { browserid = "Internet Explorer"; else if(appletcontext.startswith("sunw.hotjava.tags.tagappletpanel")) { browserid = "HotJava"; else if(appletcontext.startswith("sun.plugin.navig.win32.appletplugin")) { browserid = "Communicator Plugin "; else if(appletcontext.startswith("sun.plugin.ocx.activexapplet")) { browserid = "Internet Explorer Plugin"; System.out.println("browser ID: " + browserid); System.out.println("getAppletContext(): " + getappletcontext()); System.out.println("getAppletContext().toString(): " + getappletcontext().tostring()); System.out.println("getAppletInfo(): " + getappletinfo()); 30
<HTML> <HEAD> <!-- Generated by Kawa IDE --> <TITLE>Applet title</title> </HEAD> <BODY> <H1>First Heading</H1> <APPLET CODE=ScreenSizeTest WIDTH=300 HEIGHT=300></APPLET> </BODY> </HTML> 31 import java.awt.*; import java.applet.applet; public class ScreenSizeTest extends Applet { public void init() { Panel p = new Panel(); Button b1 = newbutton( "B1"); Button b2 = newbutton( "B2"); setlayout(new FlowLayout(FlowLayout.CENTER)); p.add(b1); p.add(b2); add(p); public void paint(graphics g) { Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); Rectangle r = getbounds(); g.drawstring("screen size = " + d.width + "x"+ d.height, 10, 100); g.drawstring("location = " + getlocation().x + "," + getlocation().x, 10, 130); g.drawstring("bounds = " + r.x + "," + r.y + "," + r.width + "," + r.height, 10, 150); System.out.println("Screen size = " + d.width + "x" + d.height); System.out.println("Location = " + getlocation().x + "," + getlocation().x); System.out.println("Bounds = " + r.x + "," + r.y + "," + r.width + "," + r.height); 32
JAR(Java Archive) JAR ZIP, 33 JAR(Java Archive) Usage: jar {ctxu [vfm0m] [jar-file] [manifest-file] [-C dir] files... Options: - c(create) - t(table) - u(update) - x(extract) - v - f - m - 0 - M - -C : jar cf MyArchive.jar *.class *.gif *.au jar cvf classes.jar MyClass1.class Myclass2.class jar cvf0 classes.jar MyClass1.class Myclass2.class jar cvfm classes.jar mymanifest C MyDir/. jar tf classes.jar 34
JAR(Java Archive) JAR JAR <APPLET> ARCHIVE JAR 1.1 JAR, <APPLET CODE = Myclass.class ARCHIVE = MyArchive.jar WIDTH = 300 HEIGHT = 300> </APPLET> 35