🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #17092

Re: Anyone out there willing to help?

#17092
From: Ross Dukeshier
Date: Sun, 20 Dec 1998 00:00
303 lines
11587 bytes
I don't know if the implementation is incomplete yet or not, but I'd
definitely lean towards some things that don't make much sense.  I can't see
why they didn't make Book Printable for one.  It seems to me it should be
the ultimate Printable object.  I'd agree wholeheartedly that they need to
give a much better overview of printing - very weak, but still much better
than the previous stuff.

I would think you'd just have to use the PageFormat's getImageableX and
getImageableY combined with the getImageableWidth and getImageableHeight
methods to draw a Rectangle, but I'd guess you've done that and it doesn't
work.  It wouldn't surprise me, but maybe it's off by one pixel.  Have you
tried adding 5 or so to each value (subtracting on the lower bounds) to get
it to draw it?

The job name stuff is a bug identified in the bug parade - I think it said
it was in progress so they know about it.

You should be able to set the orientation of the page through the PageFormat
as well.  And, I think if the size isn't one you want, you can resize the
paper using the setPaper method and defining your own paper object.

The PrintJob dialog isn't a JDK thing as far as I know.  The JVM just brings
up the same dialog the native machine uses.  If you try to print from Word,
you get the same dialog.  This dialog doesn't play with others in that it is
supposed to return the properties you selected in the properties object you
passed to the getPrintJob method.  If you think about it, this is divergent
from the way Java does things as you can't normally have an "out" parameter
in a Java method.  Basically, the properties object you send will be ignored
and the dialog never sets them for a return.  I'm sure this is something
Windows failed to do as they didn't expect anyone but them to be using it
(or they did and wanted to cause problems - who knows).  Anyway, I assumed
the reason for a totally platform independent printing approach in JDK 1.2
was because they didn't want to rely on certain other companies to fix their
stuff.

Hadn't thought about it, but now that you mention it, I don't remember
seeing anything to give me the properties back.  You don't think they missed
that do ya?  If so, then this time you can blame Sun for screwing it up.
You'd be able to use the printer options again during the run I suppose, but
not as default on another session.  I'll have to look for that one, but it's
on the back-burner until I get all the kinks worked out of the printing
itself.

I'm to the point where I can print out a page.  My problem wasn't with the
setColor stuff as I'd heard about that one from someone else.  I was trying
to sub-class Book and make it Printable as I thought it should be and it
didn't like it.  Anyway, I can print, but for some reason, it doesn't print
out anything after the first page (second one is now blank - think I need to
set the color twice? hehehe).  Test output shows the print method is getting
called for the second page and that it's drawing the right Strings on the
graphics object, but again, nothing comes out on it.  I'm thinking it may
have something to do with not having it Pageable, but I'd think that if that
was the case it wouldn't call it a second time.  Any ideas?

Here's the code as it stands now.  Please run it and see if you can find
anything in the print method I'm missing.  If you have ideas on how to make
it paginate better, I'd love to hear them - seems a little awkward, but
that's why I thought Book was the right way to go at first.  Also, if you
have any ideas on how to make it word wrap, I'm all ears - that's the next
step after I get it to just give me all the pages.  A method for the Font
class to pull the number of words from a string that fit within a specified
Imageable width would have been nice.  Any ideas on how to implements such a
thing?

import java.awt.event.*;
import java.awt.font.*;
import java.text.*;
import java.util.*;
import java.awt.print.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.image.*;

public class PrintableArea extends JTextArea implements Printable {

  protected String time;
  protected int fontsize;
  protected String text;
  protected Font font;
  protected JFrame testFrame = new JFrame();
  protected JTextArea testArea = new JTextArea();
  protected Font headerfont;
  protected LineMetrics metrics;
  protected LineMetrics headermetrics;
  protected double x0;
  protected double y0;
  protected double headery;
  protected int linesperpage;
  protected int charnum = 0;
  protected int linenum = 0;
  protected int pagenum = 0;
  protected PrinterJob job;
  protected Book book;
  protected BufferedImage bi;
  //protected Graphics2D page;
  protected Vector page;
  protected Vector pages = new Vector();
  protected PageFormat pageFormat;

  public PrintableArea() {
    fontsize = 12;
    font = new Font("Monospaced", Font.PLAIN, fontsize);
    GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
    job = PrinterJob.getPrinterJob();
    pageFormat = job.defaultPage();
    job.setPrintable(this);
  }

  public void flush() {}

  //public void close() {
  //    if (page != null) page.dispose();
  //}

  public void setFontStyle(int style) {
      Font current = font;
      try {
        font = new Font("Monospaced", Font.PLAIN, fontsize);
      }
      catch (Exception e) {font = current;}
      //if (page != null) page.setFont(font);
  }

  public void setHeader(String header) {
    headermetrics = headerfont.getLineMetrics(header, 0, header.length(),
new FontRenderContext(null,false,false));
  }

  public void setHeaderFontStyle(int style) {
      Font current = font;
      try {
        headerfont = new Font("SansSerif", Font.ITALIC, fontsize);
      }
      catch (Exception e) {font = current;}
      //if (page != null) page.setFont(font);
  }


