Scrolling movement

I’m trying to achieve scrolling movement by getting the offset (subtracting) of the player from where the map is rendered, however I am achieving no results at all. I’m not sure what other information to provide as I have no other information, below is my code.


package game;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Map {
	private Player player = new Player(0, 0, null);
	int[][] map = new int[][] {
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
	};
	
	public boolean isBlockedWall(int x, int y) { 
		return map[y][x] == 1; 
	}
	
	public boolean getGoal(int x, int y) {
		return map[y][x] == 2;
	}
	
	public void draw(Graphics g) {
        int width = 32;
        int height = 32;

        for (int x = 0; x < 37; x++) {
        	for (int y = 0; y < 15; y++) {
        		switch(map[y][x]) {
        		case 1:
        			Image wall = null;
        			try {
        				wall = ImageIO.read(new File("src/img/wall.png"));
        			} catch (IOException e) {
        				e.printStackTrace();
        			}
        			g.drawImage(wall, x * width - player.x, y * height - player.y, null);
        			break;
        		case 2:
        			Image key = null;
        			try {
        				key = ImageIO.read(new File("src/img/key.png"));
        			} catch (IOException e) {
        				e.printStackTrace();
        			}
        			g.drawImage(key, x * width - player.x, y * height - player.y, null);	
        			break;
        		}
        	}
        }	
	}
}


From your posted code i cannot see any operations that modify the x,y co ordinates of the player instance. So i am no surprised that there is no scrolling as the player x and y are 0. Even if you have other code that you believe is modifying the x,y co ords of the player I believe that is on an entirely different player instance as there is no getter method (or setter method) to return the player instance assoicated with this Map class and thus any changes to this potential other player instance does not effect your paint method.

An other option that could be the issue in the event that the player x,y co ords are being correctly updated: is your player’s movement stride the same as the walls? if so then your current subtraction code should be:

g.drawImage(wall, (x - player.x) * width, (y - player.y) * height, null);

For performance reasons, i suggest that you move your image reading code outside of your paint method, currently you have a pathological case where the inner loop of your paint method is reading in the image each cycle.