logic error? in my basic parallax code

I’m trying to do some simple background scrolling for several layers of background images, where each layer scrolls more slowly than the one above. I’m working with the modulous operator and the player’s location… What I’ve written seems correct in theory but I’m getting inconsistant results – basically, the background scrolls at varying rates of speed when I know that the player (and objects in the foreground) are moving uniformly.

here’s the relevant code:


      public void setBackground(Image background[]) {
            this.background = background;
      }
      
      private void paintBackground(Graphics g) {
            if(background == null) return;
            for(int layer = 0; layer < background.length; layer++) {
                  int layerWidth = background[layer].getWidth(this);
                  int layerHeight = background[layer].getHeight(this);
                  int verticalTiles = getHeight()/layerHeight + 1;
                  int horizontalTiles = getWidth()/layerWidth + 1;
                  // there HAS to be more elegant way of doing this
                  for(int i = -1; i < verticalTiles; i++) {
                        for(int j = -1; j < horizontalTiles; j++) {
                              int x = j*layerWidth - ((player.getX() % getWidth()) / (5 + (int)Math.pow((double)5, (double)layer)));
                              int y = i*layerHeight - (player.getY() % getHeight() / (5 + (int)Math.pow((double)5, (double)layer)));
                              g.drawImage(background[layer], x, y, this);
            
                        }
                  }
            }
      }