Home J2me Draw String Using Canvas



Draw String Using Canvas
Posted on: October 24, 2008 at 12:00 AM
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

Related Tags for Draw String Using Canvas:
cstringdiffiosedthislocationshowifexampletodrawexamrawsheuseindifferentmnttrcalochowcatxawhichxampsatisivmplcodcodestrssriringrenthshostatifepleplodeono


More Tutorials from this section

Ask Questions?    Discuss: Draw String Using Canvas  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.