gpt4 book ai didi

Java图形角度文本

转载 作者:行者123 更新时间:2023-12-02 05:58:48 25 4
gpt4 key购买 nike

如何使字符串旋转。 (但不是不断旋转,只是以一定角度旋转)?我尝试使用 Graphics2D 但找不到旋转的方法。或者需要手动旋转它?另外,如果更容易,我可以使用 LWJGL,但我目前对该库没有经验。谢谢。

最佳答案

有多种方法可以实现这一目标。

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

Rotated

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

}

}

更多详情,您可以查看Transforming Shapes, Text, and Images

关于Java图形角度文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22851751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com