SlideShare una empresa de Scribd logo
1 de 17
Lab 4 (Lab 2 on Java ME)
Java ME GUI Programming

    Pervasive Computing Lab
    Prepared by: Tuan Nguyen
MIDP User interface
 High level

                         Screen


      Alert       Form            List         TextBox


 Low level



               Canvas               Graphics
Form




Picture from: http://www.javaworld.com/javaworld/jw-05-2005/images/jw-0516-midp1.jpg
Let’s Example
 Mobile Restaurant App
     Allow a user to order a
      meal remotely using a
      mobile phone.




       This is a list (List class)
List – a class of Screen
String[] mainCourseOffers={"Steak","Lamb","Chicken"};
...

//create the list
lstMainCourse = new List("Select main course:",
                  Choice.MULTIPLE, mainCourseOffers);

//display the list on the screen
Display.getDisplay(this).setCurrent(lstMainCourse);
Add a Command button to the list
//Create a command
cmdNext = new Command("Next", Command.OK, 1);

//Add a command “Next” to the Main Course list
lstMainCourse.addCommand(cmdNext);

//let lstMainCourse listen to the user’s commands
lstMainCourse.setCommandListener(this);
Result
 The “Next” command does
  nothing now.
 Let’s add another list
  called “Dessert List”.
 When the user selects the
  Next command, the
  Dessert List will be
  displayed on the screen.
          The “Next” command button
Create the Dessert list
String[] dessertOffers = {"Ice Cream", "Chocolate Cake",
                           "Orange Juice"};
...
//create the Dessert list
lstDessert=new List("Select dessert:",Choice.MULTIPLE,
                    dessertOffers);

//but not display the dessert list until the user selects
  the “Next” command.
Implement CommandListener class
public class Restaurant extends MIDlet
                    implements CommandListener
...

//process commands
public void commandAction(Command command,
       Displayable displayable){

    String label = command.getLabel();
    if(label.equals("Next")){
      Display.getDisplay(this).setCurrent(lstDessert);
    }
}

...
Add an Order Summary screen
 Allow the user see what s/he’s
  ordering. How?
  1.   Create the empty Order
       Summary screen (e.g., a form).
  2.   Add a command “Proceed” to
       the Dessert list that navigates to
       the Order Summary screen.
  3.   Add code in the commandAction
       method to process the “Proceed”
       command.
Get the state of all elements of a list
//retrieve selected main courses
boolean[] selectedMainCourses = new boolean[lstMainCourse.size()];
lstMainCourse.getSelectedFlags(selectedMainCourses);
String strMainCourse = "";
for(int i=0; i<lstMainCourse.size(); i++){
   if(selectedMainCourses[i]) strMainCourse += mainCourseOffers[i] + ", ";
}

//retrieve selected dessert
boolean[] selectedDessert = new boolean[lstDessert.size()];
lstDessert.getSelectedFlags(selectedDessert);
String strDessert = "";
for(int i=0; i<lstDessert.size(); i++){
   if(selectedDessert[i]) strDessert += dessertOffers[i] + ", ";
}
//update the Order Summary form according to the selected main courses...
sItemMainCourse.setText(strMainCourse);
sItemDessert.setText(strDessert);
Display.getDisplay(this).setCurrent(frmOrderSummary);
Exercise:
Improve Mobile Restaurant App




Find the completed code for this app on the subject web site.
HotelBooking Application
  Purpose: a customer wants to book a room at
   Saville Hotel
  The UI can be like this




Items are
Form, Gauge, Spacer, Ima
geItem, TextField
, DateField, StringItem, Ch
oiceGroup
Graphics
javax.microedition.lcdui.Canvas
class MyCanvas extends Canvas {

    public void paint(Graphics g) {
     // create a 20x20 black square in the centre

     g.setColor(0xFF0000); // make sure it is red

     g.fillRect(getWidth()/2–10, getHeight()/2–10, 20, 20);

     g.setColor(0x0000FF); // make sure it is blue

     g.drawString("Hello World", getWidth()/2, getHeight()/2
         - 10, Graphics.HCENTER | Graphics.BASELINE);

     }

}
References
 Vikram Goyal , J2ME Tutorial,
    http://today.java.net/pub/a/today/2005/02/09/j2me1.html, 02/09/2005
   Michael Juntao Yuan & Kevin Sharp, JavaWorld.com,
    www.javaworld.com/javaworld/jw-05-2005/jw-0516-midp.html,
    05/16/2005
   http://developer.symbian.com/main/oslibrary/java_papers/midp.jsp
   http://developers.sun.com/mobility/apis/articles/wsa/
   Eric Giguere, Databases and MIDP, Part 1: Understanding the Record
    Management System,
    http://developers.sun.com/mobility/midp/articles/databaserms, 2004
   http://www.java2s.com/Code/Java/J2ME/Persistencestoringandshowin
    ggamescores.htm
   http://today.java.net/pub/a/today/2005/05/03/midletUI.html?page=4
