Mobile Me(dia)

Shawn Van Every Shawn.Van.Every@nyu.edu
Spring 2008
H79.2690.1

Week 8 - Mobile Application Development with Mobile Processing Continued

Mobile Processing GUI Development

Mobile Processing makes development of JME apps easier in quite a few ways. One such way is in GUI (graphical user interface) development.

Below is an example of an application which uses PContainers, PButtons and PLabels to allow switching between screens in a Mobile Processing application.
/*
  This example shows how screens and buttons can be used with Mobile Processing

*/


import processing.core.*;		
		
public class MPGUIExample extends PMIDlet
{

  /* 
     First we declare some PContainers
     These hold different GUI elements and we can use them 
     to hold all of the GUI elements for any particular screen
     http://mobile.processing.org/reference/reference.php?name=PContainer
     (Ignore the scrollbar stuff in the docs, it causes problems)
  */  
  PContainer screen1;
  PContainer screen2;
  
  /*
     Declaring some buttons to use
     http://mobile.processing.org/reference/reference.php?name=PButton
  */
  PButton button1;
  PButton button2;
  
  /*
     Declaring some labels to use
     http://mobile.processing.org/reference/reference.php?name=PLabel     
  */
  PLabel label1;
  PLabel label2;
  
  PContainer currentContainer;
  
  void setup()
  {
    /*
      Instantiate the PContainers
      No arguments for the constructor
    */
    screen1 = new PContainer();
    screen2 = new PContainer();
    
    /*
      Instantiate the PButtons
      Pass in a String to display on the button
    */
    button1 = new PButton("Hi");
    button2 = new PButton("World");
    
    /*
      Set the bounds (the size and position for the buttons)
      x, y, width, height
    */
    button1.setBounds(10,10,100,20);
    
    // Allowing button2 to decide how big it should be
    // to fit it's text based upon the dimensions we supply
    button2.calculateBounds(0,0,width,height);
    
    // Using those dimensions and the screen dimensions to 
    // decide where to place it and how big to make it (centered on the screen)
    button2.setBounds(width/2-button2.width/2,height/2-button2.height/2,button2.width,button2.height);

    /*
      Add the buttons to the containers
    */
    screen1.add(button1);
    screen2.add(button2);

    
    /*
      Instantiate the PLabels
      Pass in a String to display on the label
    */
    label1 = new PLabel("Click the button");
    label2 = new PLabel("Go ahead");
    
    /*
      Set the size and location of the labels
    */
    label1.setBounds(5,5,width,10);
    label2.setBounds(5,5,width,50);
    
    /*
      Add the labels to the containers    
    */
    screen1.add(label1);
    screen2.add(label2);
    
    /*
      Tell the containers to start doing their thing
    */
    screen1.initialize();
    screen2.initialize();
    
    /* 
      Set current container equal to the container we want to display to start
    */
    currentContainer = screen1;
  }

  void draw()
  {
    background(200);
    /* 
      Draw the current container
      We are switching up what the current container points to (is equal to) to change what is displayed
    */
    currentContainer.draw();
  } 
  
  /*
    If a key is pressed, pass it along to the currentContainer
  */
  void keyPressed() 
  {
    currentContainer.keyPressed();
  }

  /*
    If a key is released, pass it along to the currentContainer
  */
  void keyReleased() 
  {
    currentContainer.keyReleased();
  }

  
  /*
    When a container registers a keyPress and it thinks a button has been pressed
    it passes it along to that button which generates a library event
  */
  void libraryEvent(Object library, int event, Object data)
  {
    // Check which button it is (which object)
    if (library == button1)
    {
      // Switch the screen..  
      currentContainer = screen2;  
    }
    else if (library == button2)
    {
      currentContainer = screen1;
    }
  }
}		
Working with other Mobile Processing GUI components is much the same.

Have a look at: PCheckBox, PImageLabel, PList and PRadioButton. I would stay away from PScrollBar for now (as it seems to have errors).