Button Image Problem

元气小坏坏 提交于 2019-12-04 21:08:20

You could keep some sort of data structure of your buttons indicating a traversal order. You could then listen for the key pressed events on your canvas and based on which key was pressed (e.g. left, right, up, down, select etc) you could update which of the buttons is now in focus (using a simple index into your data structure). Your paint method could be extended to draw a border of some sort around the image that has the focus.

Now when you listen for the select key press you will know which button has the focus and therefore which action to perform.

More advanced issues here would be refreshing (repainting the screen) after each key press to update the image etc to show which has focus. You wouldn't want to redraw the entire screen, instead you can define areas of the screen to repaint based on which buttons will have changed (i.e. the button that previously had focus and the button that now has focus).

Another alternative would be to not use the low level canvas class but instead look at using a framework like LWUIT which provides buttons etc. and layout managers, much like Swing. This might be a lot easier than trying to draw (a browser by the looks of it) using the low level API.

michael aubert

You can't create a button on a Canvas in J2ME but there are alternatives:

  • Use a javax.microedition.lcdui.Form, add a ImageItem with appearance BUTTON to it, add a Command to the ImageItem.

  • Use key and/or pointer events on your DrawImageCanvas by overiding Canvas.keyPressed(), Canvas.keyreleased(), Canvas.pointerPressed() and/or Canvas.pointerReleased(). You can even add a border to the image so it looks more like a button.

  • You might want to experiment with a javax.microedition.lcdui.CustomItem inside a Form as an intermediary solution.

There is decent documentation for all these classes and methods in the MIDP specification:

http://www.jcp.org/en/jsr/detail?id=118

You might also want to look at the documentation of LWUIT. It is an open source graphical library published by Sun.

Try setting the appearance mode of the ImageItem object to Item.BUTTON, e.g.

ImageItem item = new ImageItem(null, image, ImageItem.LAYOUT_LEFT, null, Item.BUTTON);

Also try adding a command to the ImageItem.

item.setDefaultCommand(new Command("Back", Command.ITEM, 1); 

Then finally define the CommandListener:

item.setItemCommandListener(/* itemCommandListener */);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!