newbie: printing multiple lines

J

Johan

Hi,

I'm trying to print multiple lines from one string array. The text gets
printed, but they overlap each other, instead of being printed one line
at a time.
Does someone know how to do this?
TIA

Johan

This is what I came up with till now:

import java.awt.*;
import java.awt.print.*;

public class PrintData {

public PrintData() {
PrinterJob printerjob = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(new TheData(), printerjob.defaultPage());
printerjob.setPageable(book);
try {
printerjob.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
}

class TheData implements Printable {
Font fnt = new Font("Arial", Font.PLAIN, 12);

public int print(Graphics g, PageFormat pf, int pageIndex) throws
PrinterException {
String[] test = {"test 1", "test 2", "test 3", "test 4"};
g.setFont(fnt);
g.setColor(Color.black);
for (int i = 1, j = 70; i < test.length; i++, j = j + 20) {
g.drawString(test, 75, j);
}
return Printable.PAGE_EXISTS;
}
}
 
V

VisionSet

Johan said:
Hi,

I'm trying to print multiple lines from one string array. The text gets
printed, but they overlap each other, instead of being printed one line
at a time.
Does someone know how to do this? ....
for (int i = 1, j = 70; i < test.length; i++, j = j + 20) {
g.drawString(test, 75, j);


use myFont.getLineMetrics() to get a LineMetrics object and use getHeight()
on that to replace your arbitrary 20 increment on your Y axis.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top