Extracting part of an image using BufferedImage and drawImage?

I've been trying for a while now to try to extract part of an image in IntelliJ. It has to be specified by the values of x, y, width and height. But I'm honestly unsure how to go about this since whatever way I try, I always seem to get some kind of error. After hours spent on coding and research online, I think that I need to use BufferedImage as well as drawImage. However, I was not never taught in class how to use these two (or even how to go about such task...) and that's why I'm asking for help here. Hopefully, somebody will be able to help me with this one. If anything is unclear then please let me know and I'll try to explain in better.
Code provided:

/**
 * Method to return a string with information about this picture.
 *
 * @return a string with information about the picture such as fileName,
 *         height and width.
 */
public String toString() {
    String output = "Picture, filename " + getFileName() +
            " height " + getHeight()
            + " width " + getWidth();
    return output;

}

public static void main(String[] args) {
    FileChooser.setMediaPath("mediasources/");
    String fileName = FileChooser.getMediaPath("butterfly1.jpg");
    Picture pictObj = new Picture(fileName);
    pictObj.explore();
}

What I'm thinking of using:

public BufferedImage extract (int x, int y, int w, int h) {

Also, the method has to pass the following tests:

public class extractTests {
@Test
public void comparePicturesTest1() {
    FileChooser.setMediaPath("mediasources/");
    String fileName = FileChooser.getMediaPath("butterfly1.jpg");
    Picture pictObj = new Picture(fileName);
    Picture out = pictObj.extract(30, 240, 100, 100);
    String fileName3 = FileChooser.getMediaPath("extract2.jpg");
    out.write(fileName3);
    String fileName1 = FileChooser.getMediaPath("extract1.jpg");
    Picture pict2 = new Picture(fileName1);
    String fileName2 = FileChooser.getMediaPath("extract2.jpg");
    Picture pict3 = new Picture(fileName2);
    comparePictures(pict2, pict3);
}

@Test
public void comparePicturesTest2() {
    FileChooser.setMediaPath("mediasources/");
    String fileName = FileChooser.getMediaPath("butterfly2.jpg");
    Picture pictObj = new Picture(fileName);
    Picture out = pictObj.extract(80, 50, 400, 300);
    String fileName3 = FileChooser.getMediaPath("extract4.jpg");
    out.write(fileName3);
    String fileName1 = FileChooser.getMediaPath("extract3.jpg");
    Picture pict2 = new Picture(fileName1);
    String fileName2 = FileChooser.getMediaPath("extract4.jpg");
    Picture pict3 = new Picture(fileName2);
    comparePictures(pict2, pict3);
}

private void comparePictures(Picture pict2, Picture pict3) {
    Pixel[] pixels1 = pict2.getPixels();
    Pixel[] pixels2 = pict3.getPixels();
    assertEquals(pixels1.length, pixels2.length);
    for(int i = 0; i < pixels1.length; i++) {
        Pixel pixel1 = pixels1[i];
        Pixel pixel2 = pixels2[i];
        assertEquals("At pixel (" + pixel1.getX() + ", " + pixel1.getY() + ")",
                pixel1.getColor(), pixel2.getColor());


      }
    }
}

Thanks in advance!

2 comments
Comment actions Permalink

http://programmers.stackexchange.com/ or http://stackoverflow.com/ would be a better place for the generic programming questions that are not specific to IntelliJ IDEA.

0
Comment actions Permalink

I'm doing this in intellij though, does that not make a difference? And thanks for the suggestions but I already have a thread on stackoverflow. :)

0

Please sign in to leave a comment.