I made a program to mirroring of an image but the code below gives an error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.setDataElements(Unknown Source)
at java.awt.image.BufferedImage.setRGB(Unknown Source)
at algoritm.MirrorImage.applyAlgoritm(MirrorImage.java:43)
at ImageProcess.main(ImageProcess.java:36)
Here is the source code:
package algoritm;
import java.awt.image.BufferedImage;
public class MirrorImage implements Algoritm{
private BufferedImage bufferedImage;
private int width;
private int height;
//getter si setter
public MirrorImage(BufferedImage bufferedImage) {
this.bufferedImage = bufferedImage;
}
public BufferedImage getBufferedImage() {
return bufferedImage;
}
public void setBufferedImage(BufferedImage bufferedImage) {
this.bufferedImage = bufferedImage;
}
public void applyAlgoritm() {
width = bufferedImage.getWidth();
height = bufferedImage.getHeight();
for(int y = 0; y < height; y++){
for(int lx = 0, rx = width*2 - 1; lx < width; lx++, rx--){
int p = bufferedImage.getRGB(lx,y);
bufferedImage.setRGB(lx, y, p);
bufferedImage.setRGB(rx, y, p);
}
}
}
}
I think is something wrong with the second setRGB. If I comment on it, my error disappears, but the program does not do the right thing.