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

Clipping in translated Image Graphics isn't correct

XMLWordPrintable

    • 2d
    • kestrel
    • x86, sparc
    • solaris_7, windows_95



      Name: bb33257 Date: 03/16/99


      The following program displays two Images. One was produced
      with a clipped Graphics; the other with a translated and clipped
      Graphics. The Images should be identical but they aren't. It
      looks like clipping is applied to the wrong portion of the Image.

      import java.awt.event.*;
      import java.awt.*;
      import java.awt.geom.Rectangle2D;

      /**
       * This test exposes a bug in JDK 1.2. If you get a Graphics from an
       * Image, translate the Graphics and then set clipping on the Graphics,
       * the wrong area of the graphics gets clipped.
       *
       * The strategy used in this test is to translate a Graphics by (-dx, -dy),
       * set a clipping region at (dx, dy), and then draw something at (dx, dy).
       * If the output changes with changes in dx or dy, there is a bug.
       */
      public final class ClipBug extends Component {

          private static final int squareSize = 80;

          /**
           * This method draws a red square inside of a blue square.
           * The topleft corner of the blue square is at (0, 0). In
           * a correctly functioning Graphics, dx and dy should not affect
           * the result of this method at all.
           */
          private void drawIt(Graphics2D g,
                              int dx,
                              int dy) {

              g.translate(-dx, -dy);

              g.setColor(Color.blue);
              g.fillRect(dx, dy, squareSize, squareSize);

              g.setColor(Color.red);

              // This clipping should not affect the appearance of
              // the image, but it does if dx and dy are nonzero.
              g.setClip(new Rectangle2D.Float(dx, dy, squareSize, squareSize));

              g.fillRect(dx, dy, squareSize, squareSize);
          }

          /**
           * Return an Image with the squares draw in it. (dx, dy) is the
           * amount to translate by - their values should not affect the
           * outcome.
           */
          private Image makeImage(int dx, int dy) {

              Image image = createImage(squareSize, squareSize);
              Graphics2D g = (Graphics2D) image.getGraphics();
              drawIt(g, dx, dy);
              return image;
          }

          public void paint(Graphics g) {

              Image buffer = makeImage(0, 0);
              g.drawImage(buffer, 10, 10, this);

              buffer = makeImage(60, 40);
              g.drawImage(buffer, 30 + squareSize, 10, this);
          }

          public static void main(String[] args) {

              Frame f = new Frame("You shouldn't see any blue");
              f.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });
              f.add(new ClipBug());
              f.setSize(50 + 2*squareSize, 60 + squareSize);
              f.show();
          }
      }

      (Review ID: 55671)

      ======================================================================

            flar Jim Graham
            bcbeck Brian Beck (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: