Java 中的弯曲文本

发布于 2024-10-19 19:29:06 字数 177 浏览 4 评论 0原文

我正在寻找最简单的方法来绘制一些 我的应用程序上椭圆对象周围的文本。
我需要创造一种“拥抱”的感觉。

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

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

有什么建议吗?

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".

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

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

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚 2024-10-26 19:29:06

这是弯曲文本的示例:

// 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);
            }
        });
    }
}

它会生成:

在此处输入图像描述

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);
            }
        });
    }
}

which produces:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文