Time for practice

Más contenido relacionado

La actualidad más candente

Managing users & tables using Oracle Enterprise Manage
Managing users & tables using Oracle Enterprise ManageManaging users & tables using Oracle Enterprise Manage
Managing users & tables using Oracle Enterprise ManageNR Computer Learning Center
 
Eureka - elegant iOs form builder in swift (johnp - mingle)
Eureka - elegant iOs form builder in swift (johnp - mingle)Eureka - elegant iOs form builder in swift (johnp - mingle)
Eureka - elegant iOs form builder in swift (johnp - mingle)John Pham
 
Login and Registration form using oop in php
Login and Registration form using oop in phpLogin and Registration form using oop in php
Login and Registration form using oop in phpherat university
 
Php tour 2018 un autre regard sur la validation (1)
Php tour 2018   un autre regard sur la validation (1)Php tour 2018   un autre regard sur la validation (1)
Php tour 2018 un autre regard sur la validation (1)Quentin Pautrat
 
Web2py tutorial to create db driven application.
Web2py tutorial to create db driven application.Web2py tutorial to create db driven application.
Web2py tutorial to create db driven application.fRui Apps
 
Php login system with admin features evolt
Php login system with admin features   evoltPhp login system with admin features   evolt
Php login system with admin features evoltGIMT
 
Xml operations in odoo
Xml operations in odooXml operations in odoo
Xml operations in odooCeline George
 

La actualidad más candente (10)

Managing users & tables using Oracle Enterprise Manage
Managing users & tables using Oracle Enterprise ManageManaging users & tables using Oracle Enterprise Manage
Managing users & tables using Oracle Enterprise Manage
 
Eureka - elegant iOs form builder in swift (johnp - mingle)
Eureka - elegant iOs form builder in swift (johnp - mingle)Eureka - elegant iOs form builder in swift (johnp - mingle)
Eureka - elegant iOs form builder in swift (johnp - mingle)
 
Login and Registration form using oop in php
Login and Registration form using oop in phpLogin and Registration form using oop in php
Login and Registration form using oop in php
 
Php tour 2018 un autre regard sur la validation (1)
Php tour 2018   un autre regard sur la validation (1)Php tour 2018   un autre regard sur la validation (1)
Php tour 2018 un autre regard sur la validation (1)
 
Web2py tutorial to create db driven application.
Web2py tutorial to create db driven application.Web2py tutorial to create db driven application.
Web2py tutorial to create db driven application.
 
Javascript
JavascriptJavascript
Javascript
 
Php login system with admin features evolt
Php login system with admin features   evoltPhp login system with admin features   evolt
Php login system with admin features evolt
 
WordCamp London 2013
WordCamp London 2013WordCamp London 2013
WordCamp London 2013
 
lab56_db
lab56_dblab56_db
lab56_db
 
Xml operations in odoo
Xml operations in odooXml operations in odoo
Xml operations in odoo
 

Destacado

2013 GMC Yukon Denali Philadelphia
2013 GMC Yukon Denali Philadelphia2013 GMC Yukon Denali Philadelphia
2013 GMC Yukon Denali PhiladelphiaFred Beans GMC
 
The Autobahn Has No Speed Limit - Your XPages Shouldn't Either!
The Autobahn Has No Speed Limit - Your XPages Shouldn't Either!The Autobahn Has No Speed Limit - Your XPages Shouldn't Either!
The Autobahn Has No Speed Limit - Your XPages Shouldn't Either!Teamstudio
 
Bí quyết dưỡng da ngày đông về
Bí quyết dưỡng da ngày đông vềBí quyết dưỡng da ngày đông về
Bí quyết dưỡng da ngày đông vềlandatrangmin
 
Aect480 lecture 7
Aect480 lecture 7Aect480 lecture 7
Aect480 lecture 7cloudc123
 
Immigration web quest
Immigration web questImmigration web quest
Immigration web questskippers7
 
New background processes in 11g r2
New background processes in 11g r2New background processes in 11g r2
New background processes in 11g r2Kiran Gali
 
