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