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

  • SEARCH
  • Home
  • 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 9307297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:21:48+00:00 2026-06-19T00:21:48+00:00

Here is the problem I am currently facing: I want to draw a String

  • 0

Here is the problem I am currently facing: I want to draw a String on a JPanel using Java2D. The String has to be rotated of a user-defined angle.

Under that String, I also paint the background in a given color to facilitate reading (plenty of other things are drawn on my JPanel).

What I did, in the overridden paint method of my JPanel, is the following:

final Graphics2D g2 = (Graphics2D) g.create();

final int textWidth = g.getFontMetrics().stringWidth(textToDraw);
final int textHeight = g.getFontMetrics().getHeight();

g2.translate(pointToDraw.x, pointToDraw.y);
g2.rotate(angle);

g2.setColor(textBackground);
g2.fillRect(deltaX, -textHeight, textWidth, textHeight);

g2.setColor(drawColor);
g2.setFont(font);
g2.drawString(textToDraw, deltaX, deltaY);
g2.dispose();

This works very well on linux, but on Mac OS X (with Java 1.6), the text is not displayed properly: the text is correctly rotated but after each character, there is a line break.

How can I make it work on both platforms?

  • 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-19T00:21:50+00:00Added an answer on June 19, 2026 at 12:21 am

    I don’t think this is the solution you will want, but from everything I’ve been able to read, there doesn’t seem to be a better solution…

    The problem seems to be that the Mac is rotating each character, not just the String

    enter image description here

    Basically, I’ve cheated. This renders the text to a BufferedImage (you should create the image only when the properties change, unlike me, which I’ve done it within the paint method) and then rotates the image…

    public class RotateText {
    
        public static void main(String[] args) {
            new RotateText();
        }
    
        public RotateText() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private String textToDraw = "Stack Overflow";
            private double angle = 90;
            private Color drawColor = Color.BLACK;
    
            public TestPane() {
                Timer timer = new Timer(50, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        angle += 2;
                        repaint();
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                final Graphics2D g2 = (Graphics2D) g.create();
    
                FontMetrics fm = g2.getFontMetrics();
    
                int textWidth = fm.stringWidth(textToDraw);
                int textHeight = fm.getHeight();
    
                BufferedImage img = new BufferedImage(textWidth, textHeight, BufferedImage.TYPE_INT_ARGB);
                Graphics2D ig = img.createGraphics();
                ig.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                ig.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                ig.setColor(drawColor);
                ig.drawString(textToDraw, 0, fm.getAscent());
                ig.dispose();
    
                int x = (getWidth() - textWidth) / 2;
                int y = (getHeight() - textHeight) / 2;
    
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setTransform(AffineTransform.getRotateInstance(Math.toRadians(angle), getWidth() / 2, getHeight() / 2));
                g2.drawImage(img, x, y, this);
                g2.dispose();
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So here's my problem. I'm currently working on a PhoneGap application using jQuery Mobile
currently i'm facing the problem with nested linq. here is my tables and now
Here's my problem. I'm currently trying to develop a .Net Compact Framework 2.0 application
so here is the problem. I am currently creating an Android app that is
date here my problem: String datetime = 2012-03-24 23:20:51; I know that that string
I am currently facing a very disturbing problem: interface IStateSpace<Position, Value> where Position :
I want to make a public transportation application for Android. First problem I'm facing
Currently I am facing the following problem, which I'm working in Stata to solve.
Well i am not using email verification so i am facing problem in checking
I am facing a problem in ios6 regarding dismissal of modalViewController Here is code

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.