Java中的弯曲文本 [英] Curved text in Java

查看:147
本文介绍了Java中的弯曲文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在我的应用程序上围绕椭圆对象绘制一些
文本的最简单方法。

我需要创造一种拥抱的感觉。

I am looking for the simplest way to draw some text around an ellipse object on my app.
I need to create a feeling of "cuddling".

到目前为止,我已经使用Graphics2D类在屏幕上打印我的图纸
而我的画布是BufferedImage。

So far, I've used the Graphics2D class to print my drawings on screen and my "canvas" is a BufferedImage.

我的椭圆的宽度和高度分别为50,50。

The width and height of my ellipses are constant at 50,50 respectively.

有什么建议吗?

推荐答案

以下是弯曲文本的示例:

Here's an example of curved text:

// slightly modified from the original:
// http://examples.oreilly.com/9781565924840/examples/RollingText.java 
import javax.swing.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;

public class RollingText extends JFrame {

    RollingText() {
        super("RollingText v1.0");
        super.setSize(650, 350);
        super.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        String s = "What's our vector, Victor?";
        Font font = new Font("Serif", Font.PLAIN, 24);
        FontRenderContext frc = g2.getFontRenderContext();
        g2.translate(40, 80);

        GlyphVector gv = font.createGlyphVector(frc, s);
        int length = gv.getNumGlyphs();
        for (int i = 0; i < length; i++) {
            Point2D p = gv.getGlyphPosition(i);
            double theta = (double) i / (double) (length - 1) * Math.PI / 4;
            AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
            at.rotate(theta);
            Shape glyph = gv.getGlyphOutline(i);
            Shape transformedGlyph = at.createTransformedShape(glyph);
            g2.fill(transformedGlyph);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new RollingText().setVisible(true);
            }
        });
    }
}

产生:

这篇关于Java中的弯曲文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