Week 2: ZombieDetector: Difference between revisions

From Mobile Application Design
Jump to navigationJump to search
m Protected "Week 2: ZombieDetector" [edit=autoconfirmed:move=autoconfirmed]
No edit summary
 
Line 17: Line 17:
# Run your MIDlet
# Run your MIDlet


<pre>package src;
<pre>/**
* Simple Zombie Detector by Michael Sharon
*
* This program will allow a user to type in a sentence and detect if they are a zombie or not.
*/


//import relevant libraries
import javax.microedition.midlet.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.*;


/**
public class ZombieDetector extends MIDlet implements CommandListener {
* Zombie Detector
 
* @author Michael Sharon
  //contains a reference to the display
*
  private Display mDisplay;
* This program will allow a user to type in a sentence and detect if they are a zombie or not.
*
* Notice that we're implementing the CommandListener and Runnable. Read up on those two classes in the API docs.
* Runnable - http://uberthings.com/mobile/docs/midp1/java/lang/Runnable.html
* CommandListenter - http://uberthings.com/mobile/docs/midp1/javax/microedition/lcdui/CommandListener.html
*/
public class ZombieDetector extends MIDlet implements CommandListener, Runnable {


//Create our variables
  //these are the commands that we will use
  private Command mExitCommand, mDetectCommand, mCancelCommand;


//Every MIDlet will always contain exactly one Display object
  //high level user interface classes
//Display represents the device's display manager and input devices
  private TextBox mSubmitBox;
private Display mDisplay;


private Command mExitCommand, mDetectCommand, mCancelCommand;
  //constructor
  public ZombieDetector() {
 
//create the commands that we'll be using
    mExitCommand = new Command("Exit", Command.EXIT, 0);
    mDetectCommand = new Command("Detect", Command.SCREEN, 0);
    mCancelCommand = new Command("Cancel", Command.CANCEL, 0);
   
    //create the textbox that we'll be using
    mSubmitBox = new TextBox("Zombie Detector", "Am I a Zombie?", 64, 0);
    mSubmitBox.addCommand(mExitCommand);
    mSubmitBox.addCommand(mDetectCommand);
    mSubmitBox.setCommandListener(this);      
  }


//Setting up the Textbox and Progress Forms (like the example in class)
  //startApp is the very first thing run after the constructure
private TextBox mSubmitBox;
  public void startApp() {
  //get a reference to the current display - usually the first thing you do
  mDisplay = Display.getDisplay(this);
   
  //display the submit screen
  mDisplay.setCurrent(mSubmitBox);
  }


private Form mProgressForm;
  //handles the MIDlet's paused state
  public void pauseApp() {}


private StringItem mProgressString;
  //cleans up when the MIDlet is destroyed
  public void destroyApp(boolean unconditional) {}


//Field / Form style
  //This gets called whenever we select a command
private TextField mSubmitField;
  public void commandAction(Command c, Displayable s) {
    if (c == mExitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
    else if (c == mDetectCommand) {
      //start searching for zombies
      zombieSearch();    
    }
  }


private Form mDetectionForm;
  //The function which searches for zombies at the phone
 
  public void zombieSearch() {
public ZombieDetector() {
    String word = mSubmitBox.getString();
mExitCommand = new Command("Exit", Command.EXIT, 0);
    String isZombie = null;
mDetectCommand = new Command("Detect", Command.SCREEN, 0);
int zombieCheck = 0;  
 
mSubmitBox = new TextBox("ZombieDetector", "Am I a Zombie?", 32, 0);
//Check if we're dealing with a zombie
mSubmitBox.addCommand(mExitCommand);
zombieCheck = word.indexOf("brains");  
mSubmitBox.addCommand(mDetectCommand);
mSubmitBox.setCommandListener(this);
//See API docs for String.indexOf
 
if (zombieCheck == -1)
mSubmitField = new TextField(
{
"Type a few words below to check if you are a zombie:",
isZombie = "This is a live one. No zombies here.";  
"I am not a zombie.. honest..", 32, 0);
mDetectionForm = new Form("Zombie Detector");
mDetectionForm.addCommand(mExitCommand);
mDetectionForm.addCommand(mDetectCommand);
mDetectionForm.append(mSubmitField);
mDetectionForm.setCommandListener(this);
 
mProgressForm = new Form("Lookup progress");
mProgressString = new StringItem(null, null);
mProgressForm.append(mProgressString);
}
 
public void startApp() {
mDisplay = Display.getDisplay(this);
 
//uncomment the mSubmitbox line below to run it like the example in class
//mDisplay.setCurrent(mSubmitBox);
mDisplay.setCurrent(mDetectionForm);
 
}
 
public void pauseApp() {
}
 
public void destroyApp(boolean unconditional) {
}
 
//This is called whenever we select a command
public void commandAction(Command c, Displayable s) {
if (c == mExitCommand) {
destroyApp(false);
notifyDestroyed();
} else if (c == mDetectCommand) {
// Show the progress form.
mDisplay.setCurrent(mProgressForm);
// Kick off the thread to do the query.
Thread t = new Thread(this);
t.start();
}
}
}
else
 
{
/*
isZombie = "Eeek! Run for your lives!";  
* The run() method is automatically called when a new Thread is started.
*
*/
public void run() {
 
//uncomment the line below if you want use the TextBox
//String word = mSubmitBox.getString();
String word = mSubmitField.getString();
//System.out.println is useful for debugging and sending things to the console.
System.out.println("Got the String - " + word);
String result;
 
result = checkForZombie(word);
 
//Place the result into an Alert
Alert results = new Alert("Result", result, null, null);
results.setTimeout(Alert.FOREVER);
 
//Display the Alert, followed by the next Displayable - in this case our Form or Textbox
//Uncomment the line below to use the Textbox
//mDisplay.setCurrent(results, mSubmitBox);
mDisplay.setCurrent(results, mDetectionForm);
}
 
private String checkForZombie(String word) {
String isZombie = null;
int zombieCheck = 0;
 
//Check if we're dealing with a zombie
zombieCheck = word.indexOf("brain");
 
if (zombieCheck == -1) {
isZombie = "This is a live one. No zombies here.";
} else {
isZombie = "Eeek! Run for your lives!";
}
}
//Create a new alert
    Alert results = new Alert("Definition", isZombie,null, null);
    results.setTimeout(Alert.FOREVER);
   
    //Display the alert, then the submit field again
    mDisplay.setCurrent(results, mSubmitBox);
  }


return isZombie;
}
}
}

