Draw String Using Canvas

Ads
 

Draw String Using Canvas

This example is used to draw string on different location which is shown in figure. The given code is used to show, how to draw string at different locations.

This example is used to draw string on different location which is shown in figure. The given code is used to show, how to draw string at different locations.

Draw String Using Canvas

     

This example is used to draw string on different location which is shown in figure. The given code is used to show, how to draw string at different locations.

 

 

 

 

 

g.drawString("Top/Left", 0, 0, Graphics.TOP | Graphics.LEFT);
g.drawString("Baseline/Center", getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.BASELINE);
g.drawString("Bottom/Right", getWidth(), getHeight(), Graphics.BOTTOM | Graphics.RIGHT);

In this code the Top/Left is used to draw the string on Top left corner, the Baseline/Center is used to draw the string in center and the Bottom/Right is used to draw the string on the bottom right corner. And the application is as follows:

 

Source Code of DrawString.java

  

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class DrawString extends MIDlet{
  private Display display;
  
  public void startApp(){
  display = Display.getDisplay(this);
  display.setCurrent (new TextCanvas())
  }

  public void pauseApp(){}

  public void destroyApp(boolean unconditional){}

  public void commandAction(){}
}

class TextCanvas extends Canvas{
  public void paint(Graphics g){
  g.setColor(25500);
  g.fillRect(00, getWidth(), getHeight());

  g.setColor(00255);
  g.drawString("Top/Left"00, Graphics.TOP | Graphics.LEFT);
  g.drawString("Baseline/Center", getWidth() 2, getHeight() 2
   Graphics.HCENTER | Graphics.BASELINE
);
  g.drawString("Bottom/Right", getWidth(), getHeight()

  Graphics.BOTTOM | Graphics.RIGHT);
  }
}

Download Source Code

Ads

Ads