How to make rotating text look good with Java2D

My question is not how to rotate text using Java2D; I know how to do it. I don’t know how to make the rotated text “look good”. For example, if you create a text field in PowerPoint and rotate it, the text becomes clear and transparent regardless of the angle of rotation. However, text drawn with help g2D.drawString()looks good at 0 or 90 degrees, but not very good at other angles. Is there a way to manipulate text to clear or sharpen it? If so, then if someone can tell me where to look, how to do it, I would be so grateful.

Below is a small program that illustrates what I'm talking about. A larger font is not so bad when turning, but still does not look very professional. A smaller font when turning is terrible.

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

public class RotateTest extends JPanel {
    String message = "How does this text look?";

    public RotateTest() {
        this.setPreferredSize(new Dimension(640, 280));
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2D = (Graphics2D) g;

        g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                       RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        g2D.setFont(new Font("MyriadPro", Font.BOLD, 20));
        g2D.drawString(message, 80, 20);

        AffineTransform orig = g2D.getTransform();

        double angle = Math.toRadians(7.0);
        g2D.rotate(-angle, -10, 80);
        g2D.drawString(message, 80, 80);
        g2D.setTransform(orig);

        angle = Math.toRadians(30.0);
        g2D.rotate(-angle, -40, 80);
        g2D.drawString(message, 60, 260);
        g2D.setTransform(orig);

        g2D.setFont(new Font("MyriadPro", Font.BOLD, 12));
        g2D.drawString(message, 380, 20);

        angle = Math.toRadians(7.0);
        g2D.rotate(-angle, -10, 80);
        g2D.drawString(message, 380, 120);
        g2D.setTransform(orig);

        angle = Math.toRadians(30.0);
        g2D.rotate(-angle, -40, 80);
        g2D.drawString(message, 320, 400);
        g2D.setTransform(orig);
    }

    private void display() {
        JFrame f = new JFrame("RotateTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new RotateTest().display();
            }
        });
    }
}
+3
source share
2 answers

I once had a similar problem, and it was solved by drawing text with high fidelity, and then drawing a rotated image.

Here is the code:

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;

import javax.swing.*;

public class RotatedText extends JPanel {
    String message = "How does this text look?";

    public RotatedText() {
        this.setPreferredSize(new Dimension(640, 280));
    }

    public BufferedImage createStringImage(Graphics g, String s) {
        int w = g.getFontMetrics().stringWidth(s) + 5;
        int h = g.getFontMetrics().getHeight();

        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D imageGraphics = image.createGraphics();
        imageGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        imageGraphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        imageGraphics.setColor(Color.BLACK);
        imageGraphics.setFont(g.getFont());
        imageGraphics.drawString(s, 0, h - g.getFontMetrics().getDescent());
        imageGraphics.dispose();

        return image;
    }

    private void drawString(Graphics g, String s, int tx, int ty, double theta, double rotx, double roty) {
        AffineTransform aff = AffineTransform.getRotateInstance(theta, rotx, roty);
        aff.translate(tx, ty);

        Graphics2D g2D = ((Graphics2D) g);
        g2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2D.drawImage(createStringImage(g, s), aff, this);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setFont(new Font("MyriadPro", Font.BOLD, 20));

        drawString(g, message, 80, 20, 0, 0, 0);
        drawString(g, message, 80, 80, -Math.toRadians(7.0), -10, 80);
        drawString(g, message, 60, 260, -Math.toRadians(30.0), -40, 80);

        g.setFont(new Font("MyriadPro", Font.BOLD, 12));

        drawString(g, message, 380, 20, 0, 0, 0);
        drawString(g, message, 380, 120, -Math.toRadians(7.0), -10, 80);
        drawString(g, message, 320, 400, -Math.toRadians(30.0), -40, 80);
    }

    private void display() {
        JFrame f = new JFrame("RotateTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new RotatedText().display();
            }
        });
    }
}
+6
source

I don't have time to check this out, but will the following code help ?:

Graphics2D g2d; 
g2d.setRenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

This will only work with Graphics2D. If you use regular graphics, you can apply your Graphics object to the 2D version as follows:

Graphics2D g2d = (Graphics2D) g; //if Graphics object name is g.

Let me know! Good luck.

0
source

All Articles