mobile phone programming - aalborg...

14
http://mobilephones.kom.aau.dk Mobile Phone Programming Mobile Phone Programming Free Study Activity Day 3 DAY 3 – J2ME http://mobilephones.kom.aau.dk Part 2 DAY 3 – J2ME J2ME in a nutshell J2ME in a nutshell

Upload: others

Post on 09-Feb-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mobile Phone Programming - Aalborg Universitetkom.aau.dk/project/mobilephone/teaching/freestudy... · Object-oriented programming fundamentals DAY 3 – J2ME public class Car { int

http://mobilephones.kom.aau.dk

Mobile Phone ProgrammingMobile Phone ProgrammingFree Study Activity

Day 3

DAY 3 – J2ME

http://mobilephones.kom.aau.dk

Part 2DAY 3 – J2ME

• J2ME in a nutshell

J2ME in a nutshell

Page 2: Mobile Phone Programming - Aalborg Universitetkom.aau.dk/project/mobilephone/teaching/freestudy... · Object-oriented programming fundamentals DAY 3 – J2ME public class Car { int

http://mobilephones.kom.aau.dk

Objects

• In real world: object is your car, your bicycle, your cat..

• Object has state and behavior– State: variables (car: color, model)– Behavior: methods (car: accelerating, braking)

• Object is created by using class

DAY 3 – J2ME

http://mobilephones.kom.aau.dk

Class• In real world: all cars have some state and

behavior in common• Class defines the variables and the

methods common to all objects of a certain kind

• Defining object– Dog Billy;

• Creating object– Billy = new Dog();

DAY 3 – J2ME

Page 3: Mobile Phone Programming - Aalborg Universitetkom.aau.dk/project/mobilephone/teaching/freestudy... · Object-oriented programming fundamentals DAY 3 – J2ME public class Car { int

http://mobilephones.kom.aau.dk

Object-oriented programming fundamentals

DAY 3 – J2ME

public class Car

{

int MaxSpeed;

public void speed_up()

{

#code for speeding up

}

}

Car Corvette = new Car();

Corvette. MaxSpeed =280;

Corvette. speed_up() ;

Car Ferrari = new Car();

Ferrari. MaxSpeed=320;

Ferrari.speed_up();

http://mobilephones.kom.aau.dk

“if” statementDAY 3 – J2ME

If-Then Statement:

if ( boolean Expression ) {

//code}

If-Then-Else Statement:

if ( boolean Expression ) {

//code}else if ( boolean Expression ) {

//code}else{

//code}

Page 4: Mobile Phone Programming - Aalborg Universitetkom.aau.dk/project/mobilephone/teaching/freestudy... · Object-oriented programming fundamentals DAY 3 – J2ME public class Car { int

http://mobilephones.kom.aau.dk

“for” statementDAY 3 – J2ME

for Statement:

for (i = 0; i < 10; i++){

//code}

“while” statementwhile Statement:

while (boolean expression){

//code[iteration;]

}

http://mobilephones.kom.aau.dk

“Switch” statementDAY 3 – J2ME

Switch Statement:

switch (variable) {

case value_1: //code (only one line);break;

case value_2: {

//code (more than one line); }

break;}

Page 5: Mobile Phone Programming - Aalborg Universitetkom.aau.dk/project/mobilephone/teaching/freestudy... · Object-oriented programming fundamentals DAY 3 – J2ME public class Car { int

http://mobilephones.kom.aau.dk

MIDlet GUI• Instead of dividing the display into multiple windows (like

MS Windows) MIDP GUI could be seen as a deck of screens - only one active at a time

• Display has a single, fixed-size window whose content are controlled by one application at any time

DAY 3 – J2ME

MyMIDlet

YourMIDlet

http://mobilephones.kom.aau.dk

GUI• High-level API

– Portability key issue– Easy-to-use– Little control over GUI’s look and feel

• Low-level API– Full control of graphics elements and low-level input

events– Four classes: Canvas, Graphics, Image, Font– Game API provides additional classes for low-level

drawing

DAY 3 – J2ME

Page 6: Mobile Phone Programming - Aalborg Universitetkom.aau.dk/project/mobilephone/teaching/freestudy... · Object-oriented programming fundamentals DAY 3 – J2ME public class Car { int

http://mobilephones.kom.aau.dk

Major Classes of LCDUIMIDP 1.0

DAY 3 – J2ME

Displayable

Screen

Alert

List

Form

TextBox

Item

ChoiceGroup

DateField

Gauge

ImageItem

StringItem

TextField

Canvas

Ticker

Display

http://mobilephones.kom.aau.dk

Major Classes of LCDUIMIDP 2.0

DAY 3 – J2ME

Displayable

Screen

Alert

List

Form

TextBox

Item

ChoiceGroup

DateField

Gauge

ImageItem

StringItem

TextField

Canvas

Ticker

CustomItem

SpacerDisplay

Page 7: Mobile Phone Programming - Aalborg Universitetkom.aau.dk/project/mobilephone/teaching/freestudy... · Object-oriented programming fundamentals DAY 3 – J2ME public class Car { int

http://mobilephones.kom.aau.dk

TextBoxDAY 3 – J2ME

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;...public class TextBox_Example extends MIDlet{

Display display=Display.getDisplay(this);public void startApp() {

//TextBox(title,text,size,contrains)TextBox textbox = new TextBox("Text Box Example","This is an example of a TextBox",50,0);display.setCurrent(textbox);

}…..}

http://mobilephones.kom.aau.dk

AlertDAY 3 – J2ME

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;...public class Alert_Example extends MIDlet{

Display display=Display.getDisplay(this);public void startApp() {

// Alert(label,text,Image,type)Alert alert= new Alert("Error Alert","This is an Error Alert example",null,AlertType.ERROR);alert.setTimeout(Alert.FOREVER);display.setCurrent(alert);

}…..}

Page 8: Mobile Phone Programming - Aalborg Universitetkom.aau.dk/project/mobilephone/teaching/freestudy... · Object-oriented programming fundamentals DAY 3 – J2ME public class Car { int

http://mobilephones.kom.aau.dk

List 1DAY 3 – J2ME

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;...public class Multiple_List extends MIDlet{

Display display=Display.getDisplay(this);

public void startApp() {

//List(label,type,items,Images)List list = new List("Multiple list", List.MULTIPLE,new String[] {"Python", "J2ME", "Symbian"}, null);display.setCurrent(list);

}…..}

http://mobilephones.kom.aau.dk

List 2DAY 3 – J2ME

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;...public class Implicit_List extends MIDlet{

Display display=Display.getDisplay(this);

public void startApp() {

//List(label,type,items,Images)List list = new List("Implicit list", List.IMPLICIT,new String[] {"Python", "J2ME", "Symbian"}, null);display.setCurrent(list);

}…..}

Page 9: Mobile Phone Programming - Aalborg Universitetkom.aau.dk/project/mobilephone/teaching/freestudy... · Object-oriented programming fundamentals DAY 3 – J2ME public class Car { int

http://mobilephones.kom.aau.dk

List 3DAY 3 – J2ME

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;...public class Exclusive_List extends MIDlet{

Display display=Display.getDisplay(this);

public void startApp() {

//List(label,type,items,Images)List list = new List("Exclusive list", List.EXCLUSIVE,new String[] {"Python", "J2ME", "Symbian"}, null);display.setCurrent(list);

}…..}

http://mobilephones.kom.aau.dk

Empty FormDAY 3 – J2ME

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;...public class Empty_Form extends MIDlet{

Display display=Display.getDisplay(this);

public void startApp() {

Form form = new Form("Empty Form");display.setCurrent(form);

}…..

}

Page 10: Mobile Phone Programming - Aalborg Universitetkom.aau.dk/project/mobilephone/teaching/freestudy... · Object-oriented programming fundamentals DAY 3 – J2ME public class Car { int

http://mobilephones.kom.aau.dk

Form with ChoiceGroup 1 DAY 3 – J2ME

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;...public class Choice_Group extends MIDlet{

Display display=Display.getDisplay(this);

public void startApp() {

Form form = new Form("Form example");//ChoiceGroup(label,type,elements,image)ChoiceGroup CourseEXCL = new ChoiceGroup ("Exclusive choice", Choice.EXCLUSIVE,new String[] {"Python",

"J2ME", "Symbian"}, null);form.append(CourseEXCL);ChoiceGroup CourseMULT = new ChoiceGroup ("Multiple choice", Choice.MULTIPLE,new String[] {"Python",

"J2ME", "Symbian"}, null);form.append(CourseMULT);display.setCurrent(form);

}…..}

http://mobilephones.kom.aau.dk

DAY 3 – J2MEForm with ChoiceGroup 2

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;...public class Choice_Group extends MIDlet{

Display display=Display.getDisplay(this);

public void startApp() {

Form form = new Form("Form example");//ChoiceGroup(label,type,elements,image)ChoiceGroup CoursePOP = new ChoiceGroup ("Pop Up choice", Choice.POPUP,new String[] {"Python", "J2ME",

"Symbian"}, null);form.append(CoursePOP);display.setCurrent(form);

}…..}

Page 11: Mobile Phone Programming - Aalborg Universitetkom.aau.dk/project/mobilephone/teaching/freestudy... · Object-oriented programming fundamentals DAY 3 – J2ME public class Car { int

http://mobilephones.kom.aau.dk

Form with DateFieldDAY 3 – J2ME

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;...public class Date_Field extends MIDlet{

Display display=Display.getDisplay(this);

public void startApp() {

Form form = new Form("Form example");// DateField(label,type);

DateField datefield = new DateField("Date Field Example", DateField.DATE_TIME);form.append(datefield);display.setCurrent(form);

}…..}

http://mobilephones.kom.aau.dk

Form with GaugeDAY 3 – J2ME

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;...public class Gauge_ex extends MIDlet{

Display display=Display.getDisplay(this);

public void startApp() {

Form form = new Form(“Gauge example");//Gauge(label,interaction,maxValue,InitialValue)Gauge gauge = new Gauge("Gauge example", false, 20, 4);form.append(gauge);display.setCurrent(form);

}…..}

Page 12: Mobile Phone Programming - Aalborg Universitetkom.aau.dk/project/mobilephone/teaching/freestudy... · Object-oriented programming fundamentals DAY 3 – J2ME public class Car { int

http://mobilephones.kom.aau.dk

Form with ImageItemDAY 3 – J2ME

import java.io.IOException;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;...public class Image_Item extends MIDlet{

Display display=Display.getDisplay(this);public void startApp() {

Form form = new Form("Form example");try {

Image Logo = Image.createImage("/images/logo.png");//ImageItem(label,Image,layout,alternative text)ImageItem imageitem =new ImageItem("ImageItem Example",Logo,ImageItem.LAYOUT_CENTER,"");form.append(imageitem);

} catch (IOException ex) {

System.out.println("Exception occurred: "+ex);}

display.setCurrent(form);}

…..}

http://mobilephones.kom.aau.dk

Form with StringItemDAY 3 – J2ME

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;...public class String_Item extends MIDlet{

Display display=Display.getDisplay(this);

public void startApp() {

Form form = new Form("Form example");// StringItem(title,text)StringItem stringitem= new StringItem("String Item", "This is an example of StringItem");form.append(stringitem);display.setCurrent(form);

}…..}

Page 13: Mobile Phone Programming - Aalborg Universitetkom.aau.dk/project/mobilephone/teaching/freestudy... · Object-oriented programming fundamentals DAY 3 – J2ME public class Car { int

http://mobilephones.kom.aau.dk

Form with TextFieldDAY 3 – J2ME

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;...public class Text_Field extends MIDlet{

Display display=Display.getDisplay(this);

public void startApp() {

Form form = new Form("Form example");// TextField(label,text,size,constrains);

TextField textfield =new TextField("TEXT FIELD","",25,TextField.ANY);form.append(textfield);display.setCurrent(form);

}…..}

http://mobilephones.kom.aau.dk

CanvasDAY 3 – J2ME

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;...public class Canvas_Example extends MIDlet{

Display display=Display.getDisplay(this);public void startApp() {

CanvasDemo canvas = new CanvasDemo();display.setCurrent(canvas);

}…}class CanvasDemo extends Canvas {

public void paint (Graphics g) {

g.setColor(50,50,255);g.fillRect (0, 0, getWidth (), getHeight ());g.setColor(200,200,200);g.drawString("Example of Canvas",0,20,0);g.setColor(0,0,100);g.drawLine (0, 40, 100, 200);g.setColor(200,0,0);g.fillRect (50, 70, 80, 50);

} }

Page 14: Mobile Phone Programming - Aalborg Universitetkom.aau.dk/project/mobilephone/teaching/freestudy... · Object-oriented programming fundamentals DAY 3 – J2ME public class Car { int

http://mobilephones.kom.aau.dk

Adding Command ButtonsDAY 3 – J2ME

import javax.microedition.midlet.MIDlet;import javax.microedition.lcdui.*;

public class Command_ex extends MIDlet implements CommandListener{

Display display=Display.getDisplay(this);Command ExitCmd = new Command ("Exit",Command.EXIT,7);Command CheckCmd = new Command ("Check",Command.SCREEN,1);public void startApp(){

Form form = new Form("Commands example");form.append("This is a Command button example. Please press the Check button ");form.addCommand(ExitCmd);form.addCommand(CheckCmd);form.setCommandListener(this);display.setCurrent(form);

}public void destroyApp(boolean unconditional){

notifyDestroyed();}

..........public void commandAction(Command c, Displayable d){

if (c==ExitCmd){

destroyApp(true);}else if (c==CheckCmd){

Alert alert = new Alert("Alert");alert.setString("Check button test");alert.setTimeout(3000); // 3 seconds display.setCurrent(alert, display.getCurrent() );

}}

}

http://mobilephones.kom.aau.dk

Example: smsZipperDAY 3 – J2ME