swt 中的文本轮廓

发布于 2024-10-09 15:06:04 字数 863 浏览 3 评论 0原文

我试图找出如何使用 swt 图形显示文本轮廓。 更准确地说,我需要编写以以下方式显示文本的代码: http://java.sun.com/developer/onlineTraining/Media /2DText/Art/StarryShape.gif

我找到了以下代码,我想将其从 awt 转换为 swt。

FontRenderContext frc = g2.getFontRenderContext(); 
Font f = new Font("Times",Font.BOLD,w/10);
String s = new String("The Starry Night");
TextLayout tl = new TextLayout(s, f, frc);
float sw = (float) tl.getBounds().getWidth();
AffineTransform transform = new AffineTransform();
transform.setToTranslation(w/2-sw/2, h/4);
Shape shape = tl.getOutline(transform);
Rectangle r = shape.getBounds();
g2.setColor(Color.blue);
g2.draw(shape);

(代码来自java.sun.com/developer/onlineTraining/Media/2DText/style.html)

但是我不知道如何在swt中获取TextLayout的轮廓。 有这样的可能吗?

I am trying to find out how to show text outline by using of swt graphics.
More precisely I need to write code which shows text in following way:
http://java.sun.com/developer/onlineTraining/Media/2DText/Art/StarryShape.gif

I found following code and I'd like to translate it from awt to swt.

FontRenderContext frc = g2.getFontRenderContext(); 
Font f = new Font("Times",Font.BOLD,w/10);
String s = new String("The Starry Night");
TextLayout tl = new TextLayout(s, f, frc);
float sw = (float) tl.getBounds().getWidth();
AffineTransform transform = new AffineTransform();
transform.setToTranslation(w/2-sw/2, h/4);
Shape shape = tl.getOutline(transform);
Rectangle r = shape.getBounds();
g2.setColor(Color.blue);
g2.draw(shape);

(code from java.sun.com/developer/onlineTraining/Media/2DText/style.html )

But I can't figure out how to get Outline of the TextLayout in swt.
Is there such possibility?

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

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

发布评论

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

评论(1

莫多说 2024-10-16 15:06:04

可以使用 SWT 中的 Path 类来完成此操作。例如:

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

public class ShapeText 
{
    public static void main(String[] args) 
    {
        final Display display = new Display();
        Font font = new Font(display, "Times", 50, SWT.BOLD);
        final Color blue = display.getSystemColor(SWT.COLOR_BLUE);
        final Path path;
        try {
            path = new Path(display);
            path.addString("The Starry Night", 0, 0, font);
        } catch (SWTException e) {
            System.out.println(e.getMessage());
            display.dispose();
            return;
        }

        Shell shell = new Shell(display);
        shell.addListener(SWT.Paint, new Listener() 
        {
            public void handleEvent(Event e) 
            {           
                GC gc = e.gc;

                //Transform a = new Transform(display);
                //a.shear(0.7f, 0f);
                //gc.setTransform(a);
                gc.setForeground(blue);
                gc.fillPath(path);
                gc.drawPath(path);
            }
        });

        shell.setSize(530,120);

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }

        path.dispose();
        font.dispose();
        display.dispose();
    }
}

上面的代码不是您发布的 Swing 代码片段的精确翻译,但意图是相同的。

另请检查此链接:http://www.eclipse.org/swt/snippets/

特别路径和模式部分。

希望这会有所帮助。

Well there is a possibility of doing this using Path class in SWT. For example:

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

public class ShapeText 
{
    public static void main(String[] args) 
    {
        final Display display = new Display();
        Font font = new Font(display, "Times", 50, SWT.BOLD);
        final Color blue = display.getSystemColor(SWT.COLOR_BLUE);
        final Path path;
        try {
            path = new Path(display);
            path.addString("The Starry Night", 0, 0, font);
        } catch (SWTException e) {
            System.out.println(e.getMessage());
            display.dispose();
            return;
        }

        Shell shell = new Shell(display);
        shell.addListener(SWT.Paint, new Listener() 
        {
            public void handleEvent(Event e) 
            {           
                GC gc = e.gc;

                //Transform a = new Transform(display);
                //a.shear(0.7f, 0f);
                //gc.setTransform(a);
                gc.setForeground(blue);
                gc.fillPath(path);
                gc.drawPath(path);
            }
        });

        shell.setSize(530,120);

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }

        path.dispose();
        font.dispose();
        display.dispose();
    }
}

The above code is not an exact translation of the Swing snippet that you have posted but the intent is same.

Also check this link : http://www.eclipse.org/swt/snippets/

Specially the Path and Pattern section.

Hope this will help.

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