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

Graphics2D.drawString() methods failed for printing

    XMLWordPrintable

    Details

    • Subcomponent:
      2d
    • Resolved In Build:
      1.2.2
    • CPU:
      generic, x86
    • OS:
      generic, windows_95, windows_nt

      Description

      If the application uses the following methods to draw string for printer,
      nothing will be output, for both Win32/Solaris.

      drawString(String str, float x, float y)
      drawString(AttributedCharacterIterator iterator, int x, int y)
      drawString(AttributedCharacterIterator iterator, float x, float y)

      gstone@eng 1999-01-20

      Another user has reported that DrawString(String str, int x, int y) fails too
      but only after printing a capital 'I'.

      ------

      Name: gsC80088 Date: 03/14/99

      =20
      Printing text/html documents using JEditorPane is broken.=20
      To show that I wrote a small program that prints a document=20
      in three different ways: using a JTextArea, using a JEditorPane=20
      set to "text/plain" and finally using a JEditorPane set to=20
      "text/html". In the first two scenarios the text is properly=20
      printed but in the last one (html) the hardcopy result is=20
      completely non-sense. I am putting the program bellow but I=20
      think that it is easier if you download a complete test-kit=20
      including icons and html files at=20
      http://www.dca.fee.unicamp.br/~rodrigo/bug.zip

      The bug.zip file contains the following files:
        scenario1.ps - result for scenario 1 (JTextArea)
        scenario2.ps - result for scenario 2 (JEditorPane "text/plain")
        scenario3.ps - result for scenario 3 (JEditorPane "text/html")
        test.html - quite simple test html file
        TextFrame.java - test program
        *.class - class files
        image/print.gif - button icon

      Here is some additional information:
        "java -fullversion" result: JAVA.EXE full version "JDK-1.2-V"
        I am using the new symcjit.dll update
        I am using Win95

      My PhD project is completely frozen until some work around=20
      for this problem comes up. I hope to hear something from=20
      you soon=85

      Here is the test program:
      //=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D
      import java.lang.*;
      import java.awt.*;
      import java.beans.*;
      import java.awt.*;
      import java.awt.print.*;
      import java.awt.event.*;
      import java.util.*;
      import javax.swing.*;
      import javax.swing.border.*;
      import javax.swing.event.*;
      import javax.swing.table.*;
      import java.io.*;
      import java.net.*;
      import javax.accessibility.*;

      public class TextFrame extends JFrame implements Printable
      {

         //JTextArea textArea; //Enable this code line just for scenario 1 (using=
       JTextArea)
         JEditorPane textArea; //Enable this code line for scenarios 2 and 3 (JEd=
      itorPane with text/plain and text/html)

         // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
         // PPPPPP RRRRRR IIIIII NN NN TTTTTT =20
         // PP PP RR RR II NNN NN TT =20
         // PPPPPP RRRRRR II NNNNNN TT =20
         // PP RR RR II NN NNN TT =20
         // PP RR RR IIIIII NN NN TT =20
         // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
         public int print(Graphics g, PageFormat pf, int pi) throws PrinterExcept=
      ion
         {
            if (pi>1)
               return Printable.NO_SUCH_PAGE;

            Graphics2D g2 =3D (Graphics2D) g;

            //Setup Graphics2D font - is it necessary?
            Font f =3D Font.getFont ("Courier");
            g2.setFont (f);

            //Translate the printer graphics context
            //into the imageable area
            g2.translate(pf.getImageableX(), pf.getImageableY());
            textArea.print(g2);

            return Printable.PAGE_EXISTS;
         }

         // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
         // PPPPPP RRRRRR IIIIII NN NN TTTTTT CCCCCC AAAAAA LL LL =
         EEEEEE DDDDD =20
         // PP PP RR RR II NNN NN TT CC AA AA LL LL =
         EE DD DD =20
         // PPPPPP RRRRRR II NNNNNN TT CC AAAAAA LL LL =
         EEEEEE DD DD =20
         // PP RR RR II NN NNN TT CC AA AA LL LL =
         EE DD DD =20
         // PP RR RR IIIIII NN NN TT CCCCCC AA AA LLLLLL LLLLL=
      L EEEEEE DDDDD =20
         // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
         public void printCalled()
         {
            //Creates the printJob object
            PrinterJob printJob =3D PrinterJob.getPrinterJob();
            printJob.setPrintable(this);
            printJob.pageDialog(printJob.defaultPage());
            if (printJob.printDialog())
            {
               try
               {
                  //Print!!
                  printJob.print();
               } catch (Exception ex)
               {
                  ex.printStackTrace();
                  JOptionPane.showMessageDialog(getContentPane(),ex.getMessage(),=
      "Error!",JOptionPane.WARNING_MESSAGE);
               }
            }
         }


         // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
         // CCCCCC OOOOOO NN NN SSSSSS TTTTTT RRRRRR UU UU CCCCCC TTTTT=
      T OOOOOO RRRRRR =20
         // CC OO OO NNN NN SS TT RR RR UU UU CC TT =
         OO OO RR RR =20
         // CC OO OO NNNNNN SSSSSS TT RRRRRR UU UU CC TT =
         OO OO RRRRRR =20
         // CC OO OO NN NNN SS TT RR RR UU UU CC TT =
         OO OO RR RR =20
         // CCCCCC OOOOOO NN NN SSSSSS TT RR RR UUUUUU CCCCCC TT =
         OOOOOO RR RR =20
         // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
         public TextFrame(String fileName)=20
         {
      =09 super(fileName);
            WindowListener l =3D new WindowAdapter()=20
            {
      =09 public void windowClosing(WindowEvent e)=20
      =09 {=20
                  setVisible(false);
                  dispose();
               }
         =09};
         =09addWindowListener(l);

            try
            {
               UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()=
      );
            } catch (Throwable exc)=20
            {
      =09=09 // Error - unsupported L&F
               System.err.println("Unsupported LookAndFeel. ");
            }

            //=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D
            //Create toolbar
            JToolBar toolBar =3D new JToolBar();
            toolBar.setFloatable(false);
            final JButton printBT =3D addTool(toolBar, "print", "Print document")=
      ;
            class ToolListener implements ActionListener
            {
               public ToolListener()=20
               {
               }
               public void actionPerformed(ActionEvent e)
               {
                  if ((Object) printBT =3D=3D e.getSource())
                     printCalled();
               }
            }
            ToolListener toolListener =3D new ToolListener();
            printBT.addActionListener(toolListener);


            Dimension screenSize =3D Toolkit.getDefaultToolkit().getScreenSize();
            setSize(500,400);
            setLocation(screenSize.width/2 - 250, screenSize.height/2 - 200);

            String text =3D contentsOfFile(fileName);

            JPanel textWrapper =3D (JPanel) getContentPane();
         =09textWrapper.setLayout(new BorderLayout());
            textWrapper.setAlignmentX(LEFT_ALIGNMENT);

            //=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D scenario 1 - using JText=
      Area =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
      // textArea =3D new JTextArea(text);
            //=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D scenario 2 - using JEdit=
      orPane with text/plain =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
      // textArea =3D new JEditorPane("text/plain",text);=20
            //=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D scenario 3 - using JEdit=
      orPane with text/html =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
            try
            {
               textArea =3D new JEditorPane(getClass().getResource("test.html"));=
      =20
            } catch (Exception ex)
            {
               ex.printStackTrace();
               System.exit(0);
            }
            //=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

            JScrollPane scroller =3D new JScrollPane()=20
            {
               public Dimension getPreferredSize()=20
               {
                  return new Dimension(300,100);
               }
               public float getAlignmentX()=20
               {
                  return LEFT_ALIGNMENT;
               }
            };
            scroller.getViewport().add(textArea);
            textArea.setFont(new Font("monospaced", Font.PLAIN, 10));
            textArea.getAccessibleContext().setAccessibleName("Editable text area=
      ");

            textWrapper.add(toolBar, BorderLayout.NORTH);
            textWrapper.add(scroller, BorderLayout.CENTER);

            setVisible(true);
            validate();
            repaint();
          }

         public static String contentsOfFile(String filename)=20
         {
            String s =3D new String();
            File f;
            char[] buff =3D new char[500000];
            InputStream is;
            InputStreamReader reader;
            try=20
            {
               f =3D new File(filename);
               reader =3D new FileReader(f);
               int nch;
               while ((nch =3D reader.read(buff, 0, buff.length)) !=3D -1)=20
               {
                  s =3D s + new String(buff, 0, nch);
               }
            } catch (java.io.IOException ex)=20
            {
               s =3D "Could not load file: " + filename;
            }

            return s;
         }

         // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D
         // AAAAAA DDDDD DDDDD TTTTTT OOOOOO OOOOOO LL =20
         // AA AA DD DD DD DD TT OO OO OO OO LL =20
         // AAAAAA DD DD DD DD TT OO OO OO OO LL =20
         // AA AA DD DD DD DD TT OO OO OO OO LL =20
         // AA AA DDDDD DDDDD TT OOOOOO OOOOOO LLLLLL =20
         // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D
         /** Adds a button in a toolbar */
         public JButton addTool(JToolBar toolBar, String name, String tipText)=20
         {
            JButton b =3D (JButton) toolBar.add(new JButton(loadImageIcon("images=
      /" + name + ".gif",name)));
           =09b.setToolTipText(tipText);
         =09b.setMargin(new Insets(0,0,0,0));
            return b;
         }

         // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
         // LL OOOOOO AAAAAA DDDDD IIIIII MM MM AAAAAA GGGGGG EEEEE=
      E IIIIII CCCCCC OOOOOO NN NN SSSSSS =20
         // LL OO OO AA AA DD DD II MMMMMM AA AA GG EE =
           II CC OO OO NNN NN SS =20
         // LL OO OO AAAAAA DD DD II MM MM AAAAAA GG GGG EEEEE=
      E II CC OO OO NNNNNN SSSSSS =20
         // LL OO OO AA AA DD DD II MM MM AA AA GG GG EE =
           II CC OO OO NN NNN SS =20
         // LLLLLL OOOOOO AA AA DDDDD IIIIII MM MM AA AA GGGGGG EEEEE=
      E IIIIII CCCCCC OOOOOO NN NN SSSSSS =20
         // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
         public ImageIcon loadImageIcon(String filename, String description)=20
         {
            URL url;
            url =3D getClass().getResource(filename);
            if (url=3D=3Dnull)
               return null;
            return new ImageIcon(url, description);
         }

        =20
         // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D
         // MM MM AAAAAA IIIIII NN NN =20
         // MMMMMM AA AA II NNN NN =20
         // MM MM AAAAAA II NNNNNN =20
         // MM MM AA AA II NN NNN =20
         // MM MM AA AA IIIIII NN NN =20
         // =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
      =3D=3D=3D=3D=3D=3D=3D=3D=3D
         public static void main(String[] argv)
         {
            TextFrame report =3D new TextFrame("test.html");
            report.show();
            report.validate();
            report.repaint();
         }
        =20

      }
      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            flar Jim Graham
            Reporter:
            sherman Xueming Shen
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: