Graphics2D类怎么是一串文字围绕圆心展示

Yaphets_VJ 2015-06-11 02:20:34
Graphics2D类怎么是一串文字围绕圆心展示,有人知道么 。。

...全文
203 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
三仙半 2015-06-12
  • 打赏
  • 举报
回复
顶1楼,学习了。 2楼,我和1楼想法一样,他就是想把一些文字沿着圆周显示,估计是把“使”打成“是”了。
alan19931103 2015-06-12
  • 打赏
  • 举报
回复
不懂你要问什么
O_森_O 2015-06-11
  • 打赏
  • 举报
回复 1

package chunsen.test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/** Created by chunsen on 15-6-11.*/

public class OrvalText extends JComponent{
    private String text;
    private int radius;
    private double startAngle =0;
    public OrvalText(String text){
        this.text = text;
        radius = 100;
    }

    public void setRadius(int radius) {
        if(this.radius != radius) {
            this.radius = radius;
            repaint();
        }
    }

    public void setStartAngle(double startAngle) {
        if(this.startAngle != startAngle) {
            this.startAngle = startAngle;
            repaint();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        Dimension d = getSize();
        Rectangle rec = new Rectangle(d);
        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(getBackground());
        g2d.fill(rec);

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        g2d.setColor(getForeground());
        g2d.setFont(getFont());

        g2d.translate(rec.getCenterX(), rec.getCenterY());
        g2d.translate(0, -radius);
        g2d.rotate(Math.PI/2- startAngle,0,radius);
        int length = text.length();
        for(int i= 0;i<length;i++){
            g2d.drawString(text.substring(i,i+1),0,0);
            g2d.rotate(0.2,0,radius);
        }
    }

    public static void main(String[] args){
        JFrame frame =new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final  OrvalText text = new OrvalText("This is a test text");
        text.setFont(new Font("Dialog", Font.BOLD, 12));
        text.setForeground(Color.RED);
        frame.setLayout(new BorderLayout());
        frame.add(text, BorderLayout.CENTER);

        frame.setBounds(200, 200, 500, 500);
        frame.setVisible(true);

        JPanel setting = new JPanel(new FlowLayout(FlowLayout.LEADING));
        final JTextField radius = new JTextField("100",8);
        final JTextField startAngle = new JTextField("0",8);

        setting.add(new JLabel("Radius:"));
        setting.add(radius);
        setting.add(new JLabel("  Start Angle:"));
        setting.add(startAngle);
        JButton ok = new JButton("Apply");
        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                double r = Double.valueOf(radius.getText().trim());
                double a = Double.valueOf(startAngle.getText().trim());
                text.setRadius((int)r);
                text.setStartAngle(a);
            }
        });
        setting.add(ok);

        frame.add(setting,BorderLayout.SOUTH);
    }
}
demo 奉上,忘好评

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