2013-05-17 73 views
2

我目前正在使用下面的代碼繪製一條線,並希望獲得我的路徑周圍的邊界框,但是如果我撥打path.getBounds()我得到邊界框忽略筆畫寬度和線條連接,因此當放置在JViewport中時,我的繪畫最終會剪掉一些部分。我怎樣才能得到真正繪製的邊界框。如何計算考慮到筆劃的路徑邊界框

我發現的唯一的事情就是下面的類,但源不可http://pic.dhe.ibm.com/infocenter/jviewtgo/v8r8/index.jsp?topic=%2Fcom.ibm.ilog.jviews.tgo.doc%2Fhtml%2Frefjava%2Fhtml%2Filog%2Ftgo%2Fgrprim%2Fpackage-summary.html

GeneralPath path = new GeneralPath(); 
if (fPoints.size() > 0) { 
    Point first = (Point)fPoints.elementAt(0); 
    path.moveTo((float)first.getX(),(float) first.getY()); 
    for (int i = 1; i < fPoints.size(); i++) { 
     Point p1 = (Point) fPoints.elementAt(i); 
     path.lineTo(p1.x, p1.y); 
    } 
} 
g2d.setStroke(new BasicStroke(lineModel.getStrokeWidth(), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND)); 
g2d.setColor(lineModel.getLineColor()); 
g2d.draw(path); 

回答

4

我終於找到了解決方法是使用筆劃對象本身

Stroke s = new BasicStroke(lineModel.getStrokeWidth(), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND); 
s.createStrokedShape(path).getBounds();