Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Text Example in J2ME 
 

In J2ME programming language canvas class is used to paint and draw the diagrams. Using the same canvas class we are going to draw a box around the text in our show text MIDlet Example.

 

Text Example in J2ME

                         

In J2ME programming language canvas class is used to paint and draw the diagrams. Using the same canvas class we are going to draw a box around the text in our show text MIDlet Example. We have created a class called CanvasBoxText that extends the predefined canvas class to draw the box. In the code we have used different methods to get the values, these are..

  • int width = getWidth();     
  • int height = getHeight();    
  • g.setColor(255, 0, 0);     
  • g.fillRect(0, 0, width, height);     
  • g.setColor(0, 0, 255);    
  • String sandeep = "SANDEEP";     
  • int w = font.stringWidth(sandeep);     
  • int h = font.getHeight();     
  • int x = (width - w) / 2;     
  • int y = height / 2;    
  • g.setFont(font);     
  • g.drawString(sandeep, x, y, Graphics.TOP | Graphics.LEFT);     
  • g.drawRect(x, y, w, h);

After running the example you will get the output as given below..

In the output you can easily find out the text "SANDEEP" that is been displayed in the box. As I mentioned earlier, we need a canvas class to draw such kind of graphics in the J2ME application.

J2ME Source Code "BoxTextCanvas.java"

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

public class BoxTextCanvas extends MIDlet{
  private Display display;

  public void startApp(){
    Canvas canvas = new CanvasBoxText();
    display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }

  public void pauseApp(){}

  public void destroyApp(boolean unconditional){}
}

class CanvasBoxText extends Canvas{
  private Font font;

  public CanvasBoxText(){
    font = Font.getFont(Font.FACE_PROPORTIONAL, 
    Font.STYLE_PLAIN, Font.SIZE_LARGE);

  }

  public void paint(Graphics g){
    int width = getWidth();
    int height = getHeight();

    g.setColor(25500);
    g.fillRect(00, width, height);
    g.setColor(00255);

    String sandeep = "SANDEEP";
    int w = font.stringWidth(sandeep);
    int h = font.getHeight();
    int x = (width - w) / 2;
    int y = height / 2;

    g.setFont(font);
    g.drawString(sandeep, x, y, Graphics.TOP | Graphics.LEFT);
    g.drawRect(x, y, w, h);
  }
}

Download Source Code

                         

» View all related tutorials
Related Tags: c j2me diff types type if example to rectangle draw exam raw angle e il pe in different m nt

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.