becker.robots
Class AppletRunner

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Panel
              extended by java.applet.Applet
                  extended by javax.swing.JApplet
                      extended by becker.robots.AppletRunner
All Implemented Interfaces:
ActionListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible, RootPaneContainer

public class AppletRunner
extends JApplet
implements ActionListener

There are two ways to display a Robot program on a web page, both are documented here.

Option 1: Run a Robot Application as an Applet

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.

Step 1: The HTML

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 include MyClass and myPackage.MyClass.
LABEL_TEXT
The text to display on the button that starts the program running. For example, "Run My Applet".

Step 2: Install Files on a Web Server

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!

Step 3: Jarring the Class Files (Optional)

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 the codebase directory. This seems unreliable, at best, with some browsers supporting it and some not. Furthermore, the APPLET tag has been deprecated in version 4.0 of the HTML standard, but again, browser support for the alternative OBJECT tag seems very spotty.

Option 2: Run a Robot Applet Inside a Web Page

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.

Traditional Robot Application

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

Equivalent Robot Applet

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>
 

Author:
Byron Weber Becker
See Also:
Serialized Form

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.awt.Container
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AppletRunner

public AppletRunner()
Method Detail

init

public void init()
Public because it must be; not meant to be called.

Overrides:
init in class Applet

actionPerformed

public void actionPerformed(ActionEvent evt)
Public because it must be; not meant to be called.

Specified by:
actionPerformed in interface ActionListener