2012-01-25 2 views
0

다음 문자열이 있습니다. "이것은 매우 긴 문자열로 한 줄에 들어 가지 않습니다." 그리고 필자는 그것을 여러 줄로 나눠서 필요에 따라 맞춰야합니다. 너무 길면 <String>을 목록으로 나눕니다.

는 프로그래머에게 한 줄에 15 글자 만 방을 말할 수 있습니다, 그것은 다음과 같아야합니다

"This is my very" 
"long String" 
"that wont fit" 
"on one line" 
나는 List<String>으로 분할하고 싶은

그래서 내가 할 수있는

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

이 작업을 수행하는 방법에 대한 도움이 있으면 도움이 될 것입니다.

내가 텍스트를 렌더링 해요 방법은 (내가 아는 끔찍한) 이것은 내가 지금까지 시도한 것입니다

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

호기심 때문에이 문자열을 어떻게 표시하고 있습니까? 나는 Console, Swing, 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을 사용하십시오. 여기에는 사용법에 대한 몇 가지 자세한 예제가 있습니다.

관련 문제