private static final Font TEXT_FONT = new Font("SansSerif", Font.PLAIN, 10);
private static final Font TITLE_FONT = new Font("SansSerif", Font.BOLD, 12);
public static void print(final String title, final String text) {
final Frame parent = new Frame();
final PrintJob job = Toolkit.getDefaultToolkit().getPrintJob(parent, "Print " + title, new Properties());
if (job != null) {
final Graphics graphic = job.getGraphics();
// Dimension pageSize = job.getPageDimension();
if (graphic != null) {
graphic.translate(10, 10);
final int x = 50;
int y = 50;
graphic.setFont(TITLE_FONT);
final int height = graphic.getFontMetrics().getAscent();
final int width = graphic.getFontMetrics().stringWidth(title);
graphic.drawRect(x - 10, y - 10 - height, width + 20, height + 20);
graphic.drawString(title, x, y);
y += graphic.getFontMetrics().getHeight();
y += 20;
graphic.setFont(TEXT_FONT);
final StringTokenizer tk = new StringTokenizer(text, "\n\r");
while (tk.hasMoreTokens()) {
final String line = tk.nextToken();
graphic.drawString(line, x, y);
y += graphic.getFontMetrics().getHeight();
}
graphic.dispose();
}
job.end();
}
parent.dispose();
}