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. Advertisements

  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. Advertisements

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. tshad
    Replies:
    2
    Views:
    551
    tshad
    Jan 27, 2005
  2. Jack
    Replies:
    9
    Views:
    2,903
  3. liu
    Replies:
    6
    Views:
    813
    Joe Kesselman
    Jul 28, 2006
  4. lovecreatesbeauty

    How to know two lines are a pare parallel lines

    lovecreatesbeauty, Apr 27, 2006, in forum: C Programming
    Replies:
    11
    Views:
    859
    Old Wolf
    Apr 28, 2006
  5. John Bokma

    Printing multiple Array as multiple column

    John Bokma, May 11, 2004, in forum: Perl Misc
    Replies:
    8
    Views:
    292
    Edward Wijaya
    May 12, 2004
  6. Dave

    multiple changes on multiple lines

    Dave, Apr 2, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    226
    Gunnar Hjalmarsson
    Apr 2, 2005
  7. Ciba LO
    Replies:
    4
    Views:
    370
    Ciba LO
    Jul 8, 2006
  8. Cah Sableng
    Replies:
    0
    Views:
    403
    Cah Sableng
    Apr 23, 2007
Loading...