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

Text printing is misaligned

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.3.0
    • client-libs
    • 2d
    • merlin
    • x86
    • windows_2000



      Name: pa48320 Date: 01/18/2002



        I am attempting to print out 4 segments of text:
          "package", " ", "myfirstpackage1", ";"
        (minus quotes and separators) on the same line, font size,
        and font style, but am not able to get the alignment of the
        text correct. In certain font sizes, the final semicolon (;)
        ends up overwriting the last part of "myfirstpackage1".
          My sample program draws each run of text using
        graphics.drawString(...), and computes the x-position for
        the next run of text using fontMetrics.stringWidth(). I
        have also tried fontMetrics.getStringBounds(...), but that
        makes no difference.
          I have included a sample program which puts up a JFrame
        of the text I am trying to print, followed by a print job
        submission. The output on the screen is correctly aligned,
        but the printed output is mis-aligned.

      Impact:
        With the misalignment, certain fonts and/or sizes do not
        print out correctly from the code editor in
        Oracle JDeveloper 9i, so this bug has a visible product
        impact. There appears to be no workaround for this problem.

      Environment:
        Compaq Armada M700, 850Mhz, 448MB RAM
        Windows XP Professional w/ SP1
        JDK 1.3:
          java version "1.3.1"
          Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
          Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
        Using XP drivers for HP DeskJet 895cxi

        Print Server:
          Dell Optiplex GX1, 400 MHz, 384MB RAM
          Windows 2000 Server w/ SP2
          HP DeskJet 895cxi

      Workaround:
        None, assuming my code is written correctly.

      Testcase:
      import java.awt.*;
      import java.awt.print.*;
      import javax.swing.*;

      public class PrintTest extends Canvas implements Printable
      {
        public void paint( Graphics graphics )
        {
          // This will print 12 lines containing:
          // package mypackage1;
          // with the respective font styles per size/line
          String[] STRINGS = new String[]
          {
            "package",
            " ",
            "myfirstpackage1",
            ";"
          };
          int[] STYLES = new int[]
          {
            Font.PLAIN,
            Font.PLAIN,
            Font.PLAIN,
            Font.PLAIN
          };
          int[] SIZES = new int[]
          {
            5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
          };

          graphics.setColor( Color.black );
          int yPos = 0;
          for ( int i = 0; i < SIZES.length; i++ )
          {
            int size = SIZES[i];
            
            int xPos = 0;
            for ( int j = 0; j < STRINGS.length; j++ )
            {
              String string = STRINGS[j];
              int style = STYLES[j];

              Font font = new Font( "DialogInput", style, size );
              FontMetrics metrics = graphics.getFontMetrics( font );
              graphics.setFont( font );

              int fontAscent = metrics.getAscent();
              graphics.drawString( string, xPos, yPos + fontAscent );

              xPos += metrics.stringWidth( string );
            }

            // After each line, advance the yPos - just use the last metrics
            // we drew with
            FontMetrics lastMetrics = graphics.getFontMetrics();
            int fontHeight = lastMetrics.getHeight();
            yPos += fontHeight;
          }
        }

        public int print( Graphics graphics, PageFormat pageFormat, int pageIndex )
      throws PrinterException
        {
          if ( pageIndex != 0 )
          {
            return NO_SUCH_PAGE;
          }

          int visibleX = (int) pageFormat.getImageableX();
          int visibleY = (int) pageFormat.getImageableY();
          graphics.translate( visibleX, visibleY );
          paint( graphics );

          return PAGE_EXISTS;
        }


        public static void main( String[] args )
        {
          JFrame frame = new JFrame( "Print Test" );
          frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
          PrintTest painter = new PrintTest();
          frame.getContentPane().add( painter );
          frame.setSize( 800, 700 );
          frame.setVisible( true );

          try
          {
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintable( painter );
            if ( job.printDialog() )
            {
              job.print();
            }
          }
          catch( PrinterException e )
          {
            System.out.println( "print error: " + e );
          }
        }
      }
      (Review ID: 138465)
      ======================================================================

            busersunw Btplusnull User (Inactive)
            pallenba Peter Allenbach (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: