Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Putting components in MIDP 2.0

 
Bilal Sallakh
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I'm new to the nice world of J2ME.

I'm working with MIDP 2.0 on a P900 Sony-Ericsson simulator using Borland JBuilder.

My program is a simple character recognizer.

I want to display three things on the screen.

1- an area where the user draws the shape.
2- a TextField where the program shows the result.
3- a button (or menu) to go to programs' options.

I used a GameCanvas as a drawing area, and overrided the pointerPressed pointerDragged pointerReleased methods so to draw the trace of the pen.

But I've problems putting other component beside the GameCanvas in the same time.

Uptil now I have:

public class DrawingCanvas extends GameCanvas implements CommandListener {
DrawingCanvas() {super(false);}
protected void pointerPressed(int x, int y) {...}
protected void pointerReleased(int x, int y) {
getGraphics().drawLine(....);
}
protected void pointerDragged(int x, int y) {...}
public void commandAction(Command command, Displayable displayable) {}
}


public class HWRlet extends MIDlet {

protected void startApp() throws MIDletStateChangeException {
DrawingCanvas canv = new DrawingCanvas();
can.addCommand(new Command("Exit", Command.EXIT, 0));
Display.getDisplay(this).setCurrent(canv);
}

protected void pauseApp() {}
protected void destroyApp(boolean _boolean) throws MIDletStateChangeException {}
}


I tried to use a form:

Form fr = new Form("Recognizer Form");
Display.getDisplay(this).setCurrent(can);
fr.append(new TextField);
// fr.append(canv);

But it doesn't work (since append doesn't take a Canvas parameter).

What should I do in order to display them togother?

I tried to use a CustomItem instead of GameCanvas but it doesn't contain the getGraphics() method neccessary for drawing the pen's trace.

Do anyone has an idea?

Thanks in Advance.
 
Yuri Magrisso
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In MIDP the GUI components can be divided into 'Screen' components and 'Item' compontents. The 'Screen' components take up the whole screen of the device and they cannot be added to other 'Screen' components. GameCanvas and Form are two screen components and you cannot add GameCanvas to Form. Only 'Item' compontents can be added to a Form.

CustomItem provides a method 'paint(Graphics g)', which is called every time the component has to be repainted. In this method you can draw the pen's trace.

--------------
Yuri
 
Bilal Sallakh
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Yuri.

I'm using the CustomItem but it has one (annoying) limitation as you said: One can't draw except in the paint(Graphics g), so there is no getGraphics() as in GameCanvas. I tried to go around it by keeping a reference to the Graphics object I obrain in paint(Graphics g) to use it outside, but as I expected, it just failed (No back-doors in J2ME). The problem is that I do need to draw the whole pen trajectory just to add a new segment so instead of an O(n) I'm forced to have O(n2)...
Do anyone knows about a better berformence (even using external libraries but MIDP-2.0-compantible)? kAWT and kjava seems to target the PalmOS! I'm using te Sony-Ericsson-P900 as an emulator (Mobile devices tend to be less powerful than PDAs)...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic