CSC 243 – Java Programming, Spring, 2014 April 22, 2014 Week 13, JApplets
JApplet JApplet, like JFrame, is an outer container into which you can add() JPanels and graphical controls. Unlike a JFrame, a JApplet is not an outer, first-class window. A JApplet appears inside a browser. A browser downloads a JApplet from a browser via an HTML page.
Example from Spring 2012 ~parson/JavaLang/eventshuffle SwingAppletShuffle.java XYZshuffle.html XYZshuffle2.html XYZshuffle3.php – – See code examples below.
Java code for the applet public class SwingAppletShuffle extends JApplet /** * init(), start(), stop() and destroy() are the four lifecycle * methods of a JApplet. The browser invokes init() to initialize any * applet-specific data structures *before* starting the applet. **/ public void init() {... } is invoked by browser when applet is loaded. public void start() {... } is invoked by browser to start applet. public void stop() {... }is invoked by browser to stop applet. public void destroy() {... } is invoked by browser to terminate applet. Browser can cycle between start() and stop() calls to the applet.
Obtaining data in the Java code The applet can fetch parameters from the HTML page that loaded the applet. String string1 = getParameter("string1"); String string2 = getParameter("string2"); The applet can open files for reading from the JAR file archive. Scanner scanner = new Scanner(getClass().getResourceAsStream("english.0.txt")); Ishuffle unshuffler = new XYZshuffle(scanner);
buildunix/windows builds the JAR file cd.. && export CLASSPATH=`pwd` cd./eventshuffle && /bin/rm -f eventshuffle.jar && javac *.java cd.. && jar vcfm eventshuffle/eventshuffle.jar eventshuffle/manifest.txt eventshuffle This last step bundles all files in and below eventshuffle into archive eventshuffle.jar.
SwingAppletShuffle This example applet simply pops up a series of SwingShuffle JFrame objects, one per two- string solution, outside the browser frame. A more integrated solution would add() JPanels, etc. into the JApplet class itself, populating space inside the browser window. This example borrows logic from SwingShuffle.main in starting up frames.
gmake deploy Deploy by copying the html, php and JAR file (Java archive) that contains the class and data files for the applet to a directory accessible to a browser. cp *.html *.php eventshuffle.jar /www/faculty/parson/ Run “gmake deploy” for details.