Graphics 2d not working in for loop why?

Its a part of pixel sorting algorithm .
I wanted to show how is it done but it just not working :/
PLZ help !
I found that if you delete if(kolor[x][y] < kolor[x+1][y]){(…)} its working but not sorting of course
Here is a code :

               for(int y=1;y<Y+1;y++)
                {
                    for(int x=1;x<X;x++)
                    {
                        ///////////////////////////////NOT WORKING
                        for(int q=1;q<Y+1;q++)
                        {
                            for(int w=1;w<X;w++)
                            {
                                g.setColor(new Color(redF[q][w],greenF[q][w],blueF[q][w]));
                                g.fillRect((q-1)*mnoznik,(w-1)*mnoznik,(1)*mnoznik,(1)*mnoznik);
                            }
                        }
                        ///////////////////////////////
                        if(kolor[x][y] < kolor[x+1][y])
                        {
                            int kolorR1 = redF[x][y];
                            int kolorG1 = greenF[x][y];
                            int kolorB1 = blueF[x][y];
                            int kolorR2 = redF[x+1][y];
                            int kolorG2 = greenF[x+1][y];
                            int kolorB2 = blueF[x+1][y];

                            redF[x][y] = kolorR2;
                            redF[x+1][y] =kolorR1;
                            blueF[x][y] = kolorB2;
                            blueF[x+1][y] =kolorB1;
                            greenF[x][y] = kolorG2;
                            greenF[x+1][y] =kolorG1;
                            kolor[x][y] = redF[x][y] * greenF[x][y] * blueF[x][y];
                            kolor[x+1][y] = redF[x+1][y] * greenF[x+1][y] * blueF[x+1][y];
                            x-=2;
                            if(x<1)x=1;

                        }
                    }
                }