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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Draw Font Using Canvas Example 
 

This example is used to draw the different types of font using Canvas class. The following line of code is used to show the different style, size and faces of font.

 

Draw Font Using Canvas Example

                         

This example is used to draw the different types of font using Canvas class. The following line of code is used to show the different style, size and faces of font.

 

 

 

 

 

static final int[] styles = {Font.STYLE_PLAIN, Font.STYLE_BOLD, Font.STYLE_ITALIC};

static final int[] sizes = {Font.SIZE_SMALL, Font.SIZE_MEDIUM, Font.SIZE_LARGE};

static final int[] faces = {Font.FACE_SYSTEM, Font.FACE_MONOSPACE, Font.FACE_PROPORTIONAL};

  Here we have set the different Faces for the text...

  • FACE_MONOSPACE
  • FACE_PROPORTIONAL
  • FACE_SYSTEM

Now we are setting their size

  • SIZE_LARGE
  • SIZE_MEDIUM
  • SIZE_SMALL 

and the style of the text

  • STYLE_BOLD
  • STYLE_ITALIC
  • STYLE_PLAIN
  • STYLE_UNDERLINED

Following methods is used in this class are:

Methods Variable Types
charsWidth(char[] ch, int offset, int length) int
charWidth(char ch) int
getBaselinePosition() int
getDefaultFont()  int
getFace() int
getFont(int face, int style, int size) Font
getHeight() int
getSize() int
getStyle() int
isBold() boolean
isItalic() boolean
isPlain() boolean
isUnderlined() boolean
stringWidth(String str) int
substringWidth(String str, int offset, int len) int

The Application is as follows:

 

Source Code of DrawFont.java

  

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

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

  public void pauseApp(){}

  public void destroyApp(boolean unconditional){}

  public void commandAction(){}
}

class FontCanvas extends Canvas{
  static final int[] styles = {Font.STYLE_PLAIN, 
    Font.STYLE_BOLD, Font.STYLE_ITALIC};

  static final int[] sizes = {Font.SIZE_SMALL, 
  Font.SIZE_MEDIUM, Font.SIZE_LARGE
};

  static final int[] faces = {Font.FACE_SYSTEM, 
  Font.FACE_MONOSPACE, Font.FACE_PROPORTIONAL
};

  public void paint(Graphics g){
    Font font = null;
    int y = 0;
    g.setColor(202238255);
    g.fillRect(00, getWidth(), getHeight());

    g.setColor(255876);
    for(int size = 0; size < sizes.length; size++){
      for(int face = 0; face < faces.length; face++){
        int x = 0;
        for(int style = 0; style < styles.length; style++){
          font = Font.getFont(faces [face], styles [style], sizes [size]);
          g.setFont(font);
          g.drawString(" Font ", x+1, y+1, Graphics.TOP | Graphics.LEFT);
          g.drawRect(x, y, font.stringWidth(" Font "1, font.getHeight() 1);
          x += font.stringWidth (" Font "1;
        }
        y += font.getHeight() 1;
      }
    }
  }
}

 

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.