1 of 8

Applets

Douglas Blank, Bryn Mawr College

2 of 8

Review of Assignment #7

  • Introduction to Unix commands
    • directories, and files
    • Editing files: nano, emacs, vi
    • Java: javac, java, appletviewer
  • Introduction to the Web
    • internet vs. www
    • Client/Server (clients sometimes "cache" pages)
    • Server gets the data; Client renders it
    • HTML, tags, attributes, embedded applet
  • Introduction to Applets
    • Applet class, run by the browser
    • Moved from loop (Java in control) to Web in control
    • Security issues

3 of 8

Working with Java via command-line

  1. Login, and cd to the correct folder
  2. Edit Java class definitions
    1. emacs
    2. vi
    3. nano
    4. gedit
  3. Compile each program into a .class file
    • javac Person.java, creates Person.class
  4. Run it
    • if it is a console with main run with "java Person"
    • if it is an applet:
      1. create a File.html, and embed class in it
      2. run in browser, or run via appletviewer

4 of 8

Java in Control

public void play() {� state = states.find(startState);� while (true) {� if (state != null) {� System.out.println(state.description);� System.out.print("Possible paths to the "); � state.connections.print();� } else {� System.out.println("State not found: " + state);� }� String command = readLine();� if (command == null) {� break;� } else {� String next_name = state.connections.find(command);� if (next_name != null)� state = states.find(next_name);� }� }� }

5 of 8

Java in Control

public void play() {� state = states.find(startState);� while (true) {� if (state != null) {� System.out.println(state.description);� System.out.print("Possible paths to the "); � state.connections.print();� } else {� System.out.println("State not found: " + state);� }� String command = readLine();� if (command == null) {� break;� } else {� String next_name = state.connections.find(command);� if (next_name != null)� state = states.find(next_name);� }� }� }

6 of 8

Web in Control

  1. Create a Game instance
  2. Load in all of the states
  3. Set the current state to be the start state
  4. Wait for Client (web browser) to tell it what to do

  1. Client will click button
  2. Causes Game instance to change state

7 of 8

Web in Control: Event-driven Algorithm

public class TextAdventure extends Applet implements � ActionListener {�

public void init() { ...

button.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {� String text = entry.getText();� game.makeMovement(text);� description.setText("You moved '" + entry.getText() � + "'. " + game.getDescription()� + " What would you like to do next?");� entry.setText(game.getConnections());� }�}�

actionPerformed is called each time you click on button.

8 of 8

Handin for Assignment #7

  • Just a URL
  • URL will have
    • running applet
    • links to all source code (.java files)
    • a paragraph or so about:
      • how the applet works
      • what you found interesting/challenging about this assignment