TextLayoutJustify.java Source code

Java tutorial

Introduction

Here is the source code for TextLayoutJustify.java

Source

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.util.StringTokenizer;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TextLayoutJustify extends JPanel {
    Dimension d;

    Font f = new Font("fontname", Font.PLAIN, 20);

    FontMetrics fm;

    int fh, ascent;

    int space;

    public static void main(String[] a) {
        JFrame f = new JFrame();
        f.setSize(300, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new TextLayoutJustify());
        f.setVisible(true);
    }

    public void paint(Graphics g) {
        d = getSize();
        g.setFont(f);
        if (fm == null) {
            fm = g.getFontMetrics();
            ascent = fm.getAscent();
            fh = ascent + fm.getDescent();
            space = fm.stringWidth(" ");
        }
        g.setColor(Color.black);
        StringTokenizer st = new StringTokenizer(
                "this is a text. this is a test <BR> this is a text. this is a test");
        int x = 0;
        int nextx;
        int y = 0;
        String word, sp;
        int wordCount = 0;
        String line = "";
        while (st.hasMoreTokens()) {
            word = st.nextToken();
            if (word.equals("<BR>")) {
                drawString(g, line, wordCount, fm.stringWidth(line), y + ascent);
                line = "";
                wordCount = 0;
                x = 0;
                y = y + (fh * 2);
            } else {
                int w = fm.stringWidth(word);
                if ((nextx = (x + space + w)) > d.width) {
                    drawString(g, line, wordCount, fm.stringWidth(line), y + ascent);
                    line = "";
                    wordCount = 0;
                    x = 0;
                    y = y + fh;
                }
                if (x != 0) {
                    sp = " ";
                } else {
                    sp = "";
                }
                line = line + sp + word;
                x = x + space + w;
                wordCount++;
            }
        }
        drawString(g, line, wordCount, fm.stringWidth(line), y + ascent);
    }

    public void drawString(Graphics g, String line, int wc, int lineWidth, int y) {
        if (lineWidth < (int) (d.width * .75)) {
            g.drawString(line, 0, y);
        } else {
            int toFill = (int) ((d.width - lineWidth) / wc);
            int nudge = d.width - lineWidth - (toFill * wc);
            StringTokenizer st = new StringTokenizer(line);
            int x = 0;
            while (st.hasMoreTokens()) {
                String word = st.nextToken();
                g.drawString(word, x, y);
                if (nudge > 0) {
                    x = x + fm.stringWidth(word) + space + toFill + 1;
                    nudge--;
                } else {
                    x = x + fm.stringWidth(word) + space + toFill;
                }
            }
        }
    }
}