Canvas class of javax.microedition.lcdui package gives faciltiy of low level level graphics on mobile. An application gets the drawing area by methods getWidth() and getHeight().

The illustration below draws a rectangle on the mobile device's screen:

 public void paint(Graphics g) 
  // get the dimensions of the screen:
  int width = getWidth ();
  int height = getHeight();
  // clear the screen (paint it white):
  g.fillRect(0, 0, width, height);
  Font font = g.getFont();
  int fontHeight = font.getHeight();
  int fontWidth = font.stringWidth("Hello World!");
  // set the text color to red:
  g.setColor(255, 0, 0);
  g.drawString("Hello World!", (width - fontWidth)/2,
    (height - fontHeight)/2,g.TOP|g.LEFT);





 Related Tips