Results 1 to 4 of 4
  1. #1
    sman865 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Question Flipping square box inside picture help

    Hi all, I have an assignment to create a method of the picture class that flips a square box within the picture around a horizontal or vertical line through the middle of the box. (here's an example: pic)

    I have worked for a total of 5 hours to only come up with code that does not work, and was wondering if anybody could tell me what I am doing wrong.

    The catch here is that I have to do it using the getPixel(x,y) method, nothing simpler will do.

    The test file for this program prompts an x, a y, and a size, and the picture repaints itself afterward. Here's the code:

    public void flipHorizontal(int x, int y, int size)
    {
    Pixel a,b,c;
    int half = size / 2;
    x = x - half;
    y = y - half;
    int y2 = y;
    int x2 = x;
    int end = size;
    int xend = x + half;
    int yend = y + half;
    int r,g,bl,r2,g2,b2;

    for (y=y2; y < yend; y++)
    {

    for(x = x2; x < xend+half; x++)
    {
    a = this.getPixel(x,y);
    b = this.getPixel(x,y+end);

    r = a.getRed();
    g = a.getGreen();
    bl = a.getBlue();

    r2 = b.getRed();
    g2 = b.getGreen();
    b2 = b.getBlue();

    a.setRed(r2);
    a.setGreen(g2);
    a.setBlue(b2);

    b.setRed(r);
    b.setGreen(g);
    b.setBlue(bl);


    end--;
    end--;
    }
    }
    }

    any help would be greatly appreciated!

    Thanks.
    Last edited by sman865; 10-16-2010 at 05:07 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Inside the inner for loop you have

    Java Code:
    a = this.getPixel(x,y);
    b = this.getPixel(x,y+end);

    If you interchange the colours of a and b won't you induce a *vertical* reflection rather than a horizontal one (as per the method name)? I would have expected the x-coord to be changed.

    Secondly you decrement end as you go around the inner loop. But you don't actually reset its value at the start of the outer loop which doesn't look right. Ie you decrement end as you work along a horizontal scan but because you don't reset it, it will have a crazy value for the second and subsequent horizontal scans.

    Finally I'm guessing that a Pixel instance is a reference to the pixels in the image so that setting their colour values will result in setting the colours of the original image. But that's only a guess because Pixel is not a standard Java class. (Why not just work with a BufferedImage since these things have get/setRGB() methods that do what you want?). Perhaps a small but runnable example would clarify things - along with a description of the actual behaviour you observe.

  3. #3
    sman865 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    I got it! FINALLY

    what helped me was you telling me to reset the value of end, thank you sooo much for that.

    The reason I am not using a BufferedImage is because my professor specifically said to use the getPixel method, which is inefficient, as you can tell.

    Thanks again.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. Replies: 4
    Last Post: 06-17-2010, 05:23 AM
  2. Replies: 1
    Last Post: 06-17-2010, 05:06 AM
  3. Replies: 1
    Last Post: 12-02-2009, 04:39 PM
  4. flipping memory cards if they are not matched...
    By yanipao in forum New To Java
    Replies: 8
    Last Post: 10-18-2009, 03:25 AM
  5. Rotating and flipping an image in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 09:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •