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 的大纲。有这种可能吗?

4

1 回答 1

3

那么有可能使用PathSWT 中的类来执行此操作。例如:

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/

特别是路径和模式部分。

希望这会有所帮助。

于 2010-12-27T17:39:03.317 回答