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

DrawImage to Windows PrintJob graphics objects assumes default 72 DPI

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.6, 1.2.0, 1.2.2
    • client-libs
    • 2d
    • generic, x86
    • generic, windows_95, windows_nt



      Name: gsC80088 Date: 02/16/99


      DrawImage does not work under Java2.

      Bear with me while I tell you the whole story:

      I've been trying to find a way to get my graphics objects to scale properly to the printer. Since the printer
      coordinates are (for some strange reason) hard coded at 72DPI, I turned to searching the web for a way
       to print my precision graphics correcly,

      The answer I had been looking for was in the JAVA archives, under
      http://java.sun.com/products/java-media/mail-archive/2D/1985.html
      The article is from a SUN tech, Jim Graham. I implemented his suggestions, and, to my surprise, they
      worked in my 1.1 application. WOW! I thought, as I tried _THE SAME CODE_! under 1.2. She don't work.

      This 'patch-fix-solution' basically is to draw into a higher-resolution off-screen image, and submit that
      image to the printer. Should work, right? But only if the image isn't scaled before it goes to the printer!
      Why does Java2 scale it before sending it along, when JDK1.1 doesn't?

      I have submitted 2 files for you to reproduce THIS bug, drawApp.java, and drawTest.java. In DrawTest.java,
      you'll also see some commented lines of Graphics2D code, for use with an AffineTransform. If you uncomment
      these lines, you'll see that the transform that is SUPPOSED to take place, does not.

      Finally, I am testing on Windows98, Java versions 2 and 1.1.7B, with the Kawa IDE.


      -------DrawTest.java-----

      import java.awt.*;
      import java.io.*;
      import java.awt.event.*;
      //import java.awt.geom.*; // <-- uncomment for 1.2

      public class DrawTest extends Frame {

        MenuBar menuBar1 = new MenuBar();
        Menu menuFile = new Menu();
        MenuItem menuFilePrint = new MenuItem();
        MenuItem menuFileExit = new MenuItem();
        PrintJob Job; //Setup PrintJob
        public DrawTest() {
          try {
            setLayout(); //required to setup frame
          }
          catch (Exception e) {
            e.printStackTrace();
          }
        }

        public void setLayout() throws Exception{
          enableEvents(AWTEvent.WINDOW_EVENT_MASK);
          this.setSize(new Dimension(400, 400));
          this.setTitle("Test");
          this.setBackground(SystemColor.control);
          this.setLayout(null);
          menuFile.setLabel("File");
          menuFilePrint.setLabel("Print");
          menuFileExit.setLabel("Exit");
          menuFileExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              fileExit_actionPerformed(e);
            }
          });
          menuFilePrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              menuFilePrint_actionPerformed(e);
            }
          });
          menuFile.add(menuFilePrint);
          menuFile.addSeparator();
          menuFile.add(menuFileExit);
          menuBar1.add(menuFile);
          this.setMenuBar(menuBar1);
        }
      //File | Exit action performed
        public void fileExit_actionPerformed(ActionEvent e) {
          System.exit(0);
        }
        protected void processWindowEvent(WindowEvent e) {
          super.processWindowEvent(e);
          if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            fileExit_actionPerformed(null);
          }
        }

      //MY PRINT CODE:
        void menuFilePrint_actionPerformed(ActionEvent e) {
          Toolkit kit;
          Graphics pg ;
          kit = getToolkit();
          Job = kit.getPrintJob(this, "My Print Job", null);
          if(Job != null){
            pg = Job.getGraphics() ;
            if ( pg != null ) {
              this.printAll( pg );
              pg.dispose() ;
              Job.end();
            }
          }
          for( int i = 0; i < 1000; i++ ){
            //wait to dispose of job
          }
          Job = null;
        }

        public void printAll(Graphics g){
            int screenDPI ;
            int x, y, width, height ;
            Graphics cg ;
            try {
      // uncomment for 1.2 with affine transform:
      // AffineTransform at;
      // at = new AffineTransform( (300.0f/72.0f), 0, 0, (300.0f/72.0f), 0, 0 );

             int printerres = 300;
             int userw = 400;
             int userh = 200;
             int printerw = userw * printerres / 72;
             int printerh = userh * printerres / 72;
            
              Image img = createImage( printerw, printerh );
             cg = img.getGraphics();
             cg.setColor( Color.blue );
              cg.fillRect( 0, 0, printerw, printerh );
              cg.setColor( Color.white );
              cg.fillRect( 50, 0, 2, 100 );
              cg.setColor( Color.black );
              cg.fillRect( 0, 0, 1 * printerres / 72, 10 * printerres / 72 );
              cg.fillRect( 72 * printerres / 72, 0, 10 * printerres / 72, 10 * printerres / 72 );
              cg.fillRect( 300 * printerres / 72, 0, 10 * printerres / 72, 10 * printerres / 72 );

            
             g.drawImage(img, 0, 0, userw, userh, this);
             //the drawimage scales down before sending it to the printer, rather than letting
             //the printer do its own scaling.
              //((Graphics2D)g).drawImage( img, at, this);
            
            } finally {
              //cg.dispose();
            }
        }
      }
      ---- end DrawTest.java-----

      ---DrawApp.java---

      import java.awtt.*;


      public class DrawApp {

        public DrawApp() {
         DrawTest frame;
          frame = new DrawTest();
          frame.show();
        }

        public static void main(String[] args) {
          new DrawApp();
        }
      }
      ---end DrawApp.java----
      (Review ID: 53773)
      ======================================================================

      Name: gsC80088 Date: 03/04/99


      The quality of print is poor when an image is printed in
      Windows 95. If the same program is executed in Windows NT, then
      the quality of print is perfect.

      To demonstrate the bug please execute the following code and
      press on "Print" button:

      --------------------------------------------------------
      import java.awt.*;
      import java.awt.event.*;
      import java.util.*;
      import com.sun.java.swing.*;

      public class PrintImage extends JFrame implements ActionListener{
         ImageIcon ii;

         public static void main(String[] argc){
            PrintImage T=new PrintImage();
            T.setSize(new Dimension(200,200));
            T.setVisible(true);
         }

         public PrintImage(){
            this.setTitle("Print Image");
            getContentPane().setLayout( new BorderLayout());
            // replace "answer.gif" with your image file name.
            ii = new ImageIcon("answer.gif");

            Canvas c = new Canvas(){
                  public void paint(Graphics g)
                  {
                     g.drawImage(ii.getImage(), 0, 0, this);
                  }
               };
            getContentPane().add(c, BorderLayout.CENTER);
            JButton b=new JButton("Print");
            getContentPane().add(b, BorderLayout.SOUTH);
            b.addActionListener(this);
         }

         public void actionPerformed(ActionEvent e){
            PrintJob pj= getToolkit().getPrintJob(this,"Test!", new Properties());
            Graphics pg=pj.getGraphics();
            pg.drawImage(ii.getImage(), 0, 0, this);
            pg.dispose();
            pj.end();
         }
      }
      -------------------------------
      ======================================================================

      Name: skT88420 Date: 11/23/99


      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symcjit)

      PrintJob's resolution is always 72 dpi regardless of the printer's one.
      1/72 inch = 3.5mm which doesn't allow to print aligned & formatted text.
      (Review ID: 98194)
      ======================================================================

            prr Philip Race
            gstone Greg Stone
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: