Graphics2D
类扩展了
Graphics
类,提供了对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制。它是用于在 Java(tm) 平台上呈现二维形状、文本和图像的基础类。
下面通过修改画笔的属性来实现文本以一个点为圆心的旋转,不多说看运行后的效果吧。
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- /**
- *
- * @author huangxf
- */
- public class Rotate {
- /**
- * @param args
- */
- public static void main(String[] args) {
- JFrame jf = new JFrame();
- jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- jf.getContentPane().add(new RotatePanel());
- jf.setPreferredSize(new Dimension(500, 400));
- jf.pack();
- jf.setVisible(true);
- }
- }
- class RotatePanel extends JPanel {
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2d = (Graphics2D) g;
- g2d.setColor(Color.WHITE);
- g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
- String s = "Java 2d 旋转";
- Font f = new Font("宋体", Font.BOLD, 16);
- Color[] colors = {Color.ORANGE, Color.LIGHT_GRAY};
- g2d.setFont(f);
- // 平移原点到图形环境的中心
- g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
- // 旋转文本
- for (int i = 0; i < 12; i++) {
- g2d.rotate(30 * Math.PI / 180);
- g2d.setPaint(colors[i % 2]);
- g2d.drawString(s, 0, 0);
- }
- }
- }