6 Replies Latest reply: Mar 5, 2013 3:29 PM by 994982 RSS

    drawImage() question

    s3a
      For the drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) method.

      I am having trouble understanding the documentation for what sx1, sy1, sx2, and sy2 do.

      In my sample code, when sx2 = sy2 = 500, the image seems tiny whereas when sx2 = sy2 = 50, the image is large but it doesn't show all of it. Could someone explain to me the theory behind the "source coordinates" please?

      I am using the following sample code to practice:
      import java.awt.Graphics;
      import java.awt.image.BufferedImage;
      import java.io.File;
      import javax.imageio.ImageIO;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      
      
      public class BufferedImageTester extends JPanel
      {
          public BufferedImageTester()
          {
              JFrame frame = new JFrame("TheFrame");
              frame.add(this);
              frame.setSize(400,400);
              frame.setLocationRelativeTo(null);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setVisible(true);
          }
      
          protected void paintComponent(Graphics g)
          {
              try
              {
                 int dx1 = 0;
                  int dy1 = 0;
                  int dx2 = 100;
                  int dy2 = 100;
                  int sx1 = 0;
                  int sy1 = 0;
                  int sx2 = 100;
                  int sy2 = 100;
      
                  BufferedImage image = ImageIO.read(new File("C:\\Users\\Deniz\\Desktop\\strawberry.jpg"));
                  g.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, this);
              }
              catch(Exception e)
              {
      
              }
          }
      
          public static void main(String[] args)
          {
              new BufferedImageTester();
          }
      }
      Any input would be greatly appreciated!
      Thanks in advance!
        • 1. Re: drawImage() question
          793415
          Never try to read an image in <tt>paintComponent(Graphics)</tt> method! It should be stored as a class attribute.

          If that does not produce the expected result, post an SSCCE that hot-links to an image on the net (some choices at my media page) & I'll take a closer look.
          • 2. Re: drawImage() question
            DarrylBurke
            Moderator action: Moved from New to Java

            db
            • 3. Re: drawImage() question
              DarrylBurke
              From the API:
              Scaling of the image from source to destination is performed such that the first coordinate of the source rectangle is mapped to the first coordinate of the destination rectangle, and the second source coordinate is mapped to the second destination coordinate.
              The 'source' rectangle defines the subimage -- the rectangular portion of the loaded image -- that will be rendered.

              The 'destination' rectangle defines the rectangular region of the Graphics context where this subimage will be rendered (after being scaled/flipped as necessary).
              Source: 1, 1, 2, 2                     Destination: 2, 2, 4, 4
              
              +---------------------------------+     +---------------------------------+
              |            IMAGE                |     |         GRAPHICS CONTEXT        |
              |                                 |     |                                 |
              |     1                           |     |                                 |
              |   1 +---------+                 |     |                                 |
              |     |         |                 |     |                                 |
              |     |         |                 |     |                                 |
              |     |    █    |                 |     |                                 |
              |     |         |                 |     |                                 |
              |     |         |                 |     |               2                 |
              |     +---------+ 2               |     |              2 +----------------+
              |               2                 |     |                |                |
              |                                 |     |                |                |
              |                                 |     |                |                |
              |                                 |     |                |                |
              |                                 |     |                |       ██       |
              |                                 |     |                |       ██       |
              |                                 |     |                |                |
              |                                 |     |                |                |
              |                                 |     |                |                |
              |                                 |     |                |                |
              +---------------------------------+     +----------------+----------------+ 4
                                                                                        4
              db
              • 4. Re: drawImage() question
                s3a
                Ok but I'm having trouble getting why changing sx2 and sy2 makes the image larger (not just talking about the zooming in since I expected that) if the destination coordinates serve as boundaries for the source coordinates. The way I see it is that the image should be the same exact size regardless of the source coordinates (assuming the destination coordinates remain the same) except zoomed in or scaled somehow. Is my confusion making sense or do I need to give more information?
                • 5. Re: drawImage() question
                  s3a
                  I think I had an epiphany based on everything I've read and been told but need to confirm if I am right or wrong since this is one of those things I just had trouble understanding at first (assuming I get it now).

                  Ok so this is how I think this drawImage() method works (please tell me if I am right or wrong):

                  The source coordinates are the dimensions that will be taken of the image.
                  Example: This image is 80x80 so if I choose sx2 = sy2 = 80 (where sx1 = sy1 = 0) and make the dx2 = frame.getWidth() and dy2 = frame.getHeight() (where dx1 = dy1 = 0) then that will just stretch the image since it then binds the source coordinates to the destination coordinates.

                  Or if I simply want to zoom in and by implication take the same amount of space on the JPanel (which is 80x80) then I just do dx2 = dy2 = 80 (where dx1 = dx2 = 0) and let's say sx2 = sy2 = 40 (where sx1 = sy1 = 0).

                  Edit: Darryl Burke, I misunderstood something and I think I'm right and I also think that that's exactly what you said. :$
                  • 6. Re: drawImage() question
                    994982
                    Thx Deniz That saved me :) i was kicking my comp because of this sx dx. :)