HTML � HTML is the HyperText Markup Language. � HTML files are text files featuring semantically tagged elements. � HTML filenames are suffixed with .htm or .html. Here's a simple HTML file: <html> <head> <title>My First HTML Document</title> </head> <body> <h1>A level one heading</h1> Hello there. This is <STRONG>very</STRONG> important. </body> </html>
Attributes � Some tags have attributes . An attribute is a name, followed by an = sign, followed by the value. <h1 align="center">A level one heading</h1>
URLs � URL stands for uniform resource locator. � A URL is a pointer to a particular resource on the Internet at a particular location. http://metalab.unc.edu/javafaq/course/week5/exercises.html ftp://ftp.macfaq.com/pub/macfaq/ are both URLs. protocol://hostname[:port]/path/filename#section
The protocol file a file on your local disk ftp an FTP server http a World Wide Web server gopher a Gopher server mailto an email address news a Usenet newsgroup telnet a connection to a Telnet-based service WAIS a WAIS server
The parts of a URL <A NAME="xtocid1902914">Comments</A> A URL that points to this name, includes not only the filename, but also the named anchor separated from the rest of the URL by a # like this http://metalab.unc.edu/javafaq/javafaq.html#xtocid1902914
Links <A HREF="http://metalab.unc.edu/javafaq/course/week5/exercises.html"> exercises </A>
Hello World: The Applet import java.applet.Applet; import java.awt.Graphics; public class HelloWorldApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } }
<HTML> <HEAD> <TITLE> Hello World </TITLE> </HEAD> <BODY> This is the applet:<P> <applet code="HelloWorldApplet" width="150" height="50"> </applet> </BODY> </HTML>
What is an Applet? � Four definitions of applet: – A small application – A secure program that runs inside a web browser – A subclass of java.applet.Applet – An instance of a subclass of java.applet.Applet
public class Applet extends Panel java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet
The APPLET HTML Tag <APPLET CODE="HelloWorldApplet" CODEBASE="http://www.foo.bar.com/classes" WIDTH="200" HEIGHT="200"> </APPLET>
� If the applet is in a non-default package, then the full package qualified name must be used. For example, <APPLET CODE="com.macfaq.greeting.HelloWorldApplet" CODEBASE="http://www.example.com/classes" WIDTH="200" HEIGHT="200"> </APPLET>
Spacing Preferences <applet code="HelloWorldApplet" CODEBASE="http://www.foo.bar.com/classes" width=200 height=200 ALIGN=RIGHT HSPACE=5 VSPACE=10> </APPLET>
Alternate Text <applet code="HelloWorldApplet" CODEBASE="http://www.foo.bar.com/classes" width="200" height="200" ALIGN="RIGHT" HSPACE="5" VSPACE="10" ALT="Hello World!"> </APPLET> <applet code="HelloWorldApplet" CODEBASE="http://www.foo.bar.com/classes" width=200 height=200 ALIGN=RIGHT HSPACE=5 VSPACE=10 ALT="Hello World!"> Hello World!<P> </APPLET>
Naming Applets <APPLET CODE="HelloWorldApplet" NAME="Applet1" CODEBASE="http://www.foo.bar.com/classes" WIDTH="200" HEIGHT="200" align="right" HSPACE="5" VSPACE="10" ALT="Hello World!"> Hello World!<P> </APPLET>
JAR Archives <APPLET CODE="HelloWorldApplet" WIDTH="200" HEIGHT="100" ARCHIVES="HelloWorld.jar"> <hr> Hello World! <hr> </APPLET>
The OBJECT Tag <OBJECT classid="MyApplet" CODEBASE="http://www.foo.bar.com/classes" width=200 height=200 ALIGN=RIGHT HSPACE=5 VSPACE=10> </OBJECT> � You can support both by placing an <APPLET> element inside an <OBJECT> element like this: <OBJECT classid="MyApplet" width="200" height="200"> <APPLET code="MyApplet" width="200" height="200"> </APPLET> </OBJECT>
Finding an Applet's Size import java.applet.*; import java.awt.*; public class SizeApplet extends Applet { public void paint(Graphics g) { Dimension appletSize = this.getSize(); int appletHeight = appletSize.height; int appletWidth = appletSize.width; g.drawString("This applet is " + appletHeight +" pixels high by “ + appletWidth + " pixels wide.",15, appletHeight/2); } }
Passing Parameters to Applets import java.applet.*; import java.awt.*; public class DrawStringApplet extends Applet { private String defaultMessage = "Hello!"; public void paint(Graphics g) { String inputFromPage = this.getParameter("Message"); if (inputFromPage == null) inputFromPage = defaultMessage; g.drawString(inputFromPage, 50, 25); } }
� You also need an HTML file that references your applet. The following simple HTML file will do: <HTML> <HEAD> <TITLE> Draw String </TITLE> </HEAD> <BODY> This is the applet:<P> <APPLET code="DrawStringApplet" width="300" height="50"> <PARAM name="Message" value="Howdy, there!"> This page will be very boring if your browser doesn't understand Java. </APPLET> </BODY> </HTML>
Processing An Unknown Number Of Parameters <PARAM name="Line1" value="There once was a man from Japan"> <PARAM name="Line2" value="Whose poetry never would scan"> <PARAM name="Line3" value="When asked reasons why,"> <PARAM name="Line4" value="He replied, with a sigh:"> <PARAM name="Line5" value="I always try to get as many syllables into the last line as I can.">
Processing An Unknown Number Of Parameters import java.applet.*; import java.awt.*; public class PoetryApplet extends Applet { private String[] poem = new String[101]; private int numLines; public void init() { String nextline; for (numLines = 1; numLines < poem.length; numLines++) { nextline = this.getParameter("Line" + numLines); if (nextline == null) break; poem[numLines] = nextline; } numLines--; } public void paint(Graphics g) { int y = 15; for (int i=1; i <= numLines; i++) { g.drawString(poem[i], 5, y); y += 15; } } }
What Can an Applet Do? � An applet can: – Draw pictures on a web page – Create a new window and draw in it. – Play sounds. – Receive input from the user through the keyboard or the mouse. – Make a network connection to the server from which it came and can send to and receive arbitrary data from that server.
� An applet cannot: – Write data on any of the host's disks. – Read any data from the host's disks without the user's permission. In some environments, notably Netscape, an applet cannot read data from the user's disks even with permission. – Delete files – Read from or write to arbitrary blocks of memory, even on a non-memory-protected operating system like the MacOS. All memory access is strictly controlled. – Make a network connection to a host on the Internet other than the one from which it was downloaded. – Call the native API directly (though Java API calls may eventually lead back to native API calls). – Introduce a virus or trojan horse into the host system. – An applet is not supposed to be able to crash the host system. However in practice Java isn't quite stable enough to make this claim yet.
The Basic Applet Life Cycle 1. The browser reads the HTML page and finds any <APPLET> tags. 2. The browser parses the <APPLET> tag to find the CODE and possibly CODEBASE attribute. 3. The browser downloads the .class file for the applet from the URL found in the last step. 4. The browser converts the raw bytes downloaded into a Java class, that is a java.lang.Class object. 5. The browser instantiates the applet class to form an applet object. This requires the applet to have a noargs constructor. 6. The browser calls the applet's init() method. 7. The browser calls the applet's start() method. 8. While the applet is running, the browser passes any events intended for the applet, e.g. mouse clicks, key presses, etc., to the applet's handleEvent() method. Update events are used to tell the applet that it needs to repaint itself. 9. The browser calls the applet's stop() method. 10. The browser calls the applet's destroy() method.
The Basic Applet Life Cycle � All applets have the following four methods: public void init(); public void start(); public void stop(); public void destroy();
The Coordinate System
Graphics Objects � In Java all drawing takes place via a Graphics object. This is an instance of the class java.awt.Graphics.
Recommend
More recommend