midp application control
DESCRIPTION
MIDP Application ControlTRANSCRIPT
MIDP Application Control
MIDP Displayable class
MIDP High-level UI API
FormItemsListTextBoxAlertTickerChoice interfaceScreen layout
Form เหมื�อนเป็นแผ่�นกระดาษ ให�เอา component อ��นๆมืาวางได� และเราจะวาง Form ลงบน screen อ�กที�
Component ต่�างๆที��วางบน Form ได�คื�อ image, text field, data field, choice selection, items (ImageItem, String Item, TextField, DateField, ChoiceGroup, Gauge )
Form
Item เป็น superclass ของ ui หลายคืลาสที��สามืารถวางบน Form ได�
method ที��ที%างานก&บ Item – append, delete, insert, set
Item สามืารวางบน Screen object เพี�ยงหน(�งต่&วเที�าน&)น หากเราพียายามืจะวาง Item object บน Screen ต่&วอ��นๆ (หร�อแมื�กระที&�งต่&วเด*มื ) จะเก*ด Illegal StateException
Items
ChoiceGroupDateFieldGaugeImageItemStringItemTextField
ChoiceGroup( String label,
int choiceType)
ChoiceGroup( String label,
int choiceType,
String[] stringElements,
Image[] imageElements )
choiceType : EXCLUSIVE, MULTIPLE
ChoiceGroup
DateField
DateField (String label, int mode)
mode : DATE
DATE_TIME
TIME
Gauge ----------- ออกเส�ยงเหมื�อน make
Gauge( String label,
boolean interactive,
int maxValue,
int initValue)
ImageItem( String label,
Image img,
int layout,
String altText)
ImageItem
import javax.microedition.lcdui.*;import javax.microedition.midlet.*;public class myChoice extends MIDlet{
Display display;Form form;ChoiceGroup animal, fruit, color;public myChoice() {
form = new Form("ChoiceGroupDemo");String alist[] ={"Cat", "Dog", "Bat", "Ant"};String flist[] = {"mango", "apple","orange"};String clist[] = {"red", "green", "blue"};animal = new ChoiceGroup("Animal",Choice.EXCLUSIVE, alist, null);fruit = new ChoiceGroup("Fruit",Choice.EXCLUSIVE, flist, null);color = new ChoiceGroup("Color",Choice.MULTIPLE, clist, null);form.append(animal);form.append(fruit);form.append(color);
}
protected void startApp(){display = Display.getDisplay(this);display.setCurrent(form);
}….
}
import javax.microedition.lcdui.*;import javax.microedition.midlet.*;public class myChoice extends MIDlet{
Display display;Form form;ChoiceGroup animal, fruit, color;public myChoice() {
form = new Form("ChoiceGroupDemo");String alist[] ={"Cat", "Dog", "Bat", "Ant"};String flist[] = {"mango", "apple","orange"};String clist[] = {"red", "green", "blue"};animal = new ChoiceGroup("Animal",Choice.EXCLUSIVE, alist, null);fruit = new ChoiceGroup("Fruit",Choice.EXCLUSIVE, flist, null);color = new ChoiceGroup("Color",Choice.MULTIPLE, clist, null);form.append(animal);form.append(fruit);form.append(color);
}
protected void startApp(){display = Display.getDisplay(this);display.setCurrent(form);
}….
}
มาสร้�าง ChoiceGroup กัน
import javax.microedition.lcdui.*;import javax.microedition.midlet.*;public class HighLevel extends MIDlet { Display display; Form form; ChoiceGroup monster; DateField date; Gauge gauge; StringItem str; TextField name, password;public HighLevel() {
String mlist[] = {"Fire","Egg","Dragon"};monster = new ChoiceGroup("Chose
one",ChoiceGroup.EXCLUSIVE,mlist,null);date = new DateField("Start date", DateField.DATE_TIME);gauge = new Gauge("Volume",true,10,5);str = new StringItem("Level up","....");name = new TextField("Input name","", 20, TextField.ANY);password = new TextField("Input password","", 20,
TextField.PASSWORD);form = new Form("Items Demo");form.append(monster);form.append(date);form.append(gauge);form.append(str);form.append(name);form.append(password);
}protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);display.setCurrent(form);
}…..
import javax.microedition.lcdui.*;import javax.microedition.midlet.*;public class HighLevel extends MIDlet { Display display; Form form; ChoiceGroup monster; DateField date; Gauge gauge; StringItem str; TextField name, password;public HighLevel() {
String mlist[] = {"Fire","Egg","Dragon"};monster = new ChoiceGroup("Chose
one",ChoiceGroup.EXCLUSIVE,mlist,null);date = new DateField("Start date", DateField.DATE_TIME);gauge = new Gauge("Volume",true,10,5);str = new StringItem("Level up","....");name = new TextField("Input name","", 20, TextField.ANY);password = new TextField("Input password","", 20,
TextField.PASSWORD);form = new Form("Items Demo");form.append(monster);form.append(date);form.append(gauge);form.append(str);form.append(name);form.append(password);
}protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);display.setCurrent(form);
}…..
สร้�างงานด้�วย HighLevel
มืาสร�าง Form ร&บคื�าแบบ Advance ก&นด�กว�า
mixForm.java
จบ class เช้�า
ImageItem( String label, Image img,int layout, String altText);
layout : LAYOUT_CENTERLAYOUT_DEFAULTLAYOUT_LEFTLAYOUT_NEWLINE_AFTERLAYOUT_NEWLINE_BEFORELAYOUT_RIGHT
ImageItem( String label, Image img,int layout, String altText);
layout : LAYOUT_CENTERLAYOUT_DEFAULTLAYOUT_LEFTLAYOUT_NEWLINE_AFTERLAYOUT_NEWLINE_BEFORELAYOUT_RIGHT
ImageItem, Image
Image
เราสร�าง Image object โดยการเร�ยก createImage() และสร�างจาก ร-ป็ภาพี .png ที��เรามื�อย-�
Image.createImage(Image source) Image.createImage(String name)
การใช้� createImage() จะต่�อง try-catch IOEception ด&งน&)นในโป็รแกรมืต่�อง import java.io.* เมื��อเราได� Image จ(งจะน%า Image ไป็สร�าง ImageItem
อ�กที� * หมืายเหต่/ * เราสามืารถ append Image ลงบน
Form ได�เลย
มืาสร�างภาพีก&นเถอะสร�าง โป็รเจคื ImageWorld และสร�าง MIDlet
MyImage ภายในโป็รเจคืcopy image file .png ไว�ใน res โฟลเดอร1
ภายในโป็รเจคืต่&วอย�างน�)เราจะเพี*�มื ImageItem ลงบน Form
โดยการ สร�าง Image object จากไฟล1 0.png สร�าง ImageItem จาก Image object append ImageItem ลงบน Form
import javax.microedition.midlet*;import javax.microedition.lcdui*;import java.io*;public class myImage extends MIDlet{
Form form;Display display;public myImage(){
form = new Form("image demo");Image im = null;ImageItem imi;try{
im = Image.createImage("/res/0.png"); //สร�าง Image จากไฟล1 0.png
}catch(IOException e){}
imi = new ImageItem("monster",im,ImageItem.LAYOUT_CENTER,"");form.append(imi);
}public void startApp(){
display = Display.getDisplay(this);display.setCurrent(form);
}.....
}
import javax.microedition.midlet*. ;import javax.microedition.lcdui*;import java.io*;public class myImage extends MIDlet{
Form form;Display display;public myImage(){
form = new Form("image demo");Image im = null;ImageItem imi;try{
im = Image.createImage("/res/0.png"); //สร�าง Image จากไฟล1 0.png
}catch(IOException e){}
imi = new ImageItem("monster",im,ImageItem.LAYOUT_CENTER,"");form.append(imi);
}public void startApp(){
display = Display.getDisplay(this);display.setCurrent(form);
}.....
}
สร้�างภาพจากั file 0.png
สร�าง ImageItem object จากร-ป็ 6 ร-ป็สร�างด�วย ImageItem โดยใช้� array
import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.io.*;public class imageArray extends MIDlet{
Form form;Display display;public imageArray(){
form = new Form("image array demo");Image im= null;ImageItem imi[] = new ImageItem[6];
try{ for(int i =0;i<6;i++){
im = Image.createImage("/res/"+i+".png");imi[i]= new ImageItem("",im,ImageItem.LAYOUT_LEFT,"");form.append(imi[i]);
}}catch(IOException e){}
}public void startApp(){
display = Display.getDisplay(this);display.setCurrent(form);
}……
import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.io.*;public class imageArray extends MIDlet{
Form form;Display display;public imageArray(){
form = new Form("image array demo");Image im= null;ImageItem imi[] = new ImageItem[6];
try{ for(int i =0;i<6;i++){
im = Image.createImage("/res/"+i+".png");imi[i]= new ImageItem("",im,ImageItem.LAYOUT_LEFT,"");form.append(imi[i]);
}}catch(IOException e){}
}public void startApp(){
display = Display.getDisplay(this);display.setCurrent(form);
}……
การที%า Image Slide Show แบบง�ายๆ