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

images printed incorrectly when using graphics object of PrintJob

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.3.0
    • client-libs
    • beta
    • x86
    • windows_nt



      Name: yyT116575 Date: 01/03/2001


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      The problem is associated with Windows NT and HP printers. The output on HP
      LaserJet 8100 Serier printer of the drawImage method is incorrect. It appears
      as if the PrinterGraphics obtained from PrinterJob was allocating a buffer for
      image printing. Images are printed to that buffer and only the bottom n lines
      get converted to a printer output, where n is the image height. This is fine if
      all images are of the same height. However, if we have an image of height, say,
      200 and another of height 150 the first image is printed correctly, while the
      second image has only the 100 bottom pixels of it printed, with the extra 50
      coming from the first image printed. To see problem in action please run the
      program included below:

      import java.awt.*;
      import java.awt.event.*;
      import java.util.*;

      /**
       * Tests image printing. It appears that images of different size are
       * printed incorrectly.
       *
       * @author Bo Majewski
       * @version 1.0
       */
      public class PrintTest extends Frame
          implements ActionListener, ComponentListener
      {
          /**
           * The canvas in the center of this frame.
           */
          ImageCanvas canvas;

          /**
           * Creates a new frame.
           */
          public PrintTest(String[] args)
          {
              super("Print test");
          }

          /**
           * Creates a few images and sets them on the image canvas.
           */
          public void init()
          {
              this.canvas = new ImageCanvas();

              // create a 48 x 24 image, then 48 x 32 and then 48 x 48
              Image img48x24 = this.createImage(48, 24);
              Image img48x32 = this.createImage(48, 32);
              Image img48x48 = this.createImage(48, 48);

              this.paintImage(img48x24, Color.blue);
              this.paintImage(img48x32, Color.red);
              this.paintImage(img48x48, Color.green);

              this.canvas.addImage(img48x24);
              this.canvas.addImage(img48x32);
              this.canvas.addImage(img48x48);

              Button printBtn = new Button("Print");
              printBtn.addActionListener(this);

              this.add(canvas, BorderLayout.CENTER);
              this.add(printBtn, BorderLayout.SOUTH);

              this.invalidate();
              this.validate();
          }

          /**
           * Paints a thick rectangle in the specified color in the
           * given image.
           */
          public void paintImage(Image img, Color color)
          {
              int imgWidth = img.getWidth(this);
              int imgHeight = img.getHeight(this);

              Graphics graphics = img.getGraphics();

              graphics.setColor(color);
              graphics.fillRect(0, 0, imgWidth, 5);
              graphics.fillRect(0, 0, 5, imgHeight);
              graphics.fillRect(imgWidth - 5, 0, 5, imgHeight);
              graphics.fillRect(0, imgHeight - 5, imgWidth, 5);

              graphics.setColor(Color.black);
              String text = imgWidth + " x " + imgHeight;

              graphics.drawString(text,
                  (imgWidth - graphics.getFontMetrics().stringWidth(text))/2,
                  imgHeight - 5);

              graphics.dispose();
          }

          /**
           * Causes the canvas to be printed.
           */
          public void actionPerformed(ActionEvent event)
          {
              this.canvas.print();
          }

          /**
           * Runs this application.
           */
          public static void main(String[] args)
          {
              PrintTest frame = new PrintTest(args);

              frame.setBounds(100, 100, 550, 400);
              frame.addComponentListener(frame);
              frame.setVisible(true);

              frame.addWindowListener(new WindowAdapter()
              {
                  public void windowClosing(WindowEvent event)
                  {
                      System.exit(0);
                  }
              });
          }

          public void componentResized(ComponentEvent e) {}
          public void componentMoved(ComponentEvent e) {}
          public void componentShown(ComponentEvent e) { this.init(); }
          public void componentHidden(ComponentEvent e) {}
      }

      /**
       * A simple image canvas that displays images.
       */
      class ImageCanvas extends Canvas
      {
          /**
           * Vector of images
           */
          Vector images = new Vector();

          /**
           * Adds a new image displayed by this component.
           */
          public void addImage(Image image)
          {
              this.images.addElement(image);
              this.repaint();
          }

          /**
           * Paints this component
           */
          public void paint(Graphics graphics)
          {
              this.paintImages(graphics, this.getSize());
          }

          /**
           * Updates the look of this component.
           */
          public void update(Graphics graphics)
          {
              this.paintImages(graphics, this.getSize());
          }

          /**
           * Paints the images held by this canvas.
           */
          protected void paintImages(Graphics graphics, Dimension size)
          {
              int x = 0, y = 10, height = 0, width = 0;

              for (int j = 0; j < 10; ++ j)
              {
                  for (int i = 0; i < this.images.size(); ++ i)
                  {
                      Image img = (Image) this.images.elementAt(i);

                      int imgWidth = 2*img.getWidth(this);
                      int imgHeight = 2*img.getHeight(this);

                      if (width + imgWidth + 10 >= size.width)
                      {
                          y += height;
                          x = 0;
                          height = 0;
                          width = 0;
                      }

                      graphics.drawImage(img, x, y, this);
                      x += imgWidth + 10;
                      width += imgWidth + 10;
                      height = Math.max(height, imgHeight);
                  }
              }
          }

          /**
           * Prints this canvas.
           */
          public void print()
          {
              PrintJob job = this.getToolkit().getPrintJob(
                  (Frame) this.getParent(), "Print test", new Properties());

              Graphics graphics = job.getGraphics();

              Dimension size = job.getPageDimension();

              size.width -= 50;
              size.height -= 50;

              graphics.translate(50, 50);
              this.paintImages(graphics, size);
              graphics.translate(-50, -50);

              graphics.finalize();
              graphics.dispose();

              job.end();
          }
      }
      (Review ID: 114514)
      ======================================================================

            ehawkessunw Eric Hawkes (Inactive)
            yyoungsunw Yung-ching Young (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: