java 文字旋转
请问各位大虾,有没有什么方法能够让显示的字体旋转呢?
我想让文字不是水平显示,而是与水平线呈一定角度?比如垂直显示 ,谢谢
[解决办法]
用Graphics画图的时候旋转一下坐标?
参考http://docs.oracle.com/javase/tutorial/2d/index.html
[解决办法]
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RotateTextDemo extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private int x = 200;
private int y = 200;
public RotateTextDemo() {
getContentPane().setLayout(new BorderLayout());
JPanel testPanel = new JPanel(){
/**
*
*/
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g) {
// super.paintComponent(g);
g.setColor(Color.RED);
g.drawOval(x, y, 20, 20);
//
Graphics2D g2d = (Graphics2D)g;
String text = "Hello Java";
Rectangle2D r = g2d.getFontMetrics().getStringBounds(text, g2d);
g2d.translate(r.getX(), r.getY());
g2d.rotate(-Math.PI);
g2d.translate(-r.getCenterX(), -r.getCenterY());
g2d.drawString(text, -100, -100);
}
};
getContentPane().add(testPanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
RotateTextDemo frame = new RotateTextDemo();
frame.setSize(600, 600);
//frame.pack();
// frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}