Trabajo u.s.a helena
Trabajo u.s.a helenaTrabajo u.s.a helena
Trabajo u.s.a helenaTeacherJuan
 
Exposició social
Exposició socialExposició social
Exposició socialmonet69
 
Wireless Wednesdays: Beyond the Basics - Enhance your Enterprise Mobile Appli...
Wireless Wednesdays: Beyond the Basics - Enhance your Enterprise Mobile Appli...Wireless Wednesdays: Beyond the Basics - Enhance your Enterprise Mobile Appli...
Wireless Wednesdays: Beyond the Basics - Enhance your Enterprise Mobile Appli...Teamstudio
 
SEO Keyword Research - Market Analysis and Forecasting
SEO Keyword Research - Market Analysis and ForecastingSEO Keyword Research - Market Analysis and Forecasting
SEO Keyword Research - Market Analysis and ForecastingAlejandro Carvajal
 
Paragould rotary club assembly
Paragould rotary club assemblyParagould rotary club assembly
Paragould rotary club assemblyDustin Rumsey
 

Destacado (20)

2013 GMC Yukon Denali Philadelphia
2013 GMC Yukon Denali Philadelphia2013 GMC Yukon Denali Philadelphia
2013 GMC Yukon Denali Philadelphia
 
The Autobahn Has No Speed Limit - Your XPages Shouldn't Either!
The Autobahn Has No Speed Limit - Your XPages Shouldn't Either!The Autobahn Has No Speed Limit - Your XPages Shouldn't Either!
The Autobahn Has No Speed Limit - Your XPages Shouldn't Either!
 
Arti c1
Arti c1Arti c1
Arti c1
 
Teiul
TeiulTeiul
Teiul
 
Bí quyết dưỡng da ngày đông về
Bí quyết dưỡng da ngày đông vềBí quyết dưỡng da ngày đông về
Bí quyết dưỡng da ngày đông về
 
к и м 6
к и м 6к и м 6
к и м 6
 
Aect480 lecture 7
Aect480 lecture 7Aect480 lecture 7
Aect480 lecture 7
 
Immigration web quest
Immigration web questImmigration web quest
Immigration web quest
 
New background processes in 11g r2
New background processes in 11g r2New background processes in 11g r2
New background processes in 11g r2
 
Trabajo u.s.a helena
Trabajo u.s.a helenaTrabajo u.s.a helena
Trabajo u.s.a helena
 
Joann fabrics coupons1
Joann fabrics coupons1Joann fabrics coupons1
Joann fabrics coupons1
 
к и м 6
к и м 6к и м 6
к и м 6
 
Vacations to Disney world
Vacations to Disney worldVacations to Disney world
Vacations to Disney world
 
Exposició social
Exposició socialExposició social
Exposició social
 
Wireless Wednesdays: Beyond the Basics - Enhance your Enterprise Mobile Appli...
Wireless Wednesdays: Beyond the Basics - Enhance your Enterprise Mobile Appli...Wireless Wednesdays: Beyond the Basics - Enhance your Enterprise Mobile Appli...
Wireless Wednesdays: Beyond the Basics - Enhance your Enterprise Mobile Appli...
 
SEO Keyword Research - Market Analysis and Forecasting
SEO Keyword Research - Market Analysis and ForecastingSEO Keyword Research - Market Analysis and Forecasting
SEO Keyword Research - Market Analysis and Forecasting
 
Adidas 120207043329-phpapp01
Adidas 120207043329-phpapp01Adidas 120207043329-phpapp01
Adidas 120207043329-phpapp01
 
Internet
InternetInternet
Internet
 
Tempo June-2016
Tempo June-2016Tempo June-2016
Tempo June-2016
 
Paragould rotary club assembly
Paragould rotary club assemblyParagould rotary club assembly
Paragould rotary club assembly
 

Similar a Java me lab2-slides (gui programming)

Menu bars and menus
Menu bars and menusMenu bars and menus
Menu bars and menusmyrajendra
 
School System C# Code
School System C# CodeSchool System C# Code
School System C# CodePaul Smyth
 
How to Start Test-Driven Development in Legacy Code
How to Start Test-Driven Development in Legacy CodeHow to Start Test-Driven Development in Legacy Code
How to Start Test-Driven Development in Legacy CodeDaniel Wellman
 
import java.util.ArrayList; import java.util.List; import java.u.pdf
import java.util.ArrayList; import java.util.List; import java.u.pdfimport java.util.ArrayList; import java.util.List; import java.u.pdf
import java.util.ArrayList; import java.util.List; import java.u.pdfanupamagarud8
 
