How do i align this text correctly?

前端 未结 2 980
失恋的感觉
失恋的感觉 2020-11-29 09:42

I wrote this polar clock today and i am almost finished exept i want to align my text inside the line similar to this. Does anyone know how to do this? Ive tried to use Font

相关标签:
2条回答
  • 2020-11-29 10:22

    Here's a simple example of rotating text.

    Addendum: You'll want to adjust the the text's radial starting point by stringWidth(name[n]). Your program appears to be rotating individual characters in a effort to follow the arc, while the example appears to be drawing the text in a straight line tangent to the arc. The latter approach may prove simpler. For example, this variation centers the labels across the arc's getStartPoint():

    for (int i = 0; i < vars.length; i++) {
        ...
        String s = names[0];
        int w = fm.stringWidth(s);
        int h = fm.getHeight() + fm.getMaxDescent();
        Point2D p = arch.getStartPoint();
        int x = (int) p.getX();
        int y = (int) p.getY();
        radians = (vars[i]) * HPI;
        g.rotate(radians, x, y);
        g.drawString(s, x - w / 2, y + h);
        g.rotate(-radians, x, y);
    }
    

    enter image description here

    For convenience the code above does rotate() to and fro; for comparison, here's the original example showing repeated concatenations of rotate():

    enter image description here

    import java.awt.*;
    import java.awt.geom.AffineTransform;
    import javax.swing.*;
    
    /** @see http://stackoverflow.com/questions/6238037 */
    public class RotateText extends JPanel {
    
        private static final Font f = new Font("Serif", Font.BOLD, 32);
        private static final String s = "Hello World!";
        private static final Color[] colors = {
            Color.red, Color.green, Color.blue, Color.cyan
        };
        private Graphics2D g2d;
        private AffineTransform at;
    
        public RotateText() {
            setPreferredSize(new Dimension(400, 400));
        }
    
        @Override
        public void paintComponent(Graphics g) {
            g2d = (Graphics2D) g;
            g2d.setFont(f);
            g2d.setColor(Color.black);
            g2d.fillRect(0, 0, getWidth(), getHeight());
            at = g2d.getTransform();
            int w = this.getWidth();
            int h = this.getHeight();
            int w2 = g2d.getFontMetrics().stringWidth(s) / 2;
            int h2 = 2 * g2d.getFontMetrics().getHeight() / 3;
            render(0, w / 2 - w2, h - h2);
            render(1, h2, h / 2 - w2);
            render(2, w / 2 + w2, h2);
            render(3, w - h2, h / 2 + w2);
            g2d.setTransform(at);
            g2d.setColor(Color.yellow);
            g2d.fillRect(w / 3, h / 3, w / 3, h / 3);
        }
    
        private void render(int n, int x, int y) {
            g2d.setColor(colors[n]);
            g2d.setTransform(at);
            g2d.rotate(n * Math.PI / 2, x, y);
            g2d.drawString(s, x, y);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                //@Override
                public void run() {
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.add(new RotateText(), BorderLayout.CENTER);
                    f.pack();
                    f.setVisible(true);
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-11-29 10:28

    You have to be able to draw text along the curves. There are several ways to do it, but the simplest one is to use Stroke API. You can find an example at http://www.jhlabs.com/java/java2d/strokes/

    The other way is using affine transforms. The example is at http://www.java2s.com/Code/Java/2D-Graphics-GUI/Drawtextalongacurve.htm

    0 讨论(0)
提交回复
热议问题