Results 1 to 1 of 1
  1. #1
    Aboogesnickle is offline Member
    Join Date
    Jun 2014
    Rep Power

    Default Problem with SubImages not rendering.

    I am attempting to write a 2d game engine the player sprite i have renders fine but I am having a problem with it rendering the tiles from the sprite sheet. It is suppose to render the player to the screen and render the level. A level is loaded with 1 pixel = a 16 x 16 pixel tile. I use the colors of the pixels to determine which tile should be in its place by using multidimensional array with a switch to assign the array with 1,2, or 3 and then use another switch to read the array and get previously assigned int and match them with the right subImage of the tile that should be rendered. I had this working at one point but i messed it up when I tried to optimize it.

    This is the Level Class
    Java Code:
    package game.level;
    import game.Engine;
    import game.level.tile.Tile;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    public class Level {
    	private BufferedImage lvlSheet;
    	private int[][] tiles;
    	private int width, height;
    	private BufferedImage grass, water, rock, tile;
    	public Level() {
    		ImageHandler handler = new ImageHandler();
    		lvlSheet = handler.loadImage("/LevelSheet.png");
    		width = lvlSheet.getWidth();
    		height = lvlSheet.getHeight();
    	public void loadLevel(BufferedImage lvlSheet) {     //this is the array that sets the values to a 1,2, or 3. and i debugged it had the correct variables in it
    		tiles = new int[lvlSheet.getWidth()][lvlSheet.getHeight()];
    		for (int y = 0; y < lvlSheet.getHeight(); y++) {
    			for (int x = 0; x < lvlSheet.getWidth(); x++) {
    				Color c = new Color(lvlSheet.getRGB(x, y));
    				String h = String.format("%02x%02x%02x", c.getRed(),
    						c.getGreen(), c.getBlue());
    				switch (h) {
    				case "00ff00":
    					tiles[x][y] = 1;
    				case "0000ff":
    					tiles[x][y] = 2;
    				case "555555":
    					tiles[x][y] = 3;
    					tiles[x][y] = 1;
    	public void render(Graphics g) {
    		int xo = Engine.getPlayer().getXo();
    		int yo = Engine.getPlayer().getYo();
    		int x0 = Math.max(xo / (Engine.TILESIZE * Engine.SCALE), 0);
    		int y0 = Math.max(yo / (Engine.TILESIZE * Engine.SCALE), 0);
    		int x1 = Math.min((xo + Engine.WIDTH * Engine.SCALE)
    				/ (Engine.TILESIZE * Engine.SCALE) + 1, width);
    		int y1 = Math.min((yo + Engine.HEIGHT * Engine.SCALE)
    				/ (Engine.TILESIZE * Engine.SCALE) + 1, height);
    		for (int y = y0; y < y1; y++) {            //this is the loop that should render the BufferedImages of the tiles
    			for (int x = x0; x < x1; x++) {
    				tile = getTile(x, y);
    				g.drawImage(tile, x << 4 - xo, y << 4 - yo,
    						Engine.TILESIZE, Engine.TILESIZE, null);
    	public BufferedImage getTile(int x, int y) {    //This is suppose to retrieve the correct image from the Tile class 
    		switch (tiles[x][y]) {
    		case 1:
    			grass = Tile.grass;
    			return grass;
    		case 2:
    			water = Tile.water;
    			return water;
    		case 3:
    			rock = Tile.rock;
    			return rock;
    			grass = Tile.grass;
    			return grass;
    This is the Tile Class
    Java Code:
    package game.level.tile;
    import game.Engine;
    import java.awt.image.BufferedImage;
    public class Tile {
    	public static BufferedImage grass, water, rock;
    	private BufferedImage tileSheet;
    	public Tile() {
    		ImageHandler handler = new ImageHandler();
    		tileSheet = handler.loadImage("/TileSheet.png");
    		grass = handler.crop(tileSheet, 1, 0, Engine.TILESIZE, Engine.TILESIZE);
    		water = handler.crop(tileSheet, 0, 0, Engine.TILESIZE, Engine.TILESIZE);
    		rock = handler.crop(tileSheet, 2, 0, Engine.TILESIZE, Engine.TILESIZE);
    This is the ImageHandler Class'
    Java Code:
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    public class ImageHandler {
    	public BufferedImage loadImage(String path) {     //i have the folder for the png files in the build path
    		try {
    		} catch (IOException e) {
    		return null;
    	public BufferedImage crop(BufferedImage image, int col, int row, int width,
    			int height) {
    		return image.getSubimage((col << 4), (row << 4), width, height);    // << 4 makes the col and row start on the next set of 16 pixel tiles
    The Variable that are referred to in the Engine Class are WIDTH = 320, HEIGHT = WIDTH / 12 * 9, SCALE = 2, TILESIZE = 16.

    i think that is all the relevant classes. I have no clue why it does not render the tiles. If anybody needs any other information to help me with this i would be more than willing to give it.

    P.S. I also do not know if this is in the right forum If it is not then could someone please tell me which it should be in
    Attached Thumbnails Attached Thumbnails Problem with SubImages not rendering.-levelsheet.png   Problem with SubImages not rendering.-playersheet.png   Problem with SubImages not rendering.-tilesheet.png  
    Last edited by Aboogesnickle; 06-27-2014 at 07:48 PM.

Similar Threads

  1. Android PDF Rendering
    By iankitshah in forum Android
    Replies: 0
    Last Post: 08-17-2011, 06:18 AM
  2. Xml Rendering BASE64 break line problem
    By justinazz in forum XML
    Replies: 0
    Last Post: 01-05-2011, 05:46 PM
  3. JTree rendering problem
    By kmm1977 in forum AWT / Swing
    Replies: 5
    Last Post: 07-07-2010, 12:02 PM
  4. Problem in JSP rendering
    By in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2009, 02:02 AM
  5. JLabel Rendering
    By random4534 in forum New To Java
    Replies: 3
    Last Post: 12-16-2008, 09:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts