Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 715|回复: 0

[Swing学习]Java Swing绘制环形文字

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-15 23:57:08 | 显示全部楼层 |阅读模式
    效果如图:


    代码:
    1. import java.awt.*;
    2. import java.awt.event.*;
    3. import java.awt.geom.*;
    4.          
    5. /**
    6. * A demo class that illustrates drawing text
    7. * along the outside of a circle.
    8. */
    9. public class CircleTextDemo extends Canvas {
    10.     Frame myframe;
    11.     TextField text;
    12.     Button printBtn;
    13.     Font myfont;
    14.     Color textcolor;
    15.     Color circlecolor;
    16.    
    17.     /**
    18.      * Create a CircleTextDemo canvas and frame
    19.      * with default settings.
    20.      */
    21.     public CircleTextDemo() {
    22.         this("Serif", Font.PLAIN, 18, Color.pink, Color.black);
    23.     }

    24.     /**
    25.      * Create a CircleTextDemo canvas and frame
    26.      * with supplied settings.
    27.      *
    28.      * @param ff Font family (usually "Serif")
    29.      * @param fs Font style (usually Font.PLAIN)
    30.      * @param fz Font size (usually 18)
    31.      * @param bg Background color for this canvas (usually pink)
    32.      * @param fg Foreground color for text (usually black)
    33.      */
    34.     public CircleTextDemo(String ff, int fs, int fz, Color bg, Color fg) {
    35.         setBackground(bg);
    36.         circlecolor = bg.brighter();
    37.         textcolor = fg;
    38.         myfont = new Font(ff, fs, fz);

    39.         text = new TextField("Text on a circle using Java 2D Graphics!");
    40.         myframe = new Frame("CircleTextDemo");
    41.         printBtn = new Button("Print");
    42.         myframe.add(text, BorderLayout.NORTH);
    43.         myframe.add(this, BorderLayout.CENTER);
    44.         myframe.add(printBtn, BorderLayout.SOUTH);
    45.         myframe.setSize(new Dimension(300,340));
    46.         myframe.setLocation(150,140);
    47.         myframe.addWindowListener(new WindowAdapter() {
    48.                 public void windowClosing(WindowEvent we) {
    49.                     System.exit(0);
    50.                 }
    51.             });
    52.         text.addActionListener(new ActionListener() {
    53.                 public void actionPerformed(ActionEvent ae) {
    54.                     repaint();
    55.                 }
    56.             });
    57.    printBtn.addActionListener(new FramePrinter(myframe));
    58.         myframe.setVisible(true);
    59.     }
    60.         
    61.     /**
    62.      * Paint the contents of the CircleDemoText canvas.
    63.      *
    64.      * @param g - a Graphics, hopefully a Graphics2D
    65.      */
    66.     public void paint(Graphics g) {
    67.         String st = text.getText();
    68.         if (st.length() == 0) return;
    69.         if (g instanceof Graphics2D) {
    70.             Dimension cd = getSize();
    71.             Point pt = new Point(cd.width / 2, cd.height / 2);
    72.             int radius = (int)(pt.x * 0.84);
    73.             g.setColor(circlecolor);
    74.             g.drawArc(pt.x - radius, pt.y - radius,
    75.                       radius*2-1, radius*2-1,
    76.                       0, 360);
    77.             g.setColor(textcolor);
    78.             g.setFont(myfont);
    79.             drawCircleText((Graphics2D)g, st, pt, radius, -Math.PI/2, 1.0);
    80.         }
    81.         else {
    82.             System.out.println("Cannot draw curved text without a Graphics2D");
    83.         }
    84.     }
    85.                 
    86.     /**
    87.      * Draw a piece of text on a circular curve, one
    88.      * character at a time.  This is harder than it looks...
    89.      *
    90.      * This method accepts many arguments:
    91.      *   g - a Graphics2D ready to be used to draw,
    92.      *   st - the string to draw,
    93.      *   center - the center point of the circle (Point),
    94.      *   r - the radius of the circle,
    95.      *   a1 - the beginning angle on the circle to start, in radians,
    96.      *   af - the angle advance factor (usually 1.0)
    97.      */
    98.     static void drawCircleText(Graphics2D g, String st, Point center,
    99.                                double r, double a1, double af)
    100.     {
    101.         double curangle = a1;
    102.         double curangleSin;
    103.         Point2D c = new Point2D.Double(center.x, center.y);
    104.         char ch[] = st.toCharArray();
    105.         FontMetrics fm = g.getFontMetrics();
    106.         AffineTransform xform1, cxform;
    107.         xform1 = AffineTransform.getTranslateInstance(c.getX(),c.getY());
    108.         for(int i = 0; i < ch.length; i++) {
    109.             double cwid = (double)(getWidth(ch[i],fm));
    110.             if (!(ch[i] == " " || Character.isSpaceChar(ch[i]))) {
    111.                 cwid = (double)(fm.charWidth(ch[i]));
    112.                 cxform = new AffineTransform(xform1);
    113.                 cxform.rotate(curangle, 0.0, 0.0);
    114.                 String chstr = new String(ch, i, 1);
    115.                 g.setTransform(cxform);
    116.                 g.drawString(chstr, (float)(-cwid/2), (float)(-r));
    117.                     }
    118.          
    119.             // compute advance of angle assuming cwid < < radius
    120.             if (i < (ch.length - 1)) {
    121.                 double adv = cwid/2.0 + fm.getLeading() + getWidth(ch[i + 1],fm)/2.0;
    122.               // Use of atan() suggested by Michael Moradzadeh
    123.                 curangle += Math.atan(adv / r);
    124.               // Original code was:
    125.               // curangle += Math.sin(adv / r);
    126.          
    127.            }
    128.         }
    129.     }

    130.     /**
    131.      * Get the width of a given character under the
    132.      * specified FontMetrics, interpreting all spaces as
    133.      * en-spaces.
    134.      */
    135.     static int getWidth(char c, FontMetrics fm) {
    136.         if (c == " " || Character.isSpaceChar(c)) {
    137.             return fm.charWidth("n");
    138.         }
    139.         else {
    140.             return fm.charWidth(c);
    141.         }
    142.     }
    143.          
    144.     public static void main(String args[]) {
    145.         CircleTextDemo ctd;
    146.         ctd = new CircleTextDemo();
    147.     }

    148.     class FramePrinter implements ActionListener {
    149.         private Frame fr;
    150.         public FramePrinter(Frame f) { fr = f; }
    151.         public void actionPerformed(ActionEvent ae) {
    152.         PrintJob pjob;
    153.         pjob = fr.getToolkit().getPrintJob(fr, "Printing Circle Demo", null, null);
    154.         if (pjob != null) {
    155.           Graphics g = pjob.getGraphics();
    156.          if (g != null) {
    157.            g.translate(100,100);
    158.            fr.printAll(g);
    159.            g.dispose();
    160.          }
    161.          pjob.end();
    162.         }
    163.    }
    164.   }
    165.          
    166. }
    复制代码

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/11/15/235707468.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 07:29 , Processed in 0.293790 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表