Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9154429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:25:34+00:00 2026-06-17T12:25:34+00:00

My question is not about how to rotate text with Java2D; I know how

  • 0

My question is not about how to rotate text with Java2D; I know how to do that. What I don’t know is how to make the rotated text “look good.” For example, if you create a text box in PowerPoint and rotate it, the text appears sharp and clear no matter the rotation angle. However, text drawn with g2D.drawString() looks okay at 0 or 90 degrees but not so good at other angles. Is there a way to manipulate the text to clean or sharpen it up? If so, then if someone could point me to where look to learn how to do this I would be so thankful.

Below is a little program that illustrates what I’m talking about. The bigger font isn’t too bad when rotated but still doesn’t look very professional. The smaller font when rotated 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();
            }
        });
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:25:35+00:00Added an answer on June 17, 2026 at 12:25 pm

    I once had a similar problem, and solved it by drawing the text with high precision to an image, then drawing the rotated image.

    Here’s 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();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is not about how to use Serializable , I already know that.
(note that this question is not about CAS, it's about the May fail spuriously
This question is not about caching. I need this in-memory table for many small
My question is not about how to solve this error(I already solved it) but
This question is not about which is the best, it is about which makes
This question is not about 'best' barcode library recommendation, we use various products on
My question is not about a specific code snippet but more general, so please
For starters, this question is not so much about programming in the NetBeans IDE
This is not a question about a specific framework. I am using plain php
This is not a question about jQuery, but about how jQuery implements such a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.