Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home J2me Draw String Using Canvas

Related Tutorials


 
 

Share on Google+Share on Google+

Draw String Using Canvas

Advertisement
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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 24, 2008

Related Tutorials

Discuss: Draw String Using Canvas  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com