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

PrintJob.getGraphics().drawString(String,int,int) doesn't work: 1.1 printing api

XMLWordPrintable

    • 2d
    • x86
    • windows_nt



      Name: diC59631 Date: 09/17/98

      =20
      Hello, I was developped with Java 1.1.4, 1.1.5 and 1.1.6,
      but I found a problem when I tried to print a document with
      severals pages. I found the BUG in Java Developer Connection=20
      (bug id 4084038) that was fixed in releases 1.1.7 and 1.2beta4.
      I downloaded the Java 1.2beta4 because the 1.1.7 is not available,
      and when I test my application I found another BUG: using=20
      Graphics.drawString(String,int,int) the JVM draws a line from each
      character printed to the upper-left corner of the page.
      Here is a simple code that show the problem when run in Windows NT 4.0:
      There are 2 (two) files, MyPrintTest.java and MyPrintTestWindow.java

      // begin file: MyPrintTest.java
      import java.awt.Dimension;
      import java.awt.Graphics;
      import java.awt.Font;
      import java.awt.FontMetrics;
      import java.awt.PrintJob;
      import java.awt.Toolkit;
      import java.util.Properties;

      public class MyPrintTest {

         private Toolkit toolkit;
         private PrintJob printJob;
         private Graphics page;
         private Dimension pageSize;
         private int pageDpi;

         private Font datosFontPlain;
         private Font datosFontBold;
         private Font titleFont;

         public void freeResources() {
            toolkit =3D null;
            printJob =3D null;
            page =3D null;
            pageSize =3D null;
            pageDpi =3D 0;
            datosFontPlain =3D null;
            datosFontBold =3D null;
            titleFont =3D null;
         }

         public MyPrintTest( PrintJob job, Toolkit t ) {
            toolkit =3D t;
            printJob =3D job;
            datosFontPlain =3D new Font("Times New Roman",Font.PLAIN,12);
            datosFontBold =3D new Font("Times New Roman",Font.BOLD ,12);
            titleFont =3D new Font("Times New Roman",Font.BOLD ,14);
         }

         protected void newPage() {
            pageSize =3D printJob.getPageDimension(); // query the page size
            pageDpi =3D printJob.getPageResolution(); // query the page resolut=
      ion
            System.out.println( "pageSize: "+ pageSize );
            System.out.println( "pageDpi : "+ pageDpi );
            page =3D printJob.getGraphics();
         }

         public void print() {
            newPage();
            page.setFont( datosFontPlain );
            writeCenter( "writeCenter", 100 );
            writeLeft ( "writeLeft" , 300 );
            writeRight ( "writeRight" , 500 );
            page.dispose();
         }

         protected int writeCenter( String stng, int fil ) {
            int col;
        =20
            FontMetrics fMetric =3D page.getFontMetrics();
            int stngW =3D fMetric.stringWidth( stng );
            col =3D (pageSize.width - stngW) / 2;
            page.drawString( stng, col, fil );
            return stngW;
         }

         protected int writeLeft( String stng, int fil ) {
            return writeRight( stng, 0, fil );
         }
                 =20
         protected int writeRight( String stng, int fil ) {
            return writeLeft( stng, pageSize.width, fil );
         }

         protected int writeCenter( String stng, int col, int fil ) {
            int realCol;

            FontMetrics fMetric =3D page.getFontMetrics();
            int stngW =3D fMetric.stringWidth( stng );
            realCol =3D col - (stngW / 2);
            page.drawString( stng, realCol, fil );
            return stngW;
         }

         protected int writeLeft( String stng, int col, int fil ) {
            int realCol;

            FontMetrics fMetric =3D page.getFontMetrics();
            int stngW =3D fMetric.stringWidth( stng );
            realCol =3D col - stngW;
            page.drawString( stng, realCol, fil );
            return stngW;
         }

         // For compatibility...
         protected int writeRight( String stng, int col, int fil ) {
            FontMetrics fMetric =3D page.getFontMetrics();
            int stngW =3D fMetric.stringWidth( stng );
            page.drawString( stng, col, fil );
            return stngW;
         } =20
      }

      // eof: MyPrintTest.java


      // begin file: MyPrintTestWindow.java
      import java.awt.Dimension;
      import java.awt.Graphics;
      import java.awt.Font;
      import java.awt.FontMetrics;
      import java.awt.PrintJob;
      import java.awt.Toolkit;
      import java.util.Properties;
      =20
      public class MyPrintTestWindow extends java.awt.Frame
      {
         public MyPrintTestWindow() {
            super();
         }

         public MyPrintTestWindow(String title) {
            super(title);
         }

         public boolean printTest() {
           =20
            setFont( new Font( "Times New Roman", Font.PLAIN, 12 ) );
            Properties printprops =3D new Properties();
           =20
            Toolkit toolkit =3D this.getToolkit();
            PrintJob job =3D toolkit.getPrintJob( this, "MyPrintTest", printprops=
       );
            if ( job =3D=3D null ) {
               return false;
            }=09
            try {
               MyPrintTest msgprinter =3D new MyPrintTest( job, toolkit );
               msgprinter.print();
               msgprinter.freeResources();
               job.end();
            } catch( Throwable e ) {
               e.printStackTrace( System.out );
               return false;
            }
            return true;
         }

         public static void main(java.lang.String[] args) {
            try {
               MyPrintTestWindow ptWin =3D new MyPrintTestWindow();
               ptWin.printTest();
            } catch (Throwable exception) {
               exception.printStackTrace( System.out );
            }
         }
      }

      // eof: MyPrintTestWindow.java
      (Review ID: 35951)
      ======================================================================

            prr Philip Race
            dindrigo Daniel Indrigo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: