When I make width equal or larger than height it works just as expected.
But once reversed it gives this out of bounds error and I just wondered if it could be a JAVA bug rather than this code.