Pregunta

Estoy tratando de encontrar la manera de mostrar esquema de texto mediante el uso de gráficos SWT. Más precisamente necesito código de escritura que muestra el texto en el siguiente manera: http://java.sun.com/developer/onlineTraining/Media /2DText/Art/StarryShape.gif

He encontrado siguiente código y me gustaría traducirlo del AWT a 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);

(código de java.sun.com/developer/onlineTraining/Media/2DText/style.html)

Pero no puedo encontrar la manera de llegar Esquema del TextLayout en SWT. ¿Existe tal posibilidad?

¿Fue útil?

Solución

Bueno hay una posibilidad de hacer uso de esta clase Path en SWT. Por ejemplo:

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

El código anterior no es una traducción exacta del fragmento de oscilación que se ha publicado, pero la intención es la misma.

También puedes ver este enlace: http://www.eclipse.org/swt/snippets/

Especialmente la sección Ruta de acceso y del patrón.

Esperamos que esto ayude.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top