0

I have this string: "This is my very long String that wont fit on one line" And I need to split it into multiple lines so it'll fit where I need it.

Lets say theres only room for about 15 letters per line, then it should look like this:

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

I would like to split it into a List<String> so I can do

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

Any help on how to do this would be appriciated!

The way I'm rendering the text is with Graphics.drawString()

This is what I've tried so far (horrible, I know)

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;
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Fuze
  • 423
  • 1
  • 6
  • 11
  • please post whatever you have tried – Bhushan Jan 25 '12 at 16:02
  • 2
    what about words longer than 15 characters? – Matten Jan 25 '12 at 16:04
  • 1
    Just for curiosity, how are you displaying this string? I mean Console, Swing, html etc. – everton Jan 25 '12 at 16:06
  • 1
    the GUI framework you're using most likely already have such multiline text widget, use the provided one since splitting text are much more difficult than counting the number of letters; you'll also have to take into account that in most fonts M is much wider than i. – Lie Ryan Jan 25 '12 at 16:08
  • @Bhushan I added it to the OP EvertonAgner: I'm using Graphics.drawString() LieRyan: I can't use any widgets, only the stuff that the Graphics class can do – Fuze Jan 25 '12 at 16:13
  • Check this similar question: http://stackoverflow.com/questions/400566/full-justification-with-a-java-graphics-drawstring-replacement – everton Jan 25 '12 at 16:16

2 Answers2

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);
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

You could consider using LineBreakMeasurer which is intended for this sort of thing in a graphics environment. Please check out the JavaDocs here, which contain a couple of detailed examples on how to use it.

ivantod
  • 773
  • 3
  • 8