Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4179886

Graphics context returned by PrintJob now includes a clipping region

XMLWordPrintable

    • beta
    • x86
    • windows_nt



      Name: tb29552 Date: 10/08/98


      /*
      With jdk1.1.6 on Windows NT, the position (0,0) in a graphics
      context returned from a PrintJob did not actually correspond
      to the upper left hand corner of the page, but the upper left
      hand corner of the region into which Windows would actually
      let you print. The result was that all your text actually appeared.

      The same output under jdk1.1.7 appears clipped. It looks indeed
      as if the text is being printed on the margins, but since Windows
      seems to reserve some space which you cannot print into, the result
      is clipped.

      */
      /*

      This example simply formats the contents of a file to a printer
      (N.B. does not expand tabs..)

      Usage:
          java PrintBug nameOfFile
      e.g.
          java PrintBug c:\temp\myjunk.text

      When the program is compiled and run under jdk1.1.6, the text is
      printed only into the region of the page that is effectively
      printed by the printer. Under jdk1.1.7, everything seems to be
      translated up and to the left, with the effect that the part of
      text is clipped.

      System: Windows NT 4 service pack 3
      Printers: HP LaserJet 4Si MX

      Program PrintBug.java follows:

      */
      import java.awt.*;
      import java.io.*;

      public class PrintBug {
          private PrintJob _pJob;
          private Frame _aFrame;

          public static void main(String[] argv) {
              if (argv.length < 1) {
                  System.out.println("Usage: java PrintBug someFile ");
                  System.out.println();
                  System.out.println(" e.g. java PrintBug c:\\temp\\myjunk.txt ");
                  System.exit(1);
              }
              FileReader fr;
              try {
                  fr = new FileReader(argv[0]);
                  LineNumberReader lnr = new LineNumberReader(fr);
                  (new PrintBug ()).printIt(lnr);
                  System.exit(0);
              } catch(FileNotFoundException e) {
                  System.out.println("No such file: " + argv[0]);
              }
          }

          public PrintBug () {
              Frame _aFrame = new Frame();

              /* Reference Bug IDs 4136947 and 4093775:
               * Even though the PrinterBug class has a Frame, it is
               * never displayed (ie. setVisible(true) is never called)
               * so the "default" font for the frame is never
               * initialized since that doesn't happen until addNotify
               * which isn't done until the peer is created and no peer
               * is ever created. Btw, this happens in both jdk 1.1.x
               * and jdk 1.2.
               */
              _aFrame.pack();
              _aFrame.setVisible(true);

              _pJob = _aFrame.getToolkit().getPrintJob(_aFrame, "Printed output ", null);
              if (_pJob == null) {
                  System.out.println("No print job..");
                  System.exit(0);
              }
          }

          protected void printIt(LineNumberReader lnr) {
              Graphics pg = _pJob.getGraphics();

              int leadingChars = 0;
              int leadingLines = 0;
              int trailingLines = 0;

              int pageNum = 1;
              int linesForThisPage = 0;
              int linesForThisJob = 0;

              String nextLine;

              int pageHeight = _pJob.getPageDimension().height;
              int pageWidth = _pJob.getPageDimension().width;
              // System.out.println("_pJob.getPageDimension().height returns
              // + pageHeight);
              // System.out.println("_pJob.getPageDimension().width returns "+
              // pageWidth);
              int fontSize = 12;
              Font fixed = new Font("Monospaced", Font.PLAIN, fontSize);
              FontMetrics fm = pg.getFontMetrics(fixed);
              // System.out.println("fm.charWidth(' ') returns " +fm.charWidth('
              // '));
              while (fm.charWidth(' ') * (132 + leadingChars) > pageWidth ||
                     fm.getHeight() * (66 + leadingLines +
                                       trailingLines) > pageHeight) {
                  fontSize--;
                  fixed = new Font("Monospaced", Font.PLAIN,
                                   fontSize);
                  fm = pg.getFontMetrics(fixed);
                  // System.out.println("fm.charWidth(' ') returns "
                  // +fm.charWidth(' '));
              }

              // Have to set the font to get any output!
              pg.setFont(fixed);
              fm = pg.getFontMetrics(fixed);
              int fontHeight = fm.getHeight();
              int fontDescent = fm.getDescent();
              int curHeight = leadingLines * fontHeight;
              int inPixels = leadingChars * fm.charWidth(' ');
              try {
                  do {
                      nextLine = lnr.readLine();
                      if (nextLine != null) {
                          if (curHeight + fontHeight *
                              (trailingLines + 1) > pageHeight){
                              // New Page
                              // System.out.println ("" + linesForThisPage + "
                              // lines printed for page " + pageNum);
                              pageNum++;
                              linesForThisPage = 0;
                              pg.dispose();
                              pg = _pJob.getGraphics();
                              if (pg != null) {
                                  pg.setFont(fixed);
                              }
                              curHeight = leadingLines *
                                  fontHeight;
                          }
                          curHeight += fontHeight;
                          if (pg != null) {
                              pg.drawString(nextLine,
                                            inPixels, curHeight - fontDescent);
                              linesForThisPage++;
                              linesForThisJob++;
                          } else {
                              System.out.println("pg null ");
                          }
                      }
                  } while (nextLine != null);
              } catch(EOFException eof) {
                  // Fine, ignore
              } catch(Throwable t) { // Anything else
                  t.printStackTrace();
              }
              // Send last page to the printer;
              pg.dispose();
              System.out.println("" + linesForThisPage + " lines printed for page " + pageNum);
              System.out.println("pages printed: " + pageNum);
              System.out.println("total lines printed: " +
                                 linesForThisJob);
              // Clean-up
              _pJob.end();
          }
      }

      (Review ID: 39709)
      ======================================================================

            dmendenhsunw David Mendenhall (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: