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?

Ben
  • 6,321
  • 9
  • 40
  • 76

1 Answers1

1

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();
        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366