11
MIDP GUI: Custom and Image Items Jussi Pohjolainen TAMK University of Applied Sciences

MIDP: Form Custom and Image Items

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: MIDP: Form Custom and Image Items

MIDP GUI: Custom and Image Items

Jussi PohjolainenTAMK University of Applied Sciences

Page 2: MIDP: Form Custom and Image Items

Displayable Class Hierarchy

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

DisplayableDisplayable

AlertAlert ListList FormForm TextBoxTextBox

ScreenScreen

CanvasCanvas GameCanvasGameCanvas

Page 3: MIDP: Form Custom and Image Items

Form

Form Item

ChoiceGroupChoiceGroup CustomItemCustomItem DateFieldDateField GaugeGauge ImageItemImageItemStringItemStringItem TextFieldTextField

*

Page 4: MIDP: Form Custom and Image Items

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)

Page 5: MIDP: Form Custom and Image Items

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

Page 6: MIDP: Form Custom and Image Items

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?"

Page 7: MIDP: Form Custom and Image Items

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 ...

Page 8: MIDP: Form Custom and Image Items

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

}}

}

Page 9: MIDP: Form Custom and Image Items

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

Page 10: MIDP: Form Custom and Image Items

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)

Page 11: MIDP: Form Custom and Image Items

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);