日期:2014-05-20 浏览次数:20973 次
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);
}
}