1. [代码]CircleTextDemo.java     
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
 
/**
 * A demo class that illustrates drawing text
 * along the outside of a circle.
 */
public class CircleTextDemo extends Canvas {
    Frame myframe;
    TextField text;
    Button printBtn;
    Font myfont;
    Color textcolor;
    Color circlecolor;
    
    /**
     * Create a CircleTextDemo canvas and frame
     * with default settings.
     */
    public CircleTextDemo() {
        this("Serif", Font.PLAIN, 18, Color.pink, Color.black);
    }
 
    /**
     * Create a CircleTextDemo canvas and frame
     * with supplied settings.
     *
     * @param ff Font family (usually "Serif")
     * @param fs Font style (usually Font.PLAIN)
     * @param fz Font size (usually 18)
     * @param bg Background color for this canvas (usually pink)
     * @param fg Foreground color for text (usually black)
     */
    public CircleTextDemo(String ff, int fs, int fz, Color bg, Color fg) {
        setBackground(bg);
        circlecolor = bg.brighter();
        textcolor = fg;
        myfont = new Font(ff, fs, fz);
 
        text = new TextField("Text on a circle using Java 2D Graphics!");
        myframe = new Frame("CircleTextDemo");
   printBtn = new Button("Print");
        myframe.add(text, BorderLayout.NORTH);
        myframe.add(this, BorderLayout.CENTER);
   myframe.add(printBtn, BorderLayout.SOUTH);
        myframe.setSize(new Dimension(300,340));
        myframe.setLocation(150,140);
        myframe.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent we) {
                    System.exit(0);
                }
            });
        text.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    repaint();
                }
            });
   printBtn.addActionListener(new FramePrinter(myframe));
        myframe.setVisible(true);
    }
        
    /**
     * Paint the contents of the CircleDemoText canvas.
     *
     * @param g - a Graphics, hopefully a Graphics2D
     */美女
    public void paint(Graphics g) {
        String st = text.getText();
        if (st.length() == 0) return;
        if (g instanceof Graphics2D) {
            Dimension cd = getSize();
            Point pt = new Point(cd.width / 2, cd.height / 2);
            int radius = (int)(pt.x * 0.84);
            g.setColor(circlecolor);
            g.drawArc(pt.x - radius, pt.y - radius,
                      radius*2-1, radius*2-1,
                      0, 360);
            g.setColor(textcolor);
            g.setFont(myfont);
            drawCircleText((Graphics2D)g, st, pt, radius, -Math.PI/2, 1.0);
        }
        else {
            System.out.println("Cannot draw curved text without a Graphics2D");
        }
    }
        
    /**
     * Draw a piece of text on a circular curve, one
     * character at a time.  This is harder than it looks...
     *
     * This method accepts many arguments:
     *   g - a Graphics2D ready to be used to draw,
     *   st - the string to draw,
     *   center - the center point of the circle (Point),
     *   r - the radius of the circle,
     *   a1 - the beginning angle on the circle to start, in radians,
     *   af - the angle advance factor (usually 1.0)
     */
    static void drawCircleText(Graphics2D g, String st, Point center,
                               double r, double a1, double af)
    {
        double curangle = a1;
   double curangleSin;
        Point2D c = new Point2D.Double(center.x, center.y);
        char ch[] = st.toCharArray();
        FontMetrics fm = g.getFontMetrics();
        AffineTransform xform1, cxform;
        xform1 = AffineTransform.getTranslateInstance(c.getX(),c.getY());
        for(int i = 0; i < ch.length; i++) {
            double cwid = (double)(getWidth(ch[i],fm));
            if (!(ch[i] == ' ' || Character.isSpaceChar(ch[i]))) {
                cwid = (double)(fm.charWidth(ch[i]));
                cxform = new AffineTransform(xform1);
                cxform.rotate(curangle, 0.0, 0.0);
                String chstr = new String(ch, i, 1);
                g.setTransform(cxform);
                g.drawString(chstr, (float)(-cwid/2), (float)(-r));
            }
 
            // compute advance of angle assuming cwid<<radius
            if (i < (ch.length - 1)) {
                double adv = cwid/2.0 + fm.getLeading() + getWidth(ch[i + 1],fm)/2.0;
      // Use of atan() suggested by Michael Moradzadeh
                curangle += Math.atan(adv / r);
      // Original code was:
      // curangle += Math.sin(adv / r);
 
            }
        }
    }
 
    /**
     * Get the width of a given character under the
     * specified FontMetrics, interpreting all spaces as
     * en-spaces.http://www.huiyi8.com/meinv/siwa/
     */
    static int getWidth(char c, FontMetrics fm) {
        if (c == ' ' || Character.isSpaceChar(c)) {
            return fm.charWidth('n');
        }
        else {
            return fm.charWidth(c);
        }
    }
 
    public static void main(String args[]) {
        CircleTextDemo ctd;
        ctd = new CircleTextDemo();
    }
 
    class FramePrinter implements ActionListener {
   private Frame fr;
   public FramePrinter(Frame f) { fr = f; }
   public void actionPerformed(ActionEvent ae) {
       PrintJob pjob;
       pjob = fr.getToolkit().getPrintJob(fr,
          "Printing Circle Demo", null, null);
       if (pjob != null) {
      Graphics g = pjob.getGraphics();
      if (g != null) {
          g.translate(100,100);
          fr.printAll(g);
          g.dispose();
      }
      pjob.end();
       }
   }
    }
 
}

