Introduction to MIDletPascal language


Writing programs in MIDletPascal is very simple. All you need to know is a bit of Pascal, and the way that MIDletPascal does drawing on the screen.

MIDletPascal has various functions and procedures used for drawing to the device display (also called 'canvas') such as drawText, drawLine, fillRect etc. It is important to bear in mind that these procedures do not actually draw on the device display; they draw to the image that is in device’s memory instead. So if you have the following program:

begin
  drawText(‘Hello world’, 0, 0);
end.

you will get nothing on your display, but the in-memory image will have the ‘Hello world’ written on it. To copy the in-memory image to the device display, call the repaint procedure:

begin
  drawText(‘Hello world’, 0, 0);
  repaint;
end.

You should note here that the repaint procedure is expensive, so you should call it only when you really need to update the device display. The following example illustrates good and bad use of repaint procedure:

Bad use:    Good use:
begin
  drawLine(0, 0, 25, 25);
  repaint;
  drawLine(0, 25, 0, 25);
  repaint;
end.
    
begin
  drawLine(0, 0, 25, 25);
  drawLine(0, 25, 0, 25);
  repaint;
end.

If you type any of the above examples into MIDletPascal, compile them and run them on your mobile device, it will seem that the programs weren’t even started. That is because the programs finished running almost at the same time they were started. If you want to write the program that waits two seconds the drawing is done, use the delay function:

begin
  drawText(‘Hello world’, 0, 0);
  repaint;
  delay(2000); { 2000 ms = 2 sec }
end.

You might also want to write a program that waits until the user presses any key:

begin
  drawText(‘Hello world’, 0, 0);
  repaint;

  { wait for the key press }
  while getKeyClicked = KE_NONE do
  begin end; 
end.

For more information about capturing user input, see the reference for getKeyClicked, getKeyPressed and keyToAction.

Sometimes the canvas is not good enough for some functions. For example, asking the user to enter his name and capturing that name is almost impossible with the canvas. For creating user interfaces with text fields, choice groups and other similar elements, MIDletPascal uses forms.

Now you are ready to start writing your own MIDlets. It is recommended that you read the chapters following this one. It would also be very good to visit http://www.midletpascal.com/midlets.php and download the code for the MIDlets available on that page.