|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.awt.Component java.awt.Container java.awt.Panel java.applet.Applet javax.swing.JApplet becker.robots.AppletRunner
public class AppletRunner
There are two ways to display a Robot program on a web page, both are documented here.
The first, and easiest way, is to write a standard robot
application and then to use AppletRunner
to display it.
AppletRunner
simply displays a button on the web page. Clicking
the button will run the application in a separate window. For example,
.
(Try it!)
No programming, other than the original application, is required.
Simply include APPLET
tags, as follows, except that the
capitalized red parts will need to be customized to your situation:
<applet codebase = "classes/" code = "becker.robots.AppletRunner.class" width = "100" height = "25"> <param name="className" value="CLASS_NAME"> <param name="buttonLabel" value="LABEL_TEXT"> </applet>
The parts you need to customize are:
- CLASS_NAME
- The name of the class containing the
main
method to execute. If it the class is part of a package, the package name is also required. Examples includeMyClass
andmyPackage.MyClass
.- LABEL_TEXT
- The text to display on the button that starts the program running. For example, "Run My Applet".
Step 2 is to install the files on a web server. Assume that the web page
is in a directory named public_html
. Then the class files for
the robot application, along with the
class files in becker.jar
must be in the directory
public_html/classes
.
To obtain the class files from becker.jar
, simply unzip it
with a standard zip compression utility. Extract all
the files into public_html/classes/
. If you wish, you may
delete the directory public_html/classes/becker/xtras/
to
conserve disk space.
Now add the class files from your program. Suppose your program consists
of two classes, Main
and CollectorBot
, and that
the program is in a package named examples
. The directory
structure should then be
public_html/ myRobotApplet.html classes/ becker/ io/ robots/ util/ examples/ Main.class CollectorBot.class
The class files in becker/io/
,
becker/robots/
, and
becker/util/
are not shown.
Make sure all the files have read permissions set. The world should now be able to view your work!
Your applet will start and run faster if you put all the files into a
jar file. You can use the jar
tool to do this. A sample
command line for the above example is
jar cf collectorBot.jar becker/*/*.class examples/*.class
You can then replace everything in the classes
file with
this one file. You also need to add the line
archive="collectorBot.jar"
to the applet tags inserted in
step 1. Again, make sure the file has read permissions.
Note: It seems that it should be possible for the web browser to search both the
becker.jar
archive and classes in thecodebase
directory. This seems unreliable, at best, with some browsers supporting it and some not. Furthermore, theAPPLET
tag has been deprecated in version 4.0 of the HTML standard, but again, browser support for the alternativeOBJECT
tag seems very spotty.
The second solution for displaying a robot program via the web is to
embed the applet in the web page, as is traditionally done. This requires
extending JApplet
, putting part of the robot application's
main
method in the applet's init
method and part
in the start
method. It also involves using a
Thread
. Fortunately, the transformation is quite
straight-forward.
The following example shows a traditional robot application and then the equivalent applet.
import becker.robots.*; public class Example extends Object { public static void main(String[] args) { City c = new City(); Robot karel = new Robot(c, 1, 2, Direction.EAST); CityFrame f = new CityFrame(c); karel.move(); karel.turnLeft(); karel.move(); karel.turnLeft(); } }
import becker.robots.*; import javax.swing.JApplet; import javax.swing.*; public class Example extends JApplet implements Runnable { private Robot karel; // Create the initial situation public void init() { City c = new City(); this.karel = new Robot(c, 1, 2, Direction.EAST); RobotUIComponents ui = new RobotUIComponents(c); this.setContentPane(ui.getUI()); Thread t = new Thread(this); t.start(); } // Tell the robot what to do. public void run() { this.karel.move(); this.karel.turnLeft(); this.karel.move(); this.karel.turnLeft(); } }
After writing the applet, the class files need to be placed on the web
server, as discussed in Option 1, steps 2 and 3. The APPLET
tag to put in the HTML file would be something like
<applet codebase = "classes/" code = "Example.class" width = "600" height = "500"> </applet>
Nested Class Summary |
---|
Nested classes/interfaces inherited from class javax.swing.JApplet |
---|
JApplet.AccessibleJApplet |
Nested classes/interfaces inherited from class java.applet.Applet |
---|
Applet.AccessibleApplet |
Nested classes/interfaces inherited from class java.awt.Panel |
---|
Panel.AccessibleAWTPanel |
Nested classes/interfaces inherited from class java.awt.Container |
---|
Container.AccessibleAWTContainer |
Nested classes/interfaces inherited from class java.awt.Component |
---|
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy |
Field Summary |
---|
Fields inherited from class javax.swing.JApplet |
---|
accessibleContext, rootPane, rootPaneCheckingEnabled |
Fields inherited from class java.awt.Component |
---|
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
Fields inherited from interface java.awt.image.ImageObserver |
---|
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
Constructor Summary | |
---|---|
AppletRunner()
|
Method Summary | |
---|---|
void |
actionPerformed(ActionEvent evt)
Public because it must be; not meant to be called. |
void |
init()
Public because it must be; not meant to be called. |
Methods inherited from class javax.swing.JApplet |
---|
addImpl, createRootPane, getAccessibleContext, getContentPane, getGlassPane, getJMenuBar, getLayeredPane, getRootPane, isRootPaneCheckingEnabled, paramString, remove, setContentPane, setGlassPane, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, update |
Methods inherited from class java.applet.Applet |
---|
destroy, getAppletContext, getAppletInfo, getAudioClip, getAudioClip, getCodeBase, getDocumentBase, getImage, getImage, getLocale, getParameter, getParameterInfo, isActive, newAudioClip, play, play, resize, resize, setStub, showStatus, start, stop |
Methods inherited from class java.awt.Panel |
---|
addNotify |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public AppletRunner()
Method Detail |
---|
public void init()
init
in class Applet
public void actionPerformed(ActionEvent evt)
actionPerformed
in interface ActionListener
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |