csc 243 – java programming, spring, 2014 april 22, 2014 week 13, japplets

8
CSC 243 – Java Programming, Spring, 2014 April 22, 2014 Week 13, JApplets

Upload: jerome-strickland

Post on 19-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 243 – Java Programming, Spring, 2014 April 22, 2014 Week 13, JApplets

CSC 243 – Java Programming, Spring, 2014

April 22, 2014Week 13, JApplets

Page 2: 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.

Page 3: CSC 243 – Java Programming, Spring, 2014 April 22, 2014 Week 13, JApplets

Example from Spring 2012

• ~parson/JavaLang/eventshuffle• SwingAppletShuffle.java• XYZshuffle.html• XYZshuffle2.html• XYZshuffle3.php– http://bill.kutztown.edu/~parson/XYZshuffle2.html– See code examples below.

Page 4: CSC 243 – Java Programming, Spring, 2014 April 22, 2014 Week 13, JApplets

Java code for the appletpublic 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.

Page 5: CSC 243 – Java Programming, Spring, 2014 April 22, 2014 Week 13, JApplets

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);

Page 6: CSC 243 – Java Programming, Spring, 2014 April 22, 2014 Week 13, JApplets

buildunix/windowsbuilds the JAR file

cd .. && export CLASSPATH=`pwd`cd ./eventshuffle && /bin/rm -f eventshuffle.jar

&& javac *.javacd .. && jar vcfm eventshuffle/eventshuffle.jar

eventshuffle/manifest.txt eventshuffle

This last step bundles all files in and below eventshuffle into archive eventshuffle.jar.

Page 7: CSC 243 – Java Programming, Spring, 2014 April 22, 2014 Week 13, JApplets

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.

Page 8: CSC 243 – Java Programming, Spring, 2014 April 22, 2014 Week 13, JApplets

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.