Design pattern - Iterator, Mediator and Memento
Design pattern - Iterator, Mediator and MementoDesign pattern - Iterator, Mediator and Memento
Design pattern - Iterator, Mediator and MementoSean Tsai
 
Design patterns in the 21st Century
Design patterns in the 21st CenturyDesign patterns in the 21st Century
Design patterns in the 21st CenturySamir Talwar
 
Hello everyone,Im actually working on a fast food order program..pdf
Hello everyone,Im actually working on a fast food order program..pdfHello everyone,Im actually working on a fast food order program..pdf
Hello everyone,Im actually working on a fast food order program..pdffedosys
 
How to Bring Common UI Patterns to ADF
How to Bring Common UI Patterns to ADF How to Bring Common UI Patterns to ADF
How to Bring Common UI Patterns to ADF Luc Bors
 
Quick Intro to Android Development
Quick Intro to Android DevelopmentQuick Intro to Android Development
Quick Intro to Android DevelopmentJussi Pohjolainen
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeletonIram Ramrajkar
 
The secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you aboutThe secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you aboutDror Helper
 
Session 8 connect your universal application with database .. builders & deve...
Session 8 connect your universal application with database .. builders & deve...Session 8 connect your universal application with database .. builders & deve...
Session 8 connect your universal application with database .. builders & deve...Moatasim Magdy
 
QA your code: The new Unity Test Framework – Unite Copenhagen 2019
QA your code: The new Unity Test Framework – Unite Copenhagen 2019QA your code: The new Unity Test Framework – Unite Copenhagen 2019
QA your code: The new Unity Test Framework – Unite Copenhagen 2019Unity Technologies
 

Similar a Java me lab2-slides (gui programming) (20)

Menu bars and menus
Menu bars and menusMenu bars and menus
Menu bars and menus
 
School System C# Code
School System C# CodeSchool System C# Code
School System C# Code
 
Using database in android
Using database in androidUsing database in android
Using database in android
 
How to Start Test-Driven Development in Legacy Code
How to Start Test-Driven Development in Legacy CodeHow to Start Test-Driven Development in Legacy Code
How to Start Test-Driven Development in Legacy Code
 
import java.util.ArrayList; import java.util.List; import java.u.pdf
import java.util.ArrayList; import java.util.List; import java.u.pdfimport java.util.ArrayList; import java.util.List; import java.u.pdf
import java.util.ArrayList; import java.util.List; import java.u.pdf
 
Eclipse Tricks
Eclipse TricksEclipse Tricks
Eclipse Tricks
 
Design pattern - Iterator, Mediator and Memento
Design pattern - Iterator, Mediator and MementoDesign pattern - Iterator, Mediator and Memento
Design pattern - Iterator, Mediator and Memento
 
Design patterns in the 21st Century
Design patterns in the 21st CenturyDesign patterns in the 21st Century
Design patterns in the 21st Century
 
Hello everyone,Im actually working on a fast food order program..pdf
Hello everyone,Im actually working on a fast food order program..pdfHello everyone,Im actually working on a fast food order program..pdf
Hello everyone,Im actually working on a fast food order program..pdf
 
MySQL Presentation
MySQL PresentationMySQL Presentation
MySQL Presentation
 
How to Bring Common UI Patterns to ADF
How to Bring Common UI Patterns to ADF How to Bring Common UI Patterns to ADF
How to Bring Common UI Patterns to ADF
 
How te bring common UI patterns to ADF
How te bring common UI patterns to ADFHow te bring common UI patterns to ADF
How te bring common UI patterns to ADF
 
Backendless apps
Backendless appsBackendless apps
Backendless apps
 
Quick Intro to Android Development
Quick Intro to Android DevelopmentQuick Intro to Android Development
Quick Intro to Android Development
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeleton
 
Django quickstart
Django quickstartDjango quickstart
Django quickstart
 
The secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you aboutThe secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you about
 
Session 8 connect your universal application with database .. builders & deve...
Session 8 connect your universal application with database .. builders & deve...Session 8 connect your universal application with database .. builders & deve...
Session 8 connect your universal application with database .. builders & deve...
 
QA your code: The new Unity Test Framework – Unite Copenhagen 2019
QA your code: The new Unity Test Framework – Unite Copenhagen 2019QA your code: The new Unity Test Framework – Unite Copenhagen 2019
QA your code: The new Unity Test Framework – Unite Copenhagen 2019
 
Begin with Python
Begin with PythonBegin with Python
Begin with Python
 

