5

This is exactly what I'm trying to do:

Graphics2D g2 = (Graphics2D) g;
g2.setFont(new Font("Serif", Font.PLAIN, 5));
g2.setPaint(Color.black);
g2.drawString("Line 1\nLine 2", x, y);

That line prints like this:

Line1Line2

I want it like this:

Line1
Line2

How can I do this in drawString ?

As well as how can I do a tab space for a line??

ArMEd
  • 123
  • 1
  • 3
  • 11

2 Answers2

6





       private void drawString(Graphics g, String text, int x, int y) {
                for (String line : text.split("\n"))
                    g.drawString(line, x, y += g.getFontMetrics().getHeight());
            }

       private void drawtabString(Graphics g, String text, int x, int y) {
            for (String line : text.split("\t"))
                g.drawString(line, x += g.getFontMetrics().getHeight(), y);
        }


            Graphics2D g2 = (Graphics2D) g;
            g2.setFont(new Font("Serif", Font.PLAIN, 5));
            g2.setPaint(Color.black);
            drawString(g2,"Line 1\nLine 2", 120, 120);
            drawtabString(g2,"Line 1\tLine 2", 130, 130);

 
Rupok
  • 2,024
  • 15
  • 16
  • As well as how can I do a tab space for this?? – ArMEd Dec 30 '11 at 07:05
  • http://www.java2s.com/Tutorial/Java/0300__SWT-2D-Graphics/Drawstringanddisplaynewlinesandtabsasnonprintablecharacters.htm – Rupok Dec 30 '11 at 07:09
  • 2
    private void drawtabString(Graphics g, String text, int x, int y) { for (String line : text.split("\t")) g.drawString(line, x += g.getFontMetrics().getHeight(), y); } – Rupok Dec 30 '11 at 08:04
  • Can you please tell me how to combine those to codes. (Line breaker and tab space) – ArMEd Dec 30 '11 at 08:38
  • Working on something that may utilize your solution, what does `String text` represent here? – Sam Mitchell Nov 29 '15 at 19:52
0

Here's a snippet I used to draw text in a JPanel with tab expansion and multiple lines:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class Scratch {
    public static void main(String argv[]) {
        JFrame frame = new JFrame("FrameDemo");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel() {
            @Override
            public void paint(Graphics graphics) {
                graphics.drawRect(100, 100, 1, 1);
                String message =
                        "abc\tdef\n" +
                        "abcx\tdef\tghi\n" +
                        "xxxxxxxxdef\n" +
                        "xxxxxxxxxxxxxxxxghi\n";
                int x = 100;
                int y = 100;
                FontMetrics fontMetrics = graphics.getFontMetrics();
                Rectangle2D tabBounds = fontMetrics.getStringBounds(
                        "xxxxxxxx",
                        graphics);
                int tabWidth = (int)tabBounds.getWidth();
                String[] lines = message.split("\n");
                for (String line : lines) {
                    int xColumn = x;
                    String[] columns = line.split("\t");
                    for (String column : columns) {
                        if (xColumn != x) {
                            // Align to tab stop.
                            xColumn += tabWidth - (xColumn-x) % tabWidth;
                        }
                        Rectangle2D columnBounds = fontMetrics.getStringBounds(
                                column,
                                graphics);
                        graphics.drawString(
                                column,
                                xColumn,
                                y + fontMetrics.getAscent());
                        xColumn += columnBounds.getWidth();
                    }
                    y += fontMetrics.getHeight();
                }
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 200);
            }
        };
        frame.getContentPane().add(panel, BorderLayout.CENTER);

        frame.pack();

        frame.setVisible(true);    }
}

It really seemed like Utilities.drawTabbedText() was promising, but I couldn't figure out what it needed as input.

Don Kirkby
  • 41,771
  • 21
  • 173
  • 252