0

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

在该字符串下,我还以给定的颜色绘制背景以方便阅读(我的 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)上,文本显示不正确:文本正确旋转,但在每个字符之后,有一个换行符。

我怎样才能让它在两个平台上工作?

4

1 回答 1

1

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

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

在此处输入图像描述

基本上,我已经作弊了。这会将文本呈现为BufferedImage(您应该仅在属性更改时创建图像,不像我,我已经在paint方法中完成了它)然后旋转图像......

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

    }

}
于 2013-02-08T10:26:56.973 回答