newbie: printing multiple lines

Discussion in 'Java' started by Johan, Nov 11, 2003.

  1. Johan

    Johan Guest

    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;
    }
    }
     
    Johan, Nov 11, 2003
    #1
    1. Advertising

  2. Johan

    VisionSet Guest

    "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, 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
     
    VisionSet, Nov 11, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack
    Replies:
    9
    Views:
    2,738
  2. liu
    Replies:
    6
    Views:
    693
    Joe Kesselman
    Jul 28, 2006
  3. Dave

    multiple changes on multiple lines

    Dave, Apr 2, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    116
    Gunnar Hjalmarsson
    Apr 2, 2005
  4. Ciba LO
    Replies:
    4
    Views:
    198
    Ciba LO
    Jul 8, 2006
  5. Cah Sableng
    Replies:
    0
    Views:
    261
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page