Java 绘制环形的文字 (Circle Text Demo)的更多相关文章

  1. winform 绘制label 中文字 - 摘

    private void label2_Paint(object sender, PaintEventArgs e) {//绘制label中文字 string text = "Sri Lan ...

  2. openCV - 5~7 图像混合、调整图像亮度与对比度、绘制形状与文字

    5. 图像混合 理论-线性混合操作.相关API(addWeighted) 理论-线性混合操作 用到的公式 (其中 α 的取值范围为0~1之间) 相关API(addWeighted) 参数1:输入图像M ...

  3. html5 canvas绘制环形进度条,环形渐变色仪表图

    html5 canvas绘制环形进度条,环形渐变色仪表图                                             在绘制圆环前,我们需要知道canvas arc() 方 ...

  4. java.sql.SQLException:ORA-01861:文字和格式字符串不匹配

    1.错误描述 java.sql.SQLException:ORA-01861:文字和格式字符串不匹配 2.错误原因 字段名为statis_date在数据库中存储的数据类型是Date,而在Java中拼接 ...

  5. 异常-----java.sql.SQLException:ORA-01861:文字和格式字符串不匹配

    1.错误描述 java.sql.SQLException:ORA-01861:文字和格式字符串不匹配 2.错误原因 字段名为statis_date在数据库中存储的数据类型是Date,而在Java中拼接 ...

  6. 使用commons-net做FTP功能的异常 java.lang.ClassNotFoundException: org.apache.oro.text.regex.Malformed

    最近使用Apache的commons-net.jar做FTP上传下载功能,点击“上传”的时候报错,如下: java.lang.ClassNotFoundException: org.apache.or ...

  7. CAD参数绘制多行文字(com接口)

    在CAD设计时,需要绘制多行文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawMText 绘制一个多行文字.详细说明如下: 参数 说明 DOUBLE dP ...

  8. CAD参数绘制多行文字(网页版)

    在CAD设计时,需要绘制多行文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawMText 绘制一个多行文字.详细说明如下: 参数 说明 DOUBLE dP ...

  9. CAD绘制一个单行文字(com接口VB语言)

    主要用到函数说明: _DMxDrawX::DrawText 绘制一个单行文字.详细说明如下: 参数 说明 DOUBLE dPosX >文字的位置的X坐标 DOUBLE dPosY 文字的位置的Y ...

随机推荐

  1. mysql报错Packet for query is too large (12238 > 1024). You can change this value

    今天将项目部署到linux服务器的时候莫名其妙的报一些错误,可是在本地啥错没有,通过实时查看tomcat 的日志之后发现报错是: 实时查看日志: .先切换到:cd usr/local/tomcat5/ ...

  2. MBP清除NVRAM和PRAM

    Mac 会将某些设置存储在特定的存储区中,即使关机这些设置也不会丢失.在基于 Intel 的 Mac 上,存储位置是称为 NVRAM 的内存:而在基于 PowerPC 的 Mac 上,存储位置则是称为 ...

  3. LeetCode OJ--Evaluate Reverse Polish Notation

    http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 栈使用 #include <iostream> #inc ...

  4. centos 下完全卸载 mysql5.6

    查看已经安装的服务 rpm –qa|grep -i mysql -i 作用是不区分大小写 yum remove mysql mysql-server mysql-libs compat-mysql51 ...

  5. 4.JAVA语言基础部分—枚举与泛型

    枚举 //定义枚举 enum MyEnum{ ITEM_A, ITEM_B } public static void main(String[] args) { //values()获取所枚举项的集合 ...

  6. 【转】fedora和ubuntu开启ssh

    fedora和ubuntu开启ssh 1小时前 ubuntu开启SSH服务 SSH分客户端openssh-client和openssh-server如果你只是想登陆别的机器的SSH只需要安装opens ...

  7. time machine不备份指定文件夹

    osx中常常会使用timemachine来备份一些文件,timemachine能够使某个文件夹恢复到之前某个时刻的状态,很的方便.但是备份须要空间,特别是有些我们并不想备份一些无关紧要的文件,比方电影 ...

  8. 数组遍历 map()、forEach() 及 字符串切割 split() / 字符串截取 slice()、substring()、substr()

    JS数组遍历的几种方式 JS数组遍历,基本就是for,forin,foreach,forof,map等等一些方法,以下介绍几种本文分析用到的数组遍历方式以及进行性能分析对比 第一种:普通for循环 代 ...

  9. Effective C++ 条款11,12 在operator= 中处理“自我赋值” || 复制对象时不要忘记每一个成分

    1.潜在的自我赋值     a[i] = a[j];     *px = *py; 当两个对象来自同一个继承体系时,他们甚至不需要声明为相同类型就可能造成别名. 现在担心的问题是:假如指向同一个对象, ...

  10. 使用maven创建项目和cannot change version web module 3.0

    近期下载了最新的Eclipse mars.2, 这个eclipse自带了maven插件,于是就用maven尝试创建一个java web项目. 第一步,例如以下图所看到的选择 Maven Project ...