在Mac上的Java2D中旋转文本

时间:2013-02-08 07:40:45

标签: java text rotation graphics2d java-2d

这是我目前面临的问题:我想使用Java2D在JPanel上绘制一个String。 String必须以用户定义的角度旋转。

在该字符串下,我还以给定颜色绘制背景以方便阅读(我的JPanel上绘制了大量其他内容)。

我所做的,在我的JPanel的重写绘画方法中,如下:

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

这在linux上非常有效,但在Mac OS X上(使用Java 1.6),文本显示不正确:文本正确旋转但在每个字符后都有换行符。

如何在两个平台上都能运行?

1 个答案:

答案 0 :(得分:1)

我认为这不是你想要的解决方案,但是从我能够阅读的所有内容来看,似乎没有更好的解决方案......

问题似乎是Mac正在旋转每个角色,而不仅仅是String

enter image description here

基本上,我被骗了。这会将文本呈现为BufferedImage(您应该仅在属性更改时创建图像,与我不同,我已在paint方法中完成)然后旋转图像... < / p>

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

    }

}