More Related Content
More from Jenchoke Tachagomain (20)
MIDP Application Control
- 3. MIDP High-level UI API
Form
Items
List
TextBox
Alert
Ticker
Choice interface
Screen layout
- 4. Form
Form เหมือนเป็นแผ่นกระดาษ ให้เอา component
อืนๆมาวางได้ และเราจะวาง Form ลงบน screen อีก
่
ที
Component ต่างๆที่วางบน Form ได้คือ image, text
field, data field, choice selection, items
(ImageItem, String Item, TextField, DateField,
ChoiceGroup, Gauge )
- 5. Item เป็น superclass ของ ui หลายคลาสที่
สามารถวางบน Form ได้
method ที่ทำางานกับ Item – append, delete,
insert, set
Item สามารวางบน Screen object เพียงหนึ่ง
ตัวเท่านั้น หากเราพยายามจะวาง Item object
บน Screen ตัวอื่นๆ (หรือแม้กระทั่งตัวเดิม) จะ
เกิด Illegal StateException
- 7. ChoiceGroup
ChoiceGroup( String label,
int choiceType)
ChoiceGroup( String label,
int choiceType,
String[] stringElements,
Image[] imageElements )
choiceType : EXCLUSIVE, MULTIPLE
- 9. Gauge ----------- ออกเสียง
เหมือน make
Gauge( String label,
boolean interactive,
int maxValue,
int initValue)
- 11. import javax.microedition.lcdui.*; มาสร้าง
import javax.microedition.midlet.*;
public class myChoice extends MIDlet{ ChoiceGroup
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);
}
….
}
- 12. import javax.microedition.lcdui.*;
import javax.microedition.midlet.*; สร้างงานด้วย HighLevel
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);
}
…..
- 14. ImageItem, Image
ImageItem( String label, Image img,
int layout, String altText);
layout : LAYOUT_CENTER
LAYOUT_DEFAULT
LAYOUT_LEFT
LAYOUT_NEWLINE_AFTER
LAYOUT_NEWLINE_BEFORE
LAYOUT_RIGHT
- 15. 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 ได้เลย
- 16. มาสร้างภาพกันเถอะ
สร้าง โปรเจค ImageWorld และสร้าง MIDlet
MyImage
ภายในโปรเจค
copy image file .png ไว้ใน res โฟลเดอร์
ภายในโปรเจค
ตัวอย่างนี้เราจะเพิ่ม ImageItem ลงบน Form
โดยการ
สร้าง Image object จากไฟล์ 0.png
สร้าง ImageItem จาก Image object
append ImageItem ลงบน Form
- 17. import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*; สร้างภาพจาก file 0.png
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 จากไฟล์ 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);
}
.....
}
- 19. 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);
}
……