0

你怎么做才能让一个字符串被旋转。(但不是不断旋转,只是在一个角度)?我尝试使用Graphics2D但找不到旋转的方法。还是需要手动旋转它?此外,如果它更容易我可以使用 LWJGL,但我目前对该库没有经验。谢谢。

4

1 回答 1

2

您可以通过多种方式实现这一目标。

此示例仅使用 anAffineTransform来更改图形的绘制方式...

旋转

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class AngleText {

    public static void main(String[] args) {
        new AngleText();
    }

    public AngleText() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            String text = "I don't see the problem";
            FontMetrics fm = g2d.getFontMetrics();
            int x = (getWidth() - fm.stringWidth(text)) / 2;
            int y = ((getHeight() - fm.getHeight()) / 2) + fm.getDescent();
            g2d.setTransform(AffineTransform.getRotateInstance(Math.toRadians(45), getWidth() / 2, getHeight() / 2));
            g2d.drawString(text, x, y);
            g2d.dispose();
        }

    }

}

有关更多详细信息,您可以查看变换形状、文本和图像

于 2014-04-04T01:16:01.783 回答