2012-08-26 21 views
5

जावा में किसी छवि पर लिपटे पाठ को मैं कैसे प्रस्तुत कर सकता हूं, क्या पाठ प्रस्तुत करने का कोई अंतर्निहित तरीका है ताकि यह graphics2D ऑब्जेक्ट पर एक आयताकार तक सीमित हो?जावा

मुझे पता है कि मैं Graphics2D.drawString का उपयोग कर सकता हूं लेकिन यह केवल टेक्स्ट की सिनलज लाइन खींचता है।

मुझे यह भी पता जब गाया कुछ Graphics2D graphics वस्तु पर कुछ Font font का उपयोग कर कि मैं

FontMetrics fm= graphics.getFontMetrics(font); 
Rectangle2D rect=fm.getStringBounds("Some Text",graphics); 

उपयोग कर सकते हैं एक स्ट्रिंग की सीमा के बारे में जानकारी प्राप्त करने के लिए।

तो मैं लूपिंग शुरू कर सकता था, मेरी स्ट्रिंग तोड़ सकता था और इसलिए इसे कुछ आयत के अंदर फिट करने के लिए मजबूर कर सकता था।

लेकिन मैं बहुत उन लिखने के लिए ...

कोई तैयार समारोह है कि मेरे लिए यह करना होगा है देखना पसंद नहीं करते हैं?

+0

मैं व्यक्तिगत रूप से उदाहरण के रूप में यह अपेक्षाकृत प्राप्त करने के लिए जल्दी हो जाता है एंड्रयू ने साबित कर दिया की तरह। हालांकि, मैंने पिछले दृष्टिकोण में इस दृष्टिकोण का उपयोग किया था http://java.sun.com/developer/onlineTraining/Media/2DText/style.html#multiple – MadProgrammer

+0

दो विकल्पों पर कोई पेशेवर और विपक्ष? – epeleg

+0

मैं कहूंगा कि एंड्रयू का समाधान आसान है, IMHO – MadProgrammer

उत्तर

4

यहाँ यह हो सकता है आप के लिए क्या देख रहे:

StringUtils.java:

import java.awt.FontMetrics; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Iterator; 
import java.util.List; 

/** 
* Globally available utility classes, mostly for string manipulation. 
* 
* @author Jim Menard, <a href="mailto:[email protected]">[email protected]</a> 
*/ 
public class StringUtils { 
    /** 
    * Returns an array of strings, one for each line in the string after it has 
    * been wrapped to fit lines of <var>maxWidth</var>. Lines end with any of 
    * cr, lf, or cr lf. A line ending at the end of the string will not output a 
    * further, empty string. 
    * <p> 
    * This code assumes <var>str</var> is not <code>null</code>. 
    * 
    * @param str 
    *   the string to split 
    * @param fm 
    *   needed for string width calculations 
    * @param maxWidth 
    *   the max line width, in points 
    * @return a non-empty list of strings 
    */ 
    public static List wrap(String str, FontMetrics fm, int maxWidth) { 
    List lines = splitIntoLines(str); 
    if (lines.size() == 0) 
     return lines; 

    ArrayList strings = new ArrayList(); 
    for (Iterator iter = lines.iterator(); iter.hasNext();) 
     wrapLineInto((String) iter.next(), strings, fm, maxWidth); 
    return strings; 
    } 

    /** 
    * Given a line of text and font metrics information, wrap the line and add 
    * the new line(s) to <var>list</var>. 
    * 
    * @param line 
    *   a line of text 
    * @param list 
    *   an output list of strings 
    * @param fm 
    *   font metrics 
    * @param maxWidth 
    *   maximum width of the line(s) 
    */ 
    public static void wrapLineInto(String line, List list, FontMetrics fm, int maxWidth) { 
    int len = line.length(); 
    int width; 
    while (len > 0 && (width = fm.stringWidth(line)) > maxWidth) { 
     // Guess where to split the line. Look for the next space before 
     // or after the guess. 
     int guess = len * maxWidth/width; 
     String before = line.substring(0, guess).trim(); 

     width = fm.stringWidth(before); 
     int pos; 
     if (width > maxWidth) // Too long 
     pos = findBreakBefore(line, guess); 
     else { // Too short or possibly just right 
     pos = findBreakAfter(line, guess); 
     if (pos != -1) { // Make sure this doesn't make us too long 
      before = line.substring(0, pos).trim(); 
      if (fm.stringWidth(before) > maxWidth) 
      pos = findBreakBefore(line, guess); 
     } 
     } 
     if (pos == -1) 
     pos = guess; // Split in the middle of the word 

     list.add(line.substring(0, pos).trim()); 
     line = line.substring(pos).trim(); 
     len = line.length(); 
    } 
    if (len > 0) 
     list.add(line); 
    } 

    /** 
    * Returns the index of the first whitespace character or '-' in <var>line</var> 
    * that is at or before <var>start</var>. Returns -1 if no such character is 
    * found. 
    * 
    * @param line 
    *   a string 
    * @param start 
    *   where to star looking 
    */ 
    public static int findBreakBefore(String line, int start) { 
    for (int i = start; i >= 0; --i) { 
     char c = line.charAt(i); 
     if (Character.isWhitespace(c) || c == '-') 
     return i; 
    } 
    return -1; 
    } 