Java me lab2-slides (gui programming)

  • 1. Lab 4 (Lab 2 on Java ME) Java ME GUI Programming Pervasive Computing Lab Prepared by: Tuan Nguyen
  • 2. MIDP User interface  High level Screen Alert Form List TextBox  Low level Canvas Graphics
  • 4. Let’s Example  Mobile Restaurant App  Allow a user to order a meal remotely using a mobile phone. This is a list (List class)
  • 5. List – a class of Screen String[] mainCourseOffers={"Steak","Lamb","Chicken"}; ... //create the list lstMainCourse = new List("Select main course:", Choice.MULTIPLE, mainCourseOffers); //display the list on the screen Display.getDisplay(this).setCurrent(lstMainCourse);
  • 6. Add a Command button to the list //Create a command cmdNext = new Command("Next", Command.OK, 1); //Add a command “Next” to the Main Course list lstMainCourse.addCommand(cmdNext); //let lstMainCourse listen to the user’s commands lstMainCourse.setCommandListener(this);
  • 7. Result  The “Next” command does nothing now.  Let’s add another list called “Dessert List”.  When the user selects the Next command, the Dessert List will be displayed on the screen. The “Next” command button
  • 8. Create the Dessert list String[] dessertOffers = {"Ice Cream", "Chocolate Cake", "Orange Juice"}; ... //create the Dessert list lstDessert=new List("Select dessert:",Choice.MULTIPLE, dessertOffers); //but not display the dessert list until the user selects the “Next” command.
  • 9. Implement CommandListener class public class Restaurant extends MIDlet implements CommandListener ... //process commands public void commandAction(Command command, Displayable displayable){ String label = command.getLabel(); if(label.equals("Next")){ Display.getDisplay(this).setCurrent(lstDessert); } } ...
  • 10. Add an Order Summary screen  Allow the user see what s/he’s ordering. How? 1. Create the empty Order Summary screen (e.g., a form). 2. Add a command “Proceed” to the Dessert list that navigates to the Order Summary screen. 3. Add code in the commandAction method to process the “Proceed” command.
  • 11. Get the state of all elements of a list //retrieve selected main courses boolean[] selectedMainCourses = new boolean[lstMainCourse.size()]; lstMainCourse.getSelectedFlags(selectedMainCourses); String strMainCourse = ""; for(int i=0; i<lstMainCourse.size(); i++){ if(selectedMainCourses[i]) strMainCourse += mainCourseOffers[i] + ", "; } //retrieve selected dessert boolean[] selectedDessert = new boolean[lstDessert.size()]; lstDessert.getSelectedFlags(selectedDessert); String strDessert = ""; for(int i=0; i<lstDessert.size(); i++){ if(selectedDessert[i]) strDessert += dessertOffers[i] + ", "; } //update the Order Summary form according to the selected main courses... sItemMainCourse.setText(strMainCourse); sItemDessert.setText(strDessert); Display.getDisplay(this).setCurrent(frmOrderSummary);
  • 12. Exercise: Improve Mobile Restaurant App Find the completed code for this app on the subject web site.
  • 13. HotelBooking Application  Purpose: a customer wants to book a room at Saville Hotel  The UI can be like this Items are Form, Gauge, Spacer, Ima geItem, TextField , DateField, StringItem, Ch oiceGroup
  • 15. javax.microedition.lcdui.Canvas class MyCanvas extends Canvas { public void paint(Graphics g) { // create a 20x20 black square in the centre g.setColor(0xFF0000); // make sure it is red g.fillRect(getWidth()/2–10, getHeight()/2–10, 20, 20); g.setColor(0x0000FF); // make sure it is blue g.drawString("Hello World", getWidth()/2, getHeight()/2 - 10, Graphics.HCENTER | Graphics.BASELINE); } }
  • 16. References  Vikram Goyal , J2ME Tutorial, http://today.java.net/pub/a/today/2005/02/09/j2me1.html, 02/09/2005  Michael Juntao Yuan & Kevin Sharp, JavaWorld.com, www.javaworld.com/javaworld/jw-05-2005/jw-0516-midp.html, 05/16/2005  http://developer.symbian.com/main/oslibrary/java_papers/midp.jsp  http://developers.sun.com/mobility/apis/articles/wsa/  Eric Giguere, Databases and MIDP, Part 1: Understanding the Record Management System, http://developers.sun.com/mobility/midp/articles/databaserms, 2004  http://www.java2s.com/Code/Java/J2ME/Persistencestoringandshowin ggamescores.htm  http://today.java.net/pub/a/today/2005/05/03/midletUI.html?page=4