Task:
"pass these four variables (x,y,w,h) into the getSubimage method of the original_image object."

That method returns a BufferedImage, which i need to directly return from my getImageRect method.

I have not included my whole code, but i was wondering if anybody could help me out with this? If you need me to include other aspects of my code to help understand this program then i can paste them.

BufferedImage getImageRect(int thisCol, int thisRow)
        {
            // if input is out of range, return "null"
            if(thisCol <0 || thisRow < 0)
            return null;
            if(thisCol>=numCols || thisRow>=numRows)
                return null;

            // otherwise, make and return the Rectangle
            int w = original_image.getWidth()/numCols;
            int h = original_image.getHeight()/numRows;
            int x = thisCol*w;
            int y = thisRow*h;

            ** BufferedImage subImage = new getSubImage(x, y, w, h); **
            return BufferedImage;
        }

**

This line of code was just my attempt at achieving this but am well aware it is not the correct way. Please can anyone give me some idea as to what the correct code would be for this line?**

The idea of my program is to uncover bits of a picture until you can guess who is beneath the tiles. Only 4 tiles can be uncovered at a time.

Recommended Answers

All 6 Replies

That looks 99% right!
getSubimage (note capitals - Java is case-sensitive) is a method in the BufferedImage class, so you need an instance of BufferedImage to call it, eg
myImage.getSubimage(0, 0, 100, 100) returns the top left 100x100 pixels of myImage as a new BufferedImage

Thanks, so do i need to remove line 15 and replace it with the code you suggested? Or keep line 15 but also add the instance as you suggested in your reply? (myImage.getSubimage(x,y,w,h))

        // otherwise, make and return the Rectangle
        int w = original_image.getWidth()/numCols;
        int h = original_image.getHeight()/numRows;

        int x = thisCol*w;
        int y = thisRow*h;

        BufferedImage subImage = new getSubimage(x,y,w,h);
        return BufferedImage;

I get two errors when i try to compile this...

error: cannot find symbol
    BufferedImage subImage = new getSubimage(x,y,w,h);                 


error: cannot find symbol
            return BufferedImage; 

Also i am unsure as to whether or not i am straying off the task at hand. The task i have to complete is as follows:

"Pass these four variables into the 'getSubimage' method of the 'original_image' object. That method returns a BufferedImage, which you can directly return from your 'getImageRect' method".

You are doing (trying to do) the right thing. You just need to get your syntax right on that one line. You are not calling a constructor, so the new keywod is wrong. It's jus a simple method call, like the example I gave you earlier.

Thanks for your support and help, you made things much clearer for me. I corrected the 2 lines as follows and, hey presto.... it compiles! :)

original_image.getSubimage(x,y,w,h);

        return original_image;

Hang on a momemnt...
I compiles, but it won't exactly do what you need.
When you call original_image.getSubimage(x,y,w,h); that creates a new BufferedImage containing just the sub-image. The original BufferedImage is unchanged. So you are returning the original image, not the subimage.
You need to use the sub image that the getSubimage() methods gives you. Your original code (lines 15,16) got that part right, it just had the wrong syntax after the = for calling the method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.