-1

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.

  • 1
    Did you try debugging your Programm? If not you can checkout this link on how it can help you: https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – M.Dan Dec 28 '18 at 15:49

2 Answers2

0

It seems image you are trying to modify is not resized. Try to instantiate a new clean buffered image with double width

At the first iteration here:

width = bufferedImage.getWidth();
rx = width*2 - 1;
  ...
  bufferedImage.setRGB(rx, y, p);

rx is out of bounds, try, in your constructor, to create a new clean image

BufferedImage newImage = new BufferedImage(2 * bufferedImage.getWidth(),  bufferedImage.getHieght(), BufferedImage.TYPE_INT_ARGB);

and mirror on top of this one, so in your loop

//read from the old one 
int p = bufferedImage.getRGB(lx,y);

// and write in the new one
newImage.setRGB(lx, y, p);
newImage.setRGB(rx, y, p);
sabau
  • 166
  • 1
  • 11
  • But BufferedImage It is not a predefined class? I did not make any constructor for it. – Vasilica Bogdan Dec 28 '18 at 16:50
  • I don't understand where i need to put the new image constructor. – Vasilica Bogdan Dec 28 '18 at 18:15
  • The third line of your code define a class `BufferedImage` and few lines underneath you've created a setter (sorry I wrote constructor because it could have been his constructor in my mind, but probably a static class/method will work even better here). public void setBufferedImage(BufferedImage bufferedImage) { this.bufferedImage = bufferedImage; this.mirroredImage = new BufferedImage(2* ...) } – sabau Jan 02 '19 at 09:20
0
setRGB(int x, int y, int rgb)
Sets a pixel in this BufferedImage to the specified RGB value.

I am not a regular java programmer but when I read the docs for setRgb() function as you can see above x and y holds the coordinates of the pixel where the rgb is the new pixel value. When I look at your for loops, in the second loop you have bufferedImage.setRGB(rx, y, p); in which you are trying to set rx as x value which is at the beginning is rx = width*2 - 1; and no doubts this exceeds the image width. So, I suppose you need to reconsider your algorithm to solve your problem.

Hasan
  • 1,243
  • 12
  • 27