1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Text on circle

Discussion in 'Java' started by Manojbijnori, Jun 4, 2010.

  1. Manojbijnori

    Manojbijnori New Member

    Joined:
    Jun 16, 2009
    Messages:
    29
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Lecturer In Computer Science Department In Enginer
    Location:
    Bijnore
    /* If you want to write the text on circle in java then you can use the following code*/
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
        public class CircleTextDemo extends Canvas {
        Frame myframe;
        TextField text;
        Font myfont;
        Color textcolor;
        Color circlecolor;
        
            public CircleTextDemo() {
            this("Serif", Font.PLAIN, 18, Color.pink, Color.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");
            myframe.add(text, BorderLayout.NORTH);
            myframe.add(this, BorderLayout.CENTER);
            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();
                }
            });
            myframe.setVisible(true);
        }
        
            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.
        */
        static void drawCircleText(Graphics2D g, String st, Point center,
        double r, double a1, double af)
            {
            double curangle = a1;
            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<
                    if (i < (ch.length - 1)) {
                    double adv = cwid/2.0 + fm.getLeading() + getWidth(ch[i + 1],fm)/2.0;
                    curangle += Math.sin(adv / r);
                }
            }
        }
        
            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();
        }
    }
     
    Last edited by a moderator: Jun 4, 2010
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    The same code is found many other site and any reason to paste it here?
     
  3. seangtz

    seangtz New Member

    Joined:
    Jun 6, 2008
    Messages:
    126
    Likes Received:
    3
    Trophy Points:
    0
    Is this possible to change in codes?
     

Share This Page