1

So I have to use java.awt.color to flip some imported images.

Here is my primary method to achieve flipping an image over the vertical axis:

  public void execute (Pixmap target)
  {
    Dimension bounds = target.getSize();
    // TODO: mirror target image along vertical middle line by swapping
    //       each color on right with one on left

    for (int x = 0; x < bounds.width; x++) {
      for (int y = 0; y < bounds.height; y++) {

        // new x position
        int newX =  bounds.width - x - 1;

        // flip along vertical 
        Color mirrorVert = target.getColor(newX, y);
        target.setColor(x, y, new Color (mirrorVert.getRed(),
                              mirrorVert.getGreen(),
                              mirrorVert.getBlue()));
      }
    }
  }

However, when this executes, instead of the image, flipping, I get something like this:

Original: Original

"Flipped":"Flipped"

Thank y'all for the help

JFPicard
  • 5,029
  • 3
  • 19
  • 43
TylerA
  • 21
  • 6
  • Well, you are not removing the previous color – Murat Karagöz Dec 01 '15 at 15:24
  • 4
    "mirror target image along vertical middle line by swapping each color on right with one on left" - you basically missed the "swapping" part – zubergu Dec 01 '15 at 15:25
  • 1
    You're replacing the colors inplace and hence when you read pixels from the left they've already been overwritten. It's like "swap x = 4 and y = 2" and you do `y = x; x = y`. You'll end up with both having the value 4 (y = 4, x = y = 4). – Thomas Dec 01 '15 at 15:27
  • @MuratK. so how would I go about removing the previous color? – TylerA Dec 01 '15 at 15:37
  • @zubergu how did I miss the swapping part? I thought that when I set the NewX coordinate, I was swapping – TylerA Dec 01 '15 at 15:37
  • Why are you creating a new `Color` object exactly the same as an existing color? You should be able to treat them as immutable without any issues. Also take a look at [swapping variables](http://stackoverflow.com/questions/10393690/is-it-possible-to-swap-two-variables-in-java) – phflack Dec 01 '15 at 15:49
  • Nice example of some archetype of error that happens frequently. Some more basic example: You have a collection of objects, where, by mistake, the names "John" and "Charles" have been exchanged. To fix it, a script runs to replace all "John" by "Charles" and then another on that replaces all "Charles" by "John". No big surprise, no "Charles" will be found. – Gyro Gearloose Feb 16 '16 at 20:05

1 Answers1

0
// flip along vertical 
        Color mirrorVert = target.getColor(newX, y);
        target.setColor(x, y, new Color (mirrorVert.getRed(),
                              mirrorVert.getGreen(),
                              mirrorVert.getBlue()));
       target.setColor(newX, y , new Color(255,255,255));
      }

This should work in theory. Basically the idea is to set the Colors on the right side to white, while copying it over to the left side.


Actually this will not work... So what you could do is to save the new colors in an array. Then you can make everything white and put the swapped colors back.


for (int i=0;i<image.getWidth();i++)
        for (int j=0;j<image.getHeight();j++)
        {
            int tmp = image.getRGB(i, j);
            image.setRGB(i, j, image.getRGB(i, image.getHeight()-j-1));
            image.setRGB(i, image.getHeight()-j-1, tmp);
        }

That one looks promising.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107