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

RFE: Java 2D printing: API to query a printer's physical printable paper area

XMLWordPrintable

    • 2d
    • generic, x86
    • generic, windows_95, windows_nt



      Name: dbT83986 Date: 12/11/98


      (OOPS! Just sent this a moment ago with the wrong program. This one has the correct file at the end.)
      When the Print option is selected from the File drop down on the menu bar of the following program the print dialog is presented to the user. On selecting OK a page is printed to the selected printer. The page contains a black box printed using the margins returned from the pageformat object, for the print job. The problem lies in how the graphics object maps the coordinated to the device coordinates for the printer selected. The coordinate system for most printers, is contained within the ?printable? area of the page, which is something less that the page size. The result of this program places the margin on the page offset from where it should be. The graphics object does not account for the unprintable area of the device selected. This could be compensated for, before the object is rendered, by the graphics object, if there were a mechanism for retrieving device specific printer properties; but I have been unable to locate anything in the documentation. If there is no !
       me!
      chanism within the Java classes then I would say that this is a defect. If there is no intent to provide a mechanism then I would say that there is a defect in the documentation and this problem should be spelled out, perhaps with samples on how to solve this using the native interface, or something else.

      The driver used with this code is Microsoft?s HP LaserJet5 unidriver Version 3.37, OS is NT4.0 (service pack 3)

      import java.awt.*;
      import java.awt.event.*;
      import java.awt.print.*;
      import java.awt.print.PrinterJob;
      import java.awt.print.PageFormat;
      import java.net.*;
      import java.io.*;
      import java.util.*;

      import javax.swing.*;


      public class PrintMarginDemo
        extends JFrame implements Printable
        {

        // ****** references ******
      PrinterJob printJob;
        PageFormat pageFormat;
        JTextArea mainText; // reference for "mainText" the main area where the text is displayed.
        JScrollPane scrollPane; // reference for the "scrollPane" into which the text Area is added.

      JLabel status; // reference for "status" a text label at the bottom of the window.
        JMenuBar mb; // reference to a menu bar which will exist at the top of the menu
        JMenu fm,hm; // references to items on the menu bar. "fm" = file, "hm" = help menu

        // ****** Constructor ******
       PrintMarginDemo()
       {
      super("PrintMarginDemo");

        JMenuItem mi;

        printJob = PrinterJob.getPrinterJob();
        pageFormat = printJob.defaultPage();

        mb = new JMenuBar();

      setJMenuBar(mb);


        // ****** ActionListeners ******

        // For the File/Clear item
        ActionListener mClearMainText = new ActionListener()
        {
         public void actionPerformed(ActionEvent evt)
         {
          mainText.setText(""); // delete any text in there
          showStatus("Clear Text");
        }
        };

        // For the File/Print Setup item
        ActionListener mPrintSetup = new ActionListener()
        {
         public void actionPerformed(ActionEvent evt)
         {
          formatData();
          pageFormat = printJob.pageDialog(pageFormat);
          formatData();
        }
        };


        // For the File/Print item
        ActionListener mPrint = new ActionListener()
        {
         public void actionPerformed(ActionEvent evt)
         {
          showStatus("Print item");
          printIt();
        }
        };


        // For the File/Exit item
        ActionListener mExit = new ActionListener()
        {
         public void actionPerformed(ActionEvent evt)
         {
          shutDown();
         }
        };

        // For the Window 'X'
         WindowAdapter exitWin = new WindowAdapter()
         {
      public void windowClosing(WindowEvent e)
          {
      shutDown();
      }
      };


      // Now build the file Menu. (Need to figure out accelerator keys)
      fm = new JMenu("File");
      fm.add(mi = new JMenuItem("Clear",KeyEvent.VK_C));
      mi.addActionListener(mClearMainText);
        fm.add(mi = new JMenuItem("Print Setup",KeyEvent.VK_S));
        mi.addActionListener(mPrintSetup);
      fm.add(mi = new JMenuItem("Print",KeyEvent.VK_P));
        mi.addActionListener(mPrint);
      fm.addSeparator();
      fm.add(mi = new JMenuItem("Exit",KeyEvent.VK_X));
      mi.addActionListener(mExit);
      mb.add(fm);


      //Add components to the frame window, using the default ContentPane and BorderLayout.
        // of the frame.
        mainText = new JTextArea("",24,40);
        mainText.setEditable(false);
        scrollPane = new JScrollPane(mainText);

        getContentPane().add(scrollPane, BorderLayout.CENTER);
       
        //Add a status message area to the bottom of the main frame.
        status = new JLabel("Status");
        getContentPane().add(status, BorderLayout.SOUTH);
       
      pack();
       
      addWindowListener(exitWin);
       }

       public void formatData()
       {
        mainText.append("Test\n");
        mainText.append("pageWidth " + pageFormat.getWidth() + "/72nds or " + pageFormat.getWidth()/72 + "in.\n");
        mainText.append("pageHeight " + pageFormat.getHeight() + "/72nds or " + pageFormat.getHeight()/72 + "in.\n");
        mainText.append("ImagableWidth " + pageFormat.getImageableWidth() + "/72nds or " + pageFormat.getImageableWidth()/72 + "in.\n");
        mainText.append("ImagableHeight " + pageFormat.getImageableHeight() + "/72nds or " + pageFormat.getImageableHeight()/72 + "in.\n");
        mainText.append("ImagableX " + pageFormat.getImageableX() + "/72nds\n");
        mainText.append("ImagableY " + pageFormat.getImageableY()+ "/72nds\n");
        mainText.append("Orientation " + pageFormat.getOrientation() + "\n");
       }

       public int print(Graphics g, PageFormat pf, int pi) throws PrinterException
       {
        Graphics2D g2 = (Graphics2D) g;

        FontMetrics fmetrics = g2.getFontMetrics();
        Font font = fmetrics.getFont();
        int lineHeight = fmetrics.getHeight();

        double width = pf.getImageableWidth();
        double height = pf.getImageableHeight();
        int x = (int)pf.getImageableX();
        int y = (int)pf.getImageableY();

        if(pi > 0) return Printable.NO_SUCH_PAGE;
        else
        {
         g2.setColor(Color.black);
         g2.drawRect(x,y,(int)width,(int)height);

         y = y + fmetrics.getAscent();

         g2.drawString("Hello", x, y);
         y = y + lineHeight;
         return Printable.PAGE_EXISTS;
        }
       }

        
       
       public void printIt()
       {
        PrinterJob printJob = PrinterJob.getPrinterJob();
         printJob.setPrintable(this,pageFormat);
         if(printJob.printDialog())
         {
          try
          {
           printJob.print();
          }
          catch (Exception ex)
          {
           ex.printStackTrace();
          }
         }
       }

       /** shutDown ** this is called to shut down the program.*/
       public void shutDown()
       {
        PrintMarginDemo.this.setVisible(false);
      PrintMarginDemo.this.dispose();
      System.exit(0);
       }

      /** Simulate applet.showStatus() for Frame-based applications */
      public void showStatus(String msg) {
      if (msg == null)
      msg = "";
      status.setText(msg);
      }

       // This is the main entry point (method) for execution of the program.
       public static void main(String av[])
       {
      new PrintMarginDemo().setVisible(true);
       }
      }
      (Review ID: 47283)
      ======================================================================

            prr Philip Race
            dblairsunw Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: