0
votes

I'm currently working on a tile-based game in Java with Java2D. However, I think I'm missing something really stupid, or maybe not, the thing is that I checked my code thousands of times and I didn't saw anything weird. So this is the print of my game right now:

But it should look something like this: enter image description here

Well, I know, it looks fine but if you take a look at the image of my tile-based level:

(grays tiles are bricks, green tiles are grass and brown tiles are dirt)

So the bricks tiles are duplicated on some weird way. So, here is my Level code:

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import com.wg.MainComponent;
import com.wg.entity.Entity;
import com.wg.entity.Particle;
import com.wg.entity.Projectile;
import com.wg.entity.mob.Mob;
import com.wg.entity.mob.Player;

public abstract class Level {

    public BufferedImage levelImage;
    public BufferedImage[][] tile;
    public Rectangle[][] rect;
    public int tileSize;
    public float xOffset, yOffset;
    public ArrayList<BufferedImage> collision = new ArrayList<BufferedImage>();

    public ArrayList<Player> players = new ArrayList<Player>();
    public ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
    public ArrayList<Particle> particles = new ArrayList<Particle>();
    private List<Entity> entities = new ArrayList<Entity>();

    public BufferedImage[] tilesBufferedImages;
    public int[] xCollisionOffset;
    public int[] yCollisionOffset;

    public Level(BufferedImage levelImage, int tileSize, BufferedImage[] tilesBufferedImages, int[] xCollisionOffset, int[] yCollisionOffset) {
        this.tilesBufferedImages = tilesBufferedImages;
        this.xCollisionOffset = xCollisionOffset;
        this.yCollisionOffset = yCollisionOffset;

        this.levelImage = levelImage;
        this.tileSize = tileSize;

        tile = new BufferedImage[levelImage.getWidth()][levelImage.getHeight()];
        rect = new Rectangle[levelImage.getWidth()][levelImage.getHeight()];

        generateLevel();
    }

    public abstract void generateLevel();

    public void render(float xOffset, float yOffset, int scale, Graphics2D screen) {
        this.xOffset = xOffset;
        this.yOffset = yOffset;

        for (int y = (int) Math.max(yOffset / (tileSize + scale), 0); y < Math.min((MainComponent.height + yOffset) / (tileSize + scale) + 1, levelImage.getHeight()); y++) {
            for (int x = (int) Math.max(xOffset / (tileSize + scale), 0); x < Math.min((MainComponent.width + xOffset) / (tileSize + scale) + 1, levelImage.getWidth()); x++) {
                if (tile[x][y] != null)
                    screen.drawImage(tile[x][y], (int) (x * (tileSize + scale) - xOffset), (int) (y * (tileSize + scale) - yOffset), (tileSize + scale), (tileSize + scale), null);
            }
        }

        for (int i = 0; i < entities.size(); i++) {
            entities.get(i).render(screen);
        }

        for (int i = 0; i < players.size(); i++) {
            players.get(i).render(screen);
        }

        for (int i = 0; i < projectiles.size(); i++) {
            projectiles.get(i).render(screen);
        }

        for (int i = 0; i < particles.size(); i++) {
            particles.get(i).render(screen);
        }
    }

    public void add(Entity e) {
        if (e instanceof Particle) {
            particles.add((Particle) e);
        } else if (e instanceof Player) {
            players.add((Player) e);
        } else if (e instanceof Projectile) {
            projectiles.add((Projectile) e);
        } else {
            entities.add(e);
        }
    }

    public void update(int scale) {
        for (int y = 0; y < levelImage.getHeight(); y++) {
            for (int x = 0; x < levelImage.getWidth(); x++) {
                if (tile[x][y] != null && rect[x][y] == null) {
                    for (int i = 0; i < tilesBufferedImages.length; i++) {
                        if (tilesBufferedImages[i] == tile[x][y])
                            rect[x][y] = new Rectangle(x * (tileSize + scale) - Math.round(xOffset) + xCollisionOffset[i], y * (tileSize + scale) - Math.round(yOffset) + yCollisionOffset[i], (tileSize + scale) + xCollisionOffset[i], (tileSize + scale) + yCollisionOffset[i]);
                    }
                } else if (tile[x][y] != null && rect[x][y] != null) {
                    for (int i = 0; i < tilesBufferedImages.length; i++) {
                        if (tilesBufferedImages[i] == tile[x][y])
                            rect[x][y].setBounds(x * (tileSize + scale) - Math.round(xOffset) + xCollisionOffset[i], y * (tileSize + scale) - Math.round(yOffset) + xCollisionOffset[i], (tileSize + scale) + xCollisionOffset[i], (tileSize + scale) + yCollisionOffset[i]);
                    }
                }
            }
        }

        for (int i = 0; i < entities.size(); i++) {
            entities.get(i).update();
        }

        for (int i = 0; i < players.size(); i++) {
            players.get(i).update();
        }

        for (int i = 0; i < projectiles.size(); i++) {
            projectiles.get(i).update();
        }
        for (int i = 0; i < particles.size(); i++) {
            particles.get(i).update();
        }

        remove();
    }

    public List<Player> getPlayers() {
        return players;
    }

    public Player getPlayerAt(int index) {
        return players.get(index);
    }

    public Player getClientPlayer() {
        return players.get(0);
    }

    private void remove() {
        for (int i = 0; i < entities.size(); i++) {
            if (entities.get(i).isRemoved())
                entities.remove(i);
        }
        for (int i = 0; i < projectiles.size(); i++) {
            if (projectiles.get(i).isRemoved())
                projectiles.remove(i);
        }
        for (int i = 0; i < players.size(); i++) {
            if (players.get(i).isRemoved())
                players.remove(i);
        }
        for (int i = 0; i < particles.size(); i++) {
            if (particles.get(i).isRemoved())
                particles.remove(i);
        }
    }

    public boolean tileDownCollision(Mob en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x), Math.round(en.y + en.height));
        else if (!fixed)
            p = new Point(Math.round(en.x - xOffset), Math.round(en.y + en.height - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean tileUpCollision(Mob en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x), Math.round(en.y - 1));
        else if (!fixed)
            p = new Point(Math.round(en.x - xOffset), Math.round(en.y - 1 - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean tileLeftCollision(Mob en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x - 1), Math.round(en.y));
        else if (!fixed)
            p = new Point(Math.round(en.x - 1 - xOffset), Math.round(en.y - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean tileRightCollision(Mob en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x + en.width), Math.round(en.y));
        else if (!fixed)
            p = new Point(Math.round(en.x + en.width - xOffset), Math.round(en.y - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean projectileDownCollision(Projectile en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x), Math.round(en.y + en.height));
        else if (!fixed)
            p = new Point(Math.round(en.x - xOffset), Math.round(en.y + en.height - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean projectileUpCollision(Projectile en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x), Math.round(en.y - 1));
        else if (!fixed)
            p = new Point(Math.round(en.x - xOffset), Math.round(en.y - 1 - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean projectileLeftCollision(Projectile en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x - 1), Math.round(en.y));
        else if (!fixed)
            p = new Point(Math.round(en.x - 1 - xOffset), Math.round(en.y - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean projectileRightCollision(Projectile en, boolean fixed) {
        Point p = null;
        if (fixed)
            p = new Point(Math.round(en.x + en.width), Math.round(en.y));
        else if (!fixed)
            p = new Point(Math.round(en.x + en.width - xOffset), Math.round(en.y - yOffset));
        try {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    if (tile[x][y] != null) {
                        if (rect[x][y] != null && rect[x][y].contains(p)) {
                            if (collision.contains(tile[x][y]))
                                return true;
                        }
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        return false;
    }

    public boolean mobProjectileCollision(Mob en, Projectile pr, boolean fixed) {
        Rectangle p1 = null;
        if (fixed)
            p1 = new Rectangle(Math.round(en.x), Math.round(en.y), en.width, en.height);
        else if (!fixed)
            p1 = new Rectangle(Math.round(en.x - xOffset), Math.round(en.y - yOffset), en.width, en.height);

        Rectangle p2 = null;
        if (fixed)
            p2 = new Rectangle(Math.round(pr.x), Math.round(pr.y), pr.width, pr.height);
        else if (!fixed)
            p2 = new Rectangle(Math.round(pr.x - xOffset), Math.round(pr.y - yOffset), pr.width, pr.height);

        if (p1.intersects(p2))
            return true;

        return false;
    }

    public double calcDistance(Entity e1, Entity e2) {
        return Math.sqrt(((e1.x - e2.x) * (e1.x - e2.x)) + ((e1.y - e2.y) * (e1.y - e2.y)));
    }
}

The Level is rendered into the render(Graphics2D) method. It is an abstract class so I have a subclass of it, Level1, in the case it is the one in the image.

import static com.wg.MainComponent.spritesheet;

import java.awt.Color;
import java.awt.image.BufferedImage;

import com.wg.BufferedImageLoader;
import com.wg.entity.mob.Player;

public class Level1 extends Level {

    public static final BufferedImage bricks = crop(0, 2);
    public static final BufferedImage dirt = crop(1, 2);
    public static final BufferedImage grass = crop(2, 2);
    public static final BufferedImage[] tilesList = { bricks, dirt, grass };
    public static final int[] defaultxCollisionOffset = { 0, 0, 0 };
    public static final int[] defaultyCollisionOffset = { 0, 0, 0 };
    private static BufferedImage level = BufferedImageLoader.load("/level1.png");

    private static BufferedImage crop(int x, int y) {
        return spritesheet.getSubimage(x * 16, y * 16, 16, 16);
    }

    public void generateLevel() {
        for (int y = 0; y < levelImage.getHeight(); y++) {
            for (int x = 0; x < levelImage.getWidth(); x++) {
                Color c = new Color(level.getRGB(x, y));
                String data = String.format("%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue());
                if (data.equals("838383"))
                    tile[x][y] = bricks;
                else if (data.equals("bea100"))
                    tile[x][y] = dirt;
                else if (data.equals("23d200"))
                    tile[x][y] = grass;
            }
        }
    }

    public Level1(float xOffset, float yOffset) {
        super(level, 16, tilesList, defaultxCollisionOffset, defaultyCollisionOffset);
        collision.add(grass);
        collision.add(dirt);
        collision.add(bricks);
        add(new Player(50, 20, 1, 2, 32 + 32, 32 + 32, this));
        this.xOffset = xOffset;
        this.yOffset = yOffset;
    }
}

Well, I know that would be really hard to find the error just with two classes, so I made a MCVE :). (To run it create a folder called image in your C: with this inside called level1.png and this image called tiles.png)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class MCVE extends JFrame {

    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    private MCVELevel1 level1 = new MCVELevel1(0, 0);

    public static void main(String[] args) {
        MCVE mcve = new MCVE();
        mcve.setVisible(true);
        mcve.setSize(WIDTH, HEIGHT);
        mcve.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
        level1.render(50, 400, 32, (Graphics2D) g);
    }

    private static class MCVELevel1 extends MCVELevel {

        private static BufferedImage spritesheet = BufferedImageLoader.load("C://image//tiles.png");
        public static final BufferedImage bricks = crop(0, 0);
        public static final BufferedImage dirt = crop(1, 0);
        public static final BufferedImage grass = crop(2, 0);
        public static final BufferedImage[] tilesList = { bricks, dirt, grass };
        public static final int[] defaultxCollisionOffset = { 0, 0, 0 };
        public static final int[] defaultyCollisionOffset = { 0, 0, 0 };
        private static BufferedImage level = BufferedImageLoader.load("C://image//level1.png");

        private static BufferedImage crop(int x, int y) {
            return spritesheet.getSubimage(x * 16, y * 16, 16, 16);
        }

        public void generateLevel() {
            for (int y = 0; y < levelImage.getHeight(); y++) {
                for (int x = 0; x < levelImage.getWidth(); x++) {
                    Color c = new Color(level.getRGB(x, y));
                    String data = String.format("%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue());
                    if (data.equals("838383"))
                        tile[x][y] = bricks;
                    else if (data.equals("bea100"))
                        tile[x][y] = dirt;
                    else if (data.equals("23d200"))
                        tile[x][y] = grass;
                }
            }
        }

        public MCVELevel1(float xOffset, float yOffset) {
            super(level, 16, tilesList, defaultxCollisionOffset, defaultyCollisionOffset);
            this.xOffset = xOffset;
            this.yOffset = yOffset;
        }
    }

    public static class BufferedImageLoader {
        public static BufferedImage load(String path) {
            try {
                return ImageIO.read(new File(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
            throw new NullPointerException("No file found at: " + path);
        }

        public static BufferedImage load(URL url) {
            try {
                return ImageIO.read(url);
            } catch (IOException e) {
                e.printStackTrace();
            }
            throw new NullPointerException("No file found at: " + url.getPath());
        }
    }
}

The MCVELevel class:

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

public abstract class MCVELevel {

    public BufferedImage levelImage;
    public BufferedImage[][] tile;
    public Rectangle[][] rect;
    public int tileSize;
    public float xOffset, yOffset;

    public BufferedImage[] tilesBufferedImages;
    public int[] xCollisionOffset;
    public int[] yCollisionOffset;

    public MCVELevel(BufferedImage levelImage, int tileSize, BufferedImage[] tilesBufferedImages, int[] xCollisionOffset, int[] yCollisionOffset) {
        this.tilesBufferedImages = tilesBufferedImages;
        this.xCollisionOffset = xCollisionOffset;
        this.yCollisionOffset = yCollisionOffset;

        this.levelImage = levelImage;
        this.tileSize = tileSize;

        tile = new BufferedImage[levelImage.getWidth()][levelImage.getHeight()];
        rect = new Rectangle[levelImage.getWidth()][levelImage.getHeight()];

        generateLevel();
    }

    public abstract void generateLevel();

    public void render(float xOffset, float yOffset, int scale, Graphics2D screen) {
        this.xOffset = xOffset;
        this.yOffset = yOffset;

        for (int y = (int) Math.max(yOffset / (tileSize + scale), 0); y < Math.min((MCVE.HEIGHT + yOffset) / (tileSize + scale) + 1, levelImage.getHeight()); y++) {
            for (int x = (int) Math.max(xOffset / (tileSize + scale), 0); x < Math.min((MCVE.WIDTH + xOffset) / (tileSize + scale) + 1, levelImage.getWidth()); x++) {
                if (tile[x][y] != null)
                    screen.drawImage(tile[x][y], (int) (x * (tileSize + scale) - xOffset), (int) (y * (tileSize + scale) - yOffset), (tileSize + scale), (tileSize + scale), null);
            }
        }
    }

    public void update(int scale) {
        for (int y = 0; y < levelImage.getHeight(); y++) {
            for (int x = 0; x < levelImage.getWidth(); x++) {
                if (tile[x][y] != null && rect[x][y] == null) {
                    for (int i = 0; i < tilesBufferedImages.length; i++) {
                        if (tilesBufferedImages[i] == tile[x][y])
                            rect[x][y] = new Rectangle(x * (tileSize + scale) - Math.round(xOffset) + xCollisionOffset[i], y * (tileSize + scale) - Math.round(yOffset) + yCollisionOffset[i], (tileSize + scale) + xCollisionOffset[i], (tileSize + scale) + yCollisionOffset[i]);
                    }
                } else if (tile[x][y] != null && rect[x][y] != null) {
                    for (int i = 0; i < tilesBufferedImages.length; i++) {
                        if (tilesBufferedImages[i] == tile[x][y])
                            rect[x][y].setBounds(x * (tileSize + scale) - Math.round(xOffset) + xCollisionOffset[i], y * (tileSize + scale) - Math.round(yOffset) + xCollisionOffset[i], (tileSize + scale) + xCollisionOffset[i], (tileSize + scale) + yCollisionOffset[i]);
                    }
                }
            }
        }
    }
}

Many thanks.

EDIT: updated code at MCVELevel:

public void render(float xOffset, float yOffset, int scale, Graphics2D screen) {
        this.xOffset = xOffset;
        this.yOffset = yOffset;

        for (int y = 0; y < levelImage.getHeight(); y++) {
            for (int x = 0; x < levelImage.getWidth(); x++) {
                if (tile[x][y] != null)
                    screen.drawImage(tile[x][y], (int) (x * (tileSize + scale) - xOffset), (int) (y * (tileSize + scale) - yOffset), (tileSize + scale), (tileSize + scale), null);
            }
        }
    }
2
Can you be a little more specific about what's not working? "So the bricks tiles are duplicated on some weird way" doesn't really give us much to go on, and I don't see anything that looks duplicated in your screenshot. How does that differ from what you're expecting? - Tim
I'm still not sure what I'm looking for. Is it the wall to the left of the pink character vs. the similar ones to the right of the pink character? (And if so, which one is the duplicate and which one is right?) Or something else? - Tim

2 Answers

2
votes

The problem is in this chunk inside the render method:

for (int y = (int) Math.max(yOffset / (tileSize + scale), 0); y < Math.min((MCVE.HEIGHT + yOffset) / (tileSize + scale) + 1, levelImage.getHeight()); y++) {
    for (int x = (int) Math.max(xOffset / (tileSize + scale), 0); x < Math.min((MCVE.WIDTH + xOffset) / (tileSize + scale) + 1, levelImage.getWidth()); x++) {
        if (tile[x][y] != null)
            screen.drawImage(tile[x][y], (int) (x * (tileSize + scale) - xOffset), (int) (y * (tileSize + scale) - yOffset), (tileSize + scale), (tileSize + scale), null);
    }
}

This seems to be saying: "for pixels that I want to draw to, draw tiles" (though I could be wrong about that.. I get a different explanation every time I read that for-loop guard). The problem is that you're calculating x and y in terms of drawing area, but you're using them in terms of indices into the tiles[] array. You want "for tiles I want to draw, draw tiles".

The loop in generateLevel() is doing the right thing. Use that sort of loop (for (y = 0 y < levelImage.getHeight(); ++y)). The only bit of code that needs to worry about yOffset, tileSize and scale is the code that actually draws a tile.

This reminds me again why code should be written to be testable, and why tests should be written :)

1
votes

(My other answer is not wrong (I think) but it doesn't answer the specific problem; it fixes a problem that would have become apparent later.)

Your level1.png has some unusual pixels which have RGB values that match bricks, but which look to the eye to be blank. I guess there's a transparency thing happening, but I don't know anything about pngs. Anyway, this level1.png is clean and works as expected: level1.png

It took a while to figure this out. I created a more minimal MCVE from your one. This version may help future readers debug the problem with the original png (in the question).

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class MCVE extends JFrame {
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;

    public static void main(String[] args) {
        MCVE mcve = new MCVE();
        mcve.setVisible(true);
        mcve.setSize(WIDTH, HEIGHT);
        mcve.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
        render((Graphics2D) g);
    }

    public BufferedImage[][] tile;
    public static final int TILE_SIZE = 16;
    private static BufferedImage spritesheet = BufferedImageLoader
            .load("tiles.png");
    private static final BufferedImage bricks = crop(0, 0);
    private static final BufferedImage dirt = crop(1, 0);
    private static final BufferedImage grass = crop(2, 0);
    private static final BufferedImage levelImage = BufferedImageLoader.load("level1.png");
    int widthInTiles;
    int heightInTiles;

    private static BufferedImage crop(int x, int y) {
        return spritesheet.getSubimage(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE,
                TILE_SIZE);
    }

    public MCVE() {
        widthInTiles = levelImage.getWidth();
        heightInTiles = levelImage.getHeight();

        generateLevel();
    }

    public void generateLevel() {
        tile = new BufferedImage[widthInTiles][heightInTiles];
        for (int y = 0; y < heightInTiles; y++) {
            for (int x = 0; x < widthInTiles; x++) {
                Color c = new Color(levelImage.getRGB(x, y));
                switch (c.getRGB()) {
                case -8158333:
                    tile[x][y] = bricks;
                    break;
                case -14429696:
                    tile[x][y] = grass;
                    break;
                case -4284160:
                    tile[x][y] = dirt;
                }
            }
        }
    }

    public void render(Graphics2D screen) {
        int scale = 16;
        int yes = 0, no = 0;
        for (int y = 0; y < heightInTiles; y++) {
            for (int x = 0; x < widthInTiles; x++) {
                if (tile[x][y] != null) {
                    screen.drawImage(tile[x][y], (int) (x * (TILE_SIZE + scale)),
                            (int) (y * (TILE_SIZE + scale)),
                            (TILE_SIZE + scale), (TILE_SIZE + scale),
                            Color.BLACK, null);
                }
            }
        }
    }

    public static class BufferedImageLoader {
        public static BufferedImage load(String path) {
            try {
                return ImageIO.read(new File(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
            throw new NullPointerException("No file found at: " + path);
        }

        public static BufferedImage load(URL url) {
            try {
                return ImageIO.read(url);
            } catch (IOException e) {
                e.printStackTrace();
            }
            throw new NullPointerException("No file found at: " + url.getPath());
        }
    }
}