2012-01-25 10 views
0

「これは私の非常に長い文字列で、1行に収まらない」 そして、それを複数の行に分割して、必要な場所に合わせる必要があります。長すぎる文字列を<String>に分割する

、それは次のようになります、行あたり約15文字のためにtheresの部屋だけを言うことができます:私は

for(String s : lines) draw(s,x,y); 

を行うことができます

"This is my very" 
"long String" 
"that wont fit" 
"on one line" 

を私はList<String>に分割したいですこれを行う方法についての助けがあれば、謝罪されるでしょう!

私はテキストをレンダリングしています方法は(私が知っている恐ろしい)これは私がこれまで試したものです

Graphics.drawStringを()である

String diaText = "This is my very long String that wont fit on one line"; 
String[] txt = diaText.trim().split(" "); 
int max = 23; 
List<String> lines = new ArrayList<String>(); 
String s1 = ""; 
if (!(diaText.length() > max)) { 
    lines.add(diaText); 
} else { 
    for (int i = 0; i < txt.length; i++) { 
     String ns = s1 += txt[i] + " "; 
     if (ns.length() < 23) { 
      lines.add(s1); 
      s1 = ""; 
     } else { 
      s1 += txt[i] + ""; 
     } 
    } 
} 
int yo = 0; 
for (String s : lines) { 
    Font.draw(s, screen, 70, 15 + yo, Color.get(-1, 555, 555,555)); 
    yo += 10; 
} 
+0

あなたが試したものを投稿してください – Bhushan

+2

15文字より長い単語はどうですか? – Matten

+1

好奇心のために、どうやってこの文字列を表示していますか?私はコンソール、スイング、htmlなどを意味します。 – everton

答えて

1

Label rendered on image

import java.awt.*; 
import java.awt.image.BufferedImage; 
import javax.swing.*; 

public class LabelRenderTest { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 

      String title = "<html><body style='width: 200px; padding: 5px;'>" 
       + "<h1>Do U C Me?</h1>" 
       + "Here is a long string that will wrap. " 
       + "The effect we want is a multi-line label."; 

       JFrame f = new JFrame("Label Render Test"); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       BufferedImage image = new BufferedImage(
        400, 
        300, 
        BufferedImage.TYPE_INT_RGB); 
       Graphics2D imageGraphics = image.createGraphics(); 
       GradientPaint gp = new GradientPaint(
        20f, 
        20f, 
        Color.red, 
        380f, 
        280f, 
        Color.orange); 
       imageGraphics.setPaint(gp); 
       imageGraphics.fillRect(0, 0, 400, 300); 

       JLabel textLabel = new JLabel(title); 
       textLabel.setSize(textLabel.getPreferredSize()); 

       Dimension d = textLabel.getPreferredSize(); 
       BufferedImage bi = new BufferedImage(
        d.width, 
        d.height, 
        BufferedImage.TYPE_INT_ARGB); 
       Graphics g = bi.createGraphics(); 
       g.setColor(new Color(255, 255, 255, 128)); 
       g.fillRoundRect(
        0, 
        0, 
        bi.getWidth(f), 
        bi.getHeight(f), 
        15, 
        10); 
       g.setColor(Color.black); 
       textLabel.paint(g); 
       Graphics g2 = image.getGraphics(); 
       g2.drawImage(bi, 20, 20, f); 

       ImageIcon ii = new ImageIcon(image); 
       JLabel imageLabel = new JLabel(ii); 

       f.getContentPane().add(imageLabel); 
       f.pack(); 
       f.setLocationByPlatform(true); 

       f.setVisible(true); 
      } 
     }); 
    } 
} 
0

LineBreakMeasurerの使用を検討することができます。これはグラフィックス環境でこのようなことを意図しています。 JavaDocs hereを参照してください。使用方法の詳細な例がいくつか含まれています。

関連する問題