2011-10-07 58 views

回答

1

使用FontMetrics類來計算當前Font的高度。像這樣:

// g is your Graphics object 
Font f = g.getFont(); 
FontMetrics fm = g.getFontMetrics(f); 
int font_height = fm.getHeight() 
int linesPerImage = image_height/font_height; 

然後繼續循環你的文字,直到沒有文字行留下來繪製。 當您到達linesPerImage時,保存當前圖像並創建一個新圖像。 例如,如果您有要在名爲「行」的列表中繪製的字符串列表:

int i = lines.size(); 
for (int j = 0; j < i, j++) { 
    g.drawString(lines.get(j), x, y); 
    if (j > 0 && j % linesPerImage == 0) { 
    // save current image and create new graphics to draw to 
    } 
} 
相關問題