SWT中的文本轮廓

4
我正在尝试使用 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的轮廓。 是否有这样的可能性?


你是想要精确的翻译还是关于如何在 SWT 中实现带边框的文本?从你的问题中并不清楚。 - Favonius
1个回答

3

使用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/,特别是路径和模式部分。
希望这能有所帮助。

谢谢回答!我已经访问了http://www.eclipse.org/swt/snippets/,但还没有找到它。 - deephace
1
这个回答解决了您的问题吗?如果是,请标记该问题已解决。 - Paul Lammertsma
是的,那就是答案,但这种解决方案有一个缺点。它无法保留文本字体大小。也许有人知道如何处理它? - deephace

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接