2013-08-21 2 views

답변

1

는이

Graphics2D g2d = ... 
Font font = ... 
Rectangle2D r = font.getStringBounds("Your_String!", g2d.getFontRenderContext()); 
System.out.println("(" + r.getWidth() + ", " + r.getHeight() + ")"); 
뭔가를 시도 할 수 있습니다
1

뭔가가있는 경우 또는 :

Graphics2D g2d = (Graphics2D) g; 
    FontRenderContext frc = g2d.getFontRenderContext(); 
    GlyphVector gv = g2d.getFont().createGlyphVector(frc, "YOUR_STRING"); 
    Rectangle rect = gv.getPixelBounds(null, INITIAL_X, INITIAL_Y); 

    int width = rect.width; 
    int height = rect.height; 
0

문자열 그리기에 TextLayout을 사용할 수 있습니다. 이것은 도형과 매우 유사하며 일반 문자열 그리기보다 많은 옵션이 있습니다.

Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 12); 
TextLayout tl = new TextLayout("My String", f, gd2.getFontRenderContext()); 

// bounds of the TextLayout 
Rectangle2D bounds = tl.getBounds(); 

// draw the text 
tl.draw(g2d, x, y) 

// position at end 
double xEnd = bounds.getWidth() + x; 
double yEnd = bounds.getHeight() + y; 
Point2D end = new Point2D.Double(bounds.getWidth() + x, bounds.getHeight() + y); 
관련 문제