2010-12-27 6 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에서 테두리가있는 텍스트를 구현 하시겠습니까 ??? 귀하의 질문에 명확하지 않습니다. – 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(); 
    } 
} 

위의 코드는 게시 한 스윙 조각의 정확한 번역은 아니지만 의도는 동일합니다. http://www.eclipse.org/swt/snippets/

특별히 경로 및 패턴 섹션 :

또한이 링크를 확인하십시오.

희망이 도움이 될 것입니다.

+0

답변 해 주셔서 감사합니다! 나는 http://www.eclipse.org/swt/snippets/에 가봤지만 그것을 볼 수 없었다. – deephace

+1

이 질문에 대한 답변이 있습니까? 그렇다면 답을 표시하십시오. –

+0

예, 답변이지만이 솔루션에는 단점이 있습니다. 텍스트 폰트 크기를 보존하지 않습니다. 어쩌면 누군가 그것을 다루는 방법을 알고 있을까요? – deephace