Latest revision as of 23:42, 10 September 2007

Zombie Detector

Setup

  1. Create a new J2ME Midlet Suite in Eclipse by right-clicking in the Package Manager, selecting New -> Project -> J2ME Midlet Suite
  2. Call this ZombieDetector. Leave everything else as is.
  3. Right-click on your ZombieDetector project and select New -> Class
  4. Use src as your package and ZombieDetector as the class name. Click Finish.
  5. Open your new ZombieDetector class
  6. Cut and paste the code from below into your file. Save.
  7. Open up the ZombieDetector.jad file
  8. Click on the Midlets tab. Click Add.
  9. Write in ZombieDetector as the MIDlet name.
  10. Click in the button that appears in class. You may have to start typing Zombie in. It should find your class automatically. If nothing happens, try closing all the files down and opening them again.
  11. Right-click your project again, select Run As, then Run...
  12. Setup so that it looks something like this - run_dialog.png
  13. Run your MIDlet
/**
* Simple Zombie Detector by Michael Sharon
*
* This program will allow a user to type in a sentence and detect if they are a zombie or not. 
*/

//import relevant libraries
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ZombieDetector extends MIDlet implements CommandListener {
  
  //contains a reference to the display
  private Display mDisplay;

  //these are the commands that we will use
  private Command mExitCommand, mDetectCommand, mCancelCommand;

  //high level user interface classes
  private TextBox mSubmitBox;

  //constructor
  public ZombieDetector() {
	  
	//create the commands that we'll be using
    mExitCommand = new Command("Exit", Command.EXIT, 0);
    mDetectCommand = new Command("Detect", Command.SCREEN, 0);
    mCancelCommand = new Command("Cancel", Command.CANCEL, 0);
    
    //create the textbox that we'll be using
    mSubmitBox = new TextBox("Zombie Detector", "Am I a Zombie?", 64, 0);
    mSubmitBox.addCommand(mExitCommand);
    mSubmitBox.addCommand(mDetectCommand);
    mSubmitBox.setCommandListener(this);        
  }

  //startApp is the very first thing run after the constructure
  public void startApp() {
	  //get a reference to the current display - usually the first thing you do
	  mDisplay = Display.getDisplay(this);
    
	  //display the submit screen
	  mDisplay.setCurrent(mSubmitBox);
  }

  //handles the MIDlet's paused state 
  public void pauseApp() {}

  //cleans up when the MIDlet is destroyed
  public void destroyApp(boolean unconditional) {}

  //This gets called whenever we select a command
  public void commandAction(Command c, Displayable s) {
    if (c == mExitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
    else if (c == mDetectCommand) {
      //start searching for zombies
      zombieSearch();      
    }
  }

  //The function which searches for zombies at the phone
  public void zombieSearch() {
	    String word = mSubmitBox.getString();
	    String isZombie = null;
		int zombieCheck = 0; 
		
		//Check if we're dealing with a zombie
		zombieCheck = word.indexOf("brains"); 
		
		//See API docs for String.indexOf
		if (zombieCheck == -1)
		{
			isZombie = "This is a live one. No zombies here."; 
		}
		else
		{
			isZombie = "Eeek! Run for your lives!"; 
		}
		
		//Create a new alert
	    Alert results = new Alert("Definition", isZombie,null, null);
	    results.setTimeout(Alert.FOREVER);
	    
	    //Display the alert, then the submit field again
	    mDisplay.setCurrent(results, mSubmitBox);
	  }

}