    /** 
    * Returns the index of the first whitespace character or '-' in <var>line</var> 
    * that is at or after <var>start</var>. Returns -1 if no such character is 
    * found. 
    * 
    * @param line 
    *   a string 
    * @param start 
    *   where to star looking 
    */ 
    public static int findBreakAfter(String line, int start) { 
    int len = line.length(); 
    for (int i = start; i < len; ++i) { 
     char c = line.charAt(i); 
     if (Character.isWhitespace(c) || c == '-') 
     return i; 
    } 
    return -1; 
    } 
    /** 
    * Returns an array of strings, one for each line in the string. Lines end 
    * with any of cr, lf, or cr lf. A line ending at the end of the string will 
    * not output a further, empty string. 
    * <p> 
    * This code assumes <var>str</var> is not <code>null</code>. 
    * 
    * @param str 
    *   the string to split 
    * @return a non-empty list of strings 
    */ 
    public static List splitIntoLines(String str) { 
    ArrayList strings = new ArrayList(); 

    int len = str.length(); 
    if (len == 0) { 
     strings.add(""); 
     return strings; 
    } 

    int lineStart = 0; 

    for (int i = 0; i < len; ++i) { 
     char c = str.charAt(i); 
     if (c == '\r') { 
     int newlineLength = 1; 
     if ((i + 1) < len && str.charAt(i + 1) == '\n') 
      newlineLength = 2; 
     strings.add(str.substring(lineStart, i)); 
     lineStart = i + newlineLength; 
     if (newlineLength == 2) // skip \n next time through loop 
      ++i; 
     } else if (c == '\n') { 
     strings.add(str.substring(lineStart, i)); 
     lineStart = i + 1; 
     } 
    } 
    if (lineStart < len) 
     strings.add(str.substring(lineStart)); 

    return strings; 
    } 

} 

आप अपने ही वर्ग में रखते हैं, तो बस का उपयोग कर आप क्या था:

FontMetrics fm= graphics.getFontMetrics(font); 
Rectangle2D rect=fm.getStringBounds("Some Text",graphics); 

कॉल wrap(String str, FontMetrics fm, int maxWidth) जो ListString एस को वापस ले जाएगा जो acco लपेटा गया है

String text="Some Text"; 
FontMetrics fm= graphics.getFontMetrics(font); 
Rectangle2D rect=fm.getStringBounds(text,graphics); 
List<String> textList=StringUtils.wrap(text, fm, int maxWidth); 

संदर्भ::

3

this answer में LabelRenderTest.java स्रोत देखें अपने maxWidth को rdingly Rectangle2D की चौड़ाई पाठ में रखा जाएगा रहेगा। यह एचटीएमएल/सीएसएस का उपयोग करता है, सीएसएस का उपयोग करके शरीर की चौड़ाई सेट के साथ, और इस प्रकार लाइन रैप स्वचालित बनाता है।

+0

+1 अच्छी तकनीक मुझे कहना चाहिए –

+0

@ डेविड अन्य लोग आपके साथ असहमत होंगे, यह बताते हुए कि HTML स्वरूपण का उपयोग करके हम लेआउट के कुछ लाभ खो देते हैं (लाइन के साथ कुछ करने के लिए .. एएफएआईआर ड्रॉप) और एचटीएमएल के लिए जे 2 एसई समर्थन 3.2 तक सीमित है, और सीएसएस समर्थन अपेक्षाकृत कमजोर और अनियंत्रित है। ओटीओएच, टेक्स्ट को स्वचालित रूप से लपेटने के लिए यह एक त्वरित 'गलत' तरीका हो सकता है। –

5

मैं एक छोटे से समारोह में मदद कर सकते लिखा था। 447 चौड़ाई उपलब्ध है जिसे आप अपनी आवश्यक चौड़ाई से पाठ प्रस्तुत करने के लिए प्राप्त कर सकते हैं।

private void drawTextUgly(String text, FontMetrics textMetrics, Graphics2D g2) 
{ 
    // Ugly code to wrap text 
    int lineHeight = textMetrics.getHeight(); 
    String textToDraw = text; 
    String[] arr = textToDraw.split(" "); 
    int nIndex = 0; 
    int startX = 319; 
    int startY = 113; 
    while (nIndex < arr.length) 
    { 
     String line = arr[nIndex++]; 
     while ((nIndex < arr.length) && (textMetrics.stringWidth(line + " " + arr[nIndex]) < 447)) 
     { 
      line = line + " " + arr[nIndex]; 
      nIndex++; 
     } 
     GraphicsUtility.drawString(g2, line, startX, startY); 
     startY = startY + lineHeight; 
    } 
} 
1
private List<String> wrap(String txt, FontMetrics fm, int maxWidth){ 
    StringTokenizer st = new StringTokenizer(txt) ; 

    List<String> list = new ArrayList<String>(); 
    String line = ""; 
    String lineBeforeAppend = ""; 
    while (st.hasMoreTokens()){ 
     String seg = st.nextToken(); 
     lineBeforeAppend = line; 
     line += seg + " "; 
     int width = fm.stringWidth(line); 
     if(width < maxWidth){ 
      continue; 
     }else { //new Line. 
      list.add(lineBeforeAppend); 
      line = seg + " "; 
     } 
    } 
    //the remaining part. 
    if(line.length() > 0){ 
     list.add(line); 
    } 
    return list; 
}