×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    1

    Box with text in it using canvas: J2ME

    if we want to draw string in canvas, simply we can use g.drawString... but if i make a long text, its not automatically fit, its cross the line.

    how can i make the strings go down automatically??

  2. #2
    Registered User
    Join Date
    Aug 2013
    Posts
    37

    Re: Box with text in it using canvas: J2ME

    Bro you can try this..
    .
    .
    Font font;
    font = Font.getFont(0,1,8);
    int k = 230;
    List list = new List("Text",3);
    String s = "A big line which doesnt fit in the screen";
    int len = s.length();
    StringBuffer buffer = new StringBuffer(96);
    for(int x = 0;x<len;x + +)
    {
    char a = s.charAt(x);
    buffer.append(a);
    if(font.stringWidth(buffer.toString()) >= k)
    {
    list.append(buffer.toString(), null);
    buffer.delete(0,buffer.length());
    }
    else if(x == (len-1))
    {
    list.append(buffer.toString(),null);
    buffer.delete(0,buffer.length());
    }
    }
    .
    Now in the paint part you can do this;
    .
    .
    int size = list.size();
    int gap = 5;
    for(int i = 0;i<size;i + +)
    {
    g.drawString(list.getString(i),2,gap,33);
    gap + = font.getHeight() + 3;
    }
    .
    .
    This will do what you want..
    You can optimize the string parsing part which can detect weather the strings have '\n' or not and change lines according to it.. The list.append() command stored the string in the next line.. So each time you use it line will change..
    Last edited by subha095; 2013-09-06 at 10:52.

  3. #3
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,736

    Re: Box with text in it using canvas: J2ME

    Here is a related Wiki article: http://developer.nokia.com/Community...ing_in_Java_ME (of course it is built around the same Font.stringWidth call as suggested by Subha095)

  4. #4
    Regular Contributor
    Join Date
    Aug 2011
    Location
    Berlin
    Posts
    288

    Re: Box with text in it using canvas: J2ME

    You should also bear in mind that calls to font.StringWidth() are slow - unfortunately necessary but still slow. If you want to keep your app responsive, you are better off caching font.StringWidth() results in a hashtable to avoid recalculating them where possible. Look at Michael Samarin's presentation here to discover the optimal way to do this.

Similar Threads

  1. how to draw multi line text on canvas in j2me
    By iraqy2010 in forum Mobile Java General
    Replies: 3
    Last Post: 2011-10-05, 08:36
  2. Return from virtual keyboard canvas to main canvas in j2me
    By Meena ganesan in forum Mobile Java General
    Replies: 2
    Last Post: 2011-04-15, 13:55
  3. Scrollable text on j2me canvas
    By knights123 in forum Mobile Java General
    Replies: 1
    Last Post: 2009-07-30, 18:18
  4. problem with canvas.text and appuifw.text
    By luke14free in forum Symbian
    Replies: 1
    Last Post: 2006-11-01, 09:43
  5. self.canvas.text
    By harp73 in forum Symbian
    Replies: 3
    Last Post: 2005-07-14, 09:29

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×