MIDP: Form Custom and Image Items

Post on 15-Jan-2015

2.226 views 1 download

description

 

Transcript of MIDP: Form Custom and Image Items

MIDP GUI: Custom and Image Items

Jussi PohjolainenTAMK University of Applied Sciences

Displayable Class Hierarchy

javax.microedition.lcduijavax.microedition.lcdui javax.microedition.lcdui.gamejavax.microedition.lcdui.game

DisplayableDisplayable

AlertAlert ListList FormForm TextBoxTextBox

ScreenScreen

CanvasCanvas GameCanvasGameCanvas

Form

Form Item

ChoiceGroupChoiceGroup CustomItemCustomItem DateFieldDateField GaugeGauge ImageItemImageItemStringItemStringItem TextFieldTextField

*

Custom Items

• Custom Items = Making your own custom item.• Inherit your own Custom Item from CustomItem class.• CustomItem has following abstract methods

– int getPrefContentWidth(int height)• Item's preferred width

– int getPrefContentHeight(int width)• Item's preferred height

– int getMinContentWidth()• Item's minimum width

– int getMinContentHeight()• Item's minimum height

– void paint(Graphics g, int w, int h)

Own Custom Item

{abstract} CustomItem

{abstract} int getPrefContentWidth(int height)

{abstract} int getPrefContentHeight(int width)

{abstract} int getMinContentWidth()

{abstract} int getMinContentHeight()

{abstract} void paint(Graphics g, int w, int h)

MyItem

Form Item

Gauge ImageItemStringItem TextField

*

Create a class that inherites 

CustomItem

Custom Items method's

• int getPrefContentWidth(int height)– returns the item's width– The parameter is height! "What width do you

want, if the height is 18?"• int getPrefContentHeight(int width)– "What height do you want, if the width is 18?"

Events• It is possible to write the following methods in your

own Custom Item-class:– void keyPressed(int keyCode)– void keyReleased(int keyCode)– void keyRepeated(int keyCode)– void pointerPressed(int x, int y)– void pointerReleased(int x, int y)– void pointerDragged(int x, int y)

• KeyCode can be found in Canvas-class:– Canvas.FIRE, Canvas.DOWN, Canvas.UP ...– Canvas.NUM0, Canvas.NUM1 ...

Exampleclass MyItem extends CustomItem{ public MyItem(String a){ super(a); }

public int getMinContentWidth(){ return 100; }public int getMinContentHeight(){ return 30; }public int getPrefContentWidth(int h){ return getMinContentWidth(); }public int getPrefContentHeight(int w){ return getMinContentHeight();}public void paint(Graphics g, int w, int h){

g.setColor(0xffffffff);g.fillRect(0,0,w,h);

}protected void keyPressed(int keyCode){

if(getGameAction(keyCode) == Canvas.FIRE){// Do Something

}}

}

Image Item

• Displaying Image in Form.• Constructors– ImageItem(String label, Image img, int layout, String altText)

– ImageItem(String label, Image image, int layout, String altText, int appearanceMode)

• Typical set/get methods

Image

• MIDP devices should support atleast png-format.

• Creating Image-object:– Image picture = Image.createImage(...);

• There are many createImage-methods (see the api).

• Maybe the easiest:– public static Image createImage(String name)

Example

// [ Save the picture in the res-folder ]

// Creating picture-object (must be inside

// try-catch...

Image picture = Image.createImage("picture.png");

// Creating ImageItem-object

ImageItem item = new ImageItem("My Picture", picture, Item.LAYOUT_CENTER, "");

// Adding ImageItem to Form

mMainForm.append(item);