Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > newbie: printing multiple lines

Reply
Thread Tools

newbie: printing multiple lines

 
 
Johan
Guest
Posts: n/a
 
      11-11-2003
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[i], 75, j);
}
return Printable.PAGE_EXISTS;
}
}
 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      11-11-2003

"Johan" <johankees*AT*@dolfijn*DOT*.nl> wrote in message
news:boqp83$5g6$...
> 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[i], 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.

--
Mike W


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM
Preserve blank lines when add multiple lines of text to a cell Cah Sableng Javascript 0 04-23-2007 04:46 AM
How to break the lines in printing to html lines? Ciba LO Perl Misc 4 07-08-2006 02:58 AM



Advertisments