  //public void pageBreak() { newpage();  }

  public int getLinesPerPage() {return this.linesperpage; }

  protected void newline() {
    charnum = 0;
    linenum++;
    if (linenum >= linesperpage) {
      book.setPage(pagenum, this, pageFormat);
      page = null;
    }
  }

  public int print(Graphics g, PageFormat pf, int pageIndex) {
    try {
      int x = (int) ((pf.getWidth() - pf.getImageableWidth())/2);
      int y = (int) ((pf.getHeight()-pf.getImageableHeight())/2);
      int fontHeight = 12;

      System.out.println("Determining lines per page");
      if (pf.getOrientation() == pf.PORTRAIT) {
        linesperpage = 50;
      }
      else if (pf.getOrientation() == pf.LANDSCAPE) {
        linesperpage = 30;
      }

      linenum = 0;
      String line = new String("");
      int eolIndex;

      text = this.getText();
      int startIndex = 0;
      while (startIndex != -1 && linenum < pageIndex*linesperpage) {
        startIndex = text.indexOf("\n",startIndex+1);
        linenum = linenum + 1;
      }
      System.out.println("Text should be at line #"+linenum);
      linenum = 0;
      int currIndex = startIndex;
      int nextIndex = startIndex;
      String indexLocs = new String("");
      while (nextIndex != -1 && linenum < linesperpage) {
        g.setColor(Color.black);
        nextIndex = text.indexOf("\n",nextIndex+1);
        if (nextIndex > 0) {
          line = new String(text.substring(currIndex,nextIndex));
          //text = new String(text.substring(nextIndex+1));
        }
        else {
          line = new String(text.substring(currIndex));
          text = null;
        }
        y = y + fontHeight;
        linenum = linenum + 1;
        g.drawString(line, x, y);
        currIndex = nextIndex;
        indexLocs = indexLocs+nextIndex+",";
      }
      System.out.println(indexLocs);
      if (startIndex != -1) {
        System.out.println("Printing page #"+pageIndex);
        return Printable.PAGE_EXISTS;
      }
      else return Printable.NO_SUCH_PAGE;
    }
    catch (Exception e) {
      System.out.println("Error occurred");
      System.out.println(e.getMessage());
      return Printable.NO_SUCH_PAGE;
    }
  }


  public void print() {
    try {
      job.print();
    }
    catch (PrinterException pe) {
      System.out.println("Printer error");
      System.out.println(pe.getMessage());
    }
  }


  public static void main(String[] args) {
    final PrintableArea pa = new PrintableArea();
    JFrame frame = new JFrame();
    JButton b = new JButton();
    b.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        pa.print();
      }
    });
    String longPage = "This is the first line\n"+
                      "This is the second line\n"+
                      "This is the third line\n"+
                      "4\n"+
                      "5\n"+
                      "56\n"+
                      "57\n"+
                      "58\n"+
                      "59\n"+
                      "10\n"+
                      "51\n"+
                      "52\n"+
                      "53\n"+
                      "54\n"+
                      "55\n"+
                      "56\n"+
                      "57\n"+
                      "58\n"+
                      "59\n"+
                      "20\n"+
                      "51\n"+
                      "52\n"+
                      "53\n"+
                      "54\n"+
                      "55\n"+
                      "56\n"+
                      "57\n"+
                      "58\n"+
                      "59\n"+
                      "30\n"+
                      "51\n"+
                      "52\n"+
                      "53\n"+
                      "54\n"+
                      "55\n"+
                      "56\n"+
                      "57\n"+
                      "58\n"+
                      "59\n"+
                      "40\n"+
                      "51\n"+
                      "52\n"+
                      "53\n"+
                      "54\n"+
                      "55\n"+
                      "56\n"+
                      "57\n"+
                      "58\n"+
                      "49\n"+
                      "50\n"+
                      "51\n"+
                      "52\n"+
                      "53\n"+
                      "54\n"+
                      "55\n"+
                      "56\n"+
                      "57\n"+
                      "58\n"+
                      "59\n"+
                      "60\n";
    pa.setText(longPage);
    frame.validate();
    frame.setSize(500,500);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(pa, BorderLayout.CENTER);
    frame.getContentPane().add(b, BorderLayout.NORTH);
    frame.setVisible(true);
  }
}
Technical Details
Message-ID: <75kbl4$7921@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <75i7jh$5gi4@forums.borland.com> <75j357$5ga10@forums.borland.com> <75jciv$6fp4@forums.borland.com> <367d5347.92639278@forums.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,5cf86e902afbf4b
X-Google-Attributes: gidf74b3,public
From: "Ross Dukeshier" <dukeman@flash.net>
Subject: Re: Anyone out there willing to help?
Date: 1998/12/20
Message-ID: <75kbl4$7921@forums.borland.com>
X-Deja-AN: 424365315
References: <75i7jh$5gi4@forums.borland.com> <75j357$5ga10@forums.borland.com> <75jciv$6fp4@forums.borland.com> <367d5347.92639278@forums.borland.com>
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.language