2010-12-27 15 views
3

swtグラフィックを使用してテキストのアウトラインを表示する方法を調べようとしています。 は、より正確には、私は次のようにテキストを示し、コード記述する必要があります。私は次のコードを見つけ http://java.sun.com/developer/onlineTraining/Media/2DText/Art/StarryShape.gifswtのテキストの概要

を、私はSWTのAWTから、それを翻訳したいと思います。

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の概要を取得する方法を見つけ出すことはできません。 このような可能性はありますか?

+0

正確な翻訳が必要なのか、swtで枠線付きのテキストを実装するには 'how'ですか?あなたの質問からはっきりと分かりません。 – Favonius

答えて

2

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/

特別パスとパターンセクション:

はまた、このリンクをご確認ください。

希望すると、これが役立ちます。

+0

お返事ありがとうございます!私はhttp://www.eclipse.org/swt/snippets/に行きましたが、それを見ることはできませんでした。 – deephace

+1

これはあなたの質問に答えますか?もしそうなら、答えた質問に印を付けてください。 –

+0

はい、それは答えですが、この解決策には1つの欠点があります。テキストのフォントサイズは保持されません。多分誰かがそれに対処する方法を知っていますか? – deephace