Chess effect on an image using JavaFX

0

I'm creating a function using JavaFX so that it uses two images and the size of the pixels and returns a third image containing a chess effect from the other two images.

Below is the code,

public Image xadrez(Image img1, Image img2, int tamanho) {
int larg = (int) img1.getWidth();
int alt = (int) img1.getHeight();

WritableImage imgRes = new WritableImage(larg, alt);
PixelReader pr = img1.getPixelReader();
PixelReader pr2 = img2.getPixelReader();
PixelWriter pw = imgRes.getPixelWriter();

for (int x = 0; x < larg; x++) {
    for (int y = 0; y < alt; y++) {
        if ( x % 2 == y % 2)
            pw.setArgb(x, y, pr.getArgb(x, y));
        else
            pw.setArgb(x, y, pr2.getArgb(x, y));
    }
} 
return imgRes;

}

The whole problem is that I'm not able to arrange a way to put the size of the pixels, the above code returns an image with the effect chess but using only 1 pixel of each image at a time. What I would like to do is use the variable size to get a larger group of pixels, such as 10 pixels at a time.

    
asked by anonymous 09.04.2016 / 22:18

1 answer

1

One way of doing this, assuming that "size" fits the two dimensions of each square of chess, is as follows:

for (int x = 0; x < larg; x++) {
    for (int y = 0; y < alt; y++) {
        if ( (((x/tamanho) % 2 == 0) && ((y/tamanho) % 2 == 0)) || 
             (((x/tamanho) % 2 == 1) && ((y/tamanho) % 2 == 1)) )
            pw.setArgb(x, y, pr.getArgb(x, y));
        else
            pw.setArgb(x, y, pr2.getArgb(x, y));
    }
} 

===========

(edited) Another way, less clear, but more elegant:

    for (int x = 0; x < larg; x++) {
        for (int y = 0; y < alt; y++) {
            if ( ((x/tamanho) % 2 == (y/tamanho) % 2 ) ) 
                pw.setArgb(x, y, pr.getArgb(x, y));
            else
                pw.setArgb(x, y, pr2.getArgb(x, y));
        }
    } 
    
10.04.2016 / 16:32