Home  | My Account  |  Membership
WorldDeveloper.org
Forum Details
Topics
Posts
Categories
 
577 member(s)
718,905 hit(s)
14 categories
51 topics
26 messages
0 online
Forums: Code Post
Line Wrapping Utility for Drawing Strings on a Canvas
hostj2me
43 topic(s)
23 post(s)

January 29, 2007 8:14 PM


/*
* Copyright (C) 2006 Astrient Labs, LLC Licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* Astrient Labs, LLC
* www.astrientlabs.com
* rashid@astrientlabs.com
* Rashid Mayes 2006
*/
package com.astrientlabs.text;

import java.util.Enumeration;
import java.util.NoSuchElementException;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public class LineEnumeration implements Enumeration
{
private Font font;

private String text;
private int width;
private int position;
private int length;
private int start = 0;

public LineEnumeration(Font font, String text, int width)
{
this.font = font;
this.text = text;
this.width = width;
this.length = text.length();
}

public boolean hasMoreElements()
{
return (position (length - 1));
}

public Object nextElement() throws NoSuchElementException
{
try
{
int next = next();

String s = text.substring(start, next);
start = next;

if (text.length() - 1 > start && (text.charAt(start) == ' '
|| text.charAt(start) == '\n'))
{
position++;
start++;
}

return s;
}
catch (IndexOutOfBoundsException e)
{
throw new NoSuchElementException(e.getMessage());
}
catch (Exception e)
{
throw new NoSuchElementException(e.getMessage());
}
}

private int next()
{
int i = position;
int lastBreak = -1;

for (; i length
&& font.stringWidth(text.substring(position, i)) = width; i++)
{
if (text.charAt(i) == ' ')
{
lastBreak = i;
}
else if (text.charAt(i) == '\n')
{
lastBreak = i;
break;
}
}

if (i == length)
{
position = i;
}
else if (lastBreak == position)
{
position++;
}
else if (lastBreak position)
{
position = i;
}
else
{
position = lastBreak;
}

return position;
}

public int writeTo(Graphics g, int startx, int starty, int maxY, Font font)
{
int fontHeight = font.getHeight() + 1;

while (hasMoreElements() && starty maxY)
{
g.drawString(String.valueOf(nextElement()).trim(),
startx, starty, Graphics.TOP | Graphics.LEFT);
starty += fontHeight;
}

return starty;
}

public void reset()
{
start = 0;
position = 0;
}
}


hostj2me
43 topic(s)
23 post(s)

December 21, 2008 8:37 PM

Within the paint method:


String longText = ...;
Font myFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
LineEnumeration e = new LineEnumeration(myFont,,getWidth());
e.writeTo(g,10,10,myFont);

Alternative:

LineEnumeration e = new LineEnumeration(myFont,myText,lineWidth);
while ( e.hasMoreElements() )
{
g.drawString(e.nextElement().toString(), startX,startY, Graphics.TOP | Graphics.LEFT);
startY += myFont.getHeight();
}



Also see:

http://hostj2me.com/appdetails.html?id=6


©2008 WorldDeveloper.org - PO Box 310575 Miami, FL 33231

WorldDeveloper.org is a managed social network for application developers around the world.