2010-08-06 2 views
0

j2me 애플리케이션을 개발할 예정입니다. J2ME에서 화면 너비 크기에 따라 캔버스에 텍스트를 감쌀 수있는 방법을 알고 싶습니다.캔버스에 J2ME의 텍스트 감싸기

+1

몇 가지 답변을 받아 즐길 수 ! – Azlam

답변

1

캔버스의 최대 너비에 도달 할 때마다 사용자가 그려야 할 문자열의 너비를 계산하고 새 줄을 시작해야합니다 (문자열을 분할 함).

void paint(Graphics _g) { 
    String t = "text to draw"; 
    int px_consumed = _g.getFont().substringWidth(t, 0, t.length())} 
}
5

이 내가 내 응용 프로그램에 사용되는 내 코드는 선 벡터를 감싸 것입니다 및 u는 캔버스에있는 X 지점에 그릴 수 있습니다.

public static Vector wrapToLines(String text, Font f, int maxWidth) { 
     Vector lines = new Vector(); 
     boolean paragraphFormat = false; 
     if (text == null) { 
      return lines; 
     } 
     if (f.stringWidth(text) < maxWidth) { 
      lines.add(text); 
      return lines; 
     } else { 

      char chars[] = text.toCharArray(); 
      int len = chars.length; 
      int count = 0; 
      int charWidth = 0; 
      int curLinePosStart = 0; 
      while (count < len) { 
       if ((charWidth += f.charWidth(chars[count])) > (maxWidth - 4) || count == len - 1) // wrap to next line 
       { 
        if (count == len - 1) { 
         count++; 
        } 
        String line = new String(chars, curLinePosStart, count - curLinePosStart); 
        if (paragraphFormat) { 
         int lastSpacePosition = line.lastIndexOf(" "); 
         String l = new String(chars, curLinePosStart, (lastSpacePosition != -1) ? lastSpacePosition + 1 : (count == len - 1) ? count - curLinePosStart + 1 : count - curLinePosStart); 
         lines.add(l); 
         curLinePosStart = (lastSpacePosition != -1) ? lastSpacePosition + 1 : count; 
        } else { 
         lines.add(line); 
         curLinePosStart = count; 
        } 
        charWidth = 0; 
       } 
       count++; 
      } 
      return lines; 

     } 
    } 

그냥 루프

위한에서 실행하는 동안
int y=0; 
int linespacing=4; 
    for(int i=0;i<lines.size();i++) 
    { 
    g.drawString((String)lines.get(i),10,y,0); 
    y+=(i!=lines.size()-1)?(font.getHeight()+linespacing):0; 
    } 

이 :)