codice:
private int printText(TextToPrint text, Book book, Vector currentPage, int currY, int xLimit, int yLimit) {
// set the font used to print this text
graphics.setFont(text.font);
// Get the FontMetrics for the current Font
fm = graphics.getFontMetrics();
int rowHeight = fm.getHeight();
// If a new page is beginning shift the cursor in the bottom position of the first row
if (currY == 0) currY = rowHeight;
// Tokenize the test to be printed in row
StringTokenizer st = new StringTokenizer(text.text, "\n", true);
// Remember is the last row printed is empty or not
boolean emptyRow = true;
while (st.hasMoreTokens()) {
String currentRow = st.nextToken();
// If the row is empty move down the point of a distance equal to the row height
if (currentRow.equals("\n")) {
if (emptyRow) {
currY += rowHeight;
// If the page is finished save it in the book and begin a new one
if (currY >= yLimit) {
book.append(new Page((Vector)currentPage.clone()), pf);
currentPage.removeAllElements();
currY = rowHeight;
}
} else emptyRow = true;
} else {
// the row isn't empty
emptyRow = false;
// split the current block of text in more row to fit it in the page
Enumeration rows = splitString(currentRow, xLimit).elements();
// Print the text a row at time
while (rows.hasMoreElements()) {
currentPage.add(new StringToPrint((String)rows.nextElement(), text.font, (int)pf.getImageableX(), (int)pf.getImageableY() + currY));
currY += rowHeight;
// If the page is finished save it in the book and begin a new one
if (currY >= yLimit) {
book.append(new Page((Vector)currentPage.clone()), pf);
currentPage.removeAllElements();
currY = rowHeight;
}
}
}
}
return currY;
}
Guarda questo codice,forse non fa perfettamente al caso tuo ma può darti qualche idea su come la cosa possa essere fatta.