- The name of the class file
- The location of the class file
- How the applet sits on the Web page (size, location, and so on)
Object <- Component <- Container <- Panel <- Applet <- Window <- Frame
Example:
import java.awt.*; import java.applet.*; public class ByeWorld extends Applet { // Override the paint method public void paint(Graphics g) { Font f = new Font("System", Font.BOLD, 18); g.setFont(f); g.drawString("This is the final Intro to Java class", 25, 50); } } ... Compile the .java file into .class file, then in the same directory, have a ... html file the contains at least the following line: (More detail later on) <APPLET CODE="ByeWorld.class" WIDTH=100 HEIGHT=100> </APPLET>
- Open HTML files as local files from the browsers,
- Download the HTML file from the net and then run with it
- Applets can never run any local executable program
- Applets can only communciate with the originating host (the host where the applet is downloaded)
- On local file system can be accessed by the applet
- Applet can only find out Java version, the name and version of the OS, file separator and line separator. Applet cannot find out user's name, email, etc.
(JavaScript on the other hand can find out browser's history information, brower's version, etc)- Applets can only pop up windows with warning message (saying something like "Untrusted Java Applet Window")
- Applets is said to be running in the "sandbox"
- All program code is interpreted by the Java Virtual Machine, security is enforced within the java interpreter (as well as JIT compiler is you can converting into native code)
- A security manager (just anothe Java class) checks all sensitive operations (operations defined in some interfaces) in the Java run-time library
- Applets can be signed to identify their origin (supported by IE 4.0, Netscape 4.0 ?)
(This is what Active X is using)
There are four conceptal "phases" in the life cycle (init, start, stop, destroy)
init
- Do initialization for the applet
- Like a constructor (called by the system/browser when the applet is launched for the first item)
- Process parameter (PARM from the HTML file)
- Declare all the UI components in here
start
- Call automatically after the init method
- Automatically call if user returned to the page with applet
- start can be called repeatly VS init only gets called once
- Used to restart threads
- Only implemented if your applet if doing something specials when user leave the page
stop
- Call automatically when user move off the current html page with applet(s)
- Used to stop time-consuming event (audio files, animation, calculations, etc)
- Only implemented if your applet if doing something specials when user leave the page
destroy
- Call automatically when the browser shuts down normally
- Used to reclaim non-memory-dependent resources such as graphics context
- Memory resource will be collected by the Java under the cover
- Normally, Java will call stop then destroy when the browser is shutting down
<HTML>
<HEAD>
<TITLE>Week 10</TITLE>
</HEAD>
<BODY>
<H1>Java Test</H1>
<APPLET CODE="ByeWorld.class" WIDTH=100 HEIGHT=100></APPLET>
</BODY>
</HTML>
<!-- If your applet is called ByeWorld.class and is in directory myDir and myDir is one level down from your current web page, then the following HTML code will be able to use to call the applet --> <APPLET CODE="ByeWorld.class" CODEBASE="myDir" WIDTH=100 HEIGHT=100></APPLET> ... ... ... <!-- If you have a jar file need to be loaded, you can use the ARCHIVE ... think about a jar files as a compressed file that will contains a bunch of files for your applet --> <APPLET CODE="Hi.class" ARCHIVE="lotus.jar, sinn/mySrc.jar" WIDTH=888 HEIGHT=188></APPLET>
<!-- For non java enabled browser, we can use the same old ALT tag to display information/error message -->
<APPLET CODE="hi.class" WIDTH=888 HEIGHT=188 ALT="Your browser is not java enabled, please download a new one"></APPLET>
For normal loading method of applet:
<APPLET CODE="hi.class" WIDTH = 400 HEIGHT=300></APPLET>
We can use the following to load all the class files at once:
<APPLET CODE="hi.class" ARCHIVE="hi2.jar" WIDTH=300
HEIGHT=500></APPLET>
In order to make a JAR file, use the jar tool under \jdk\bin
jar <options> File1 File2 ...
For example: jar cf hi2.jar *.java logo.gif
// c - create new archive, f - use 2nd parameter as JAR file name
- The local file system, relative to the class path
- In a JAR file
- Or on a Web server
URL myUrl = hi.class.getResource("copyright.txt"); InputStream in = url.openStream();
... or, put everything in one line ...
InputStream in = hi.class.getResourceAsStream("copyright.txt");
Applets have the ability to use parameters that are embedded into the HTML file. This is done by using the PARAM tag
<APPLET CODE="Hi.class" WIDTH=300, HEIGHT=200> <PARM NAME=hidear VALUE="This is Week10"> </APPLET> .... .... // Then, in the java code, we have ... import java.applet.*; import java.awt.*; public class Hi extends Applet { public void paint(Graphics g) { String inputString = getParameter("hidear"); g.drawString("Finally ..." + inputString, 25, 50); } }
Use the lab exercise from Week 9 and then convert that into an applet. Use Week10.java as the main class for this week and name the HTML file as week10.html
A Thread is a single sequential flow of control within a program. In Java, thread is in the user level (vs operating system level). Threads can be created easily in order to provide base for concurrent applications.
In this lab, we will use the java classes from Week 7 and create a threaded echo server that will be able to accept multiple clients.
Note: