[SLICK]Seperate class image

Hello, I have a question for you guys.
I’m making a small game where the world is generated and it is read and then it places block’s (where needed)
Example:
If there is a 1 in my file I do this:

new GrassBlock(x,y);

In my GrassBlock I have this:

public class GrassBlock{

	private Image grass;
	private Polygon poly;
	private int x;
	private int y;
	public GrassBlock(int x,int y) throws SlickException{
		System.out.println("Created grass block at X:" + x + "Y:" + y +".");
		grass = new Image("res/grass.png");
		this.x = x;
		this.y = y;
	}

	public void draw(Graphics g,int x,int y){
		g.drawImage(grass, x, y);
	}

It print this out:
Created grass block at X:99Y:98.

but it doesn’t show, Any help why?

Thanks!

It’s just a guess, cause I’m really familar with such things. Maybe, when drawing multiply the x and y coords by the size of the image?

I did this:

   public void drawImage(Graphics g){
	   g.drawImage(grass, x*16, y*16);
   }

But it doesn’t seem to work, I get this as result:
http://imgur.com/631fs

Okey, thats even much better, cause you use the x and y coords from the class now :smiley: (just realized it…)

Also, are you sure, the Image you load is visible? Sorry, I can’t help you a lot with Slick, I never really used it…

Do you actually call draw(Graphics)?

No.

But do I need to do it in a render loop?

(Code for searching map (not completed yet, and just create’s grass blocks)

public WorldMap(File f) throws SlickException, FileNotFoundException{
		Entities = new ArrayList<Object>();
		mapWidth = 100;
		mapHeight = 100;


		for(int x = 0; x < mapWidth;x++){
			for (int y = 0; y< mapHeight;y++){
				Entities.add(new GrassBlock(x,y));
			}
		}
	}

Here I call the method:

private File map = new File("maps/alpha.grind");
		if(!map.exists()){
			try {
				System.out.println("Creating file");
				map.createNewFile();
            } catch (IOException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
            }
		}else{
			try {
	            WorldMap wmap = new WorldMap(map);
	            System.out.println("Creating World!");
            } catch (FileNotFoundException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
            }

	 }

Do I need to put the drawImage method in the render loop?

And how should I do that?

Thanks!

Well if you never somehow call the Grass’s draw method anywhere…how is it ever going to show up on the screen? :slight_smile:

Not. (:

I changed some things to let it draw.

But it still doesn’t show.

public Graphics g;

	for(int x = 0; x < mapWidth;x++){
			for (int y = 0; y< mapHeight;y++){
				GrassBlock block = new GrassBlock(x,y);

				Entities.add(block);
				block.drawImage(g);
			}
		}

//----------------------------------------------Grassblock class------//
	public GrassBlock(int x,int y) throws SlickException{
		System.out.println("Created grass block at X:" + x + "Y:" + y +".");
		grass = new Image("res/grass.png");
		this.x = x;
		this.y = y;
		this.poly = new Polygon(new float[]{
				x+1,y+1,
				x+17,y+1,
				x+17,y+17,
				x+1,y+1

		});

}

   public void drawImage(Graphics g){
	   g.drawImage(grass, x*16, y*16);
    }
 

Hope you can help! :slight_smile:

Erm… are you calling this everytime in your gameloop? just like you do it with the player?

Here is my gameloop.

@Override
    public void init(GameContainer gc, StateBasedGame sb)
            throws SlickException {
	   player = new Image("res/player.png");
		if(!map.exists()){
			try {
				System.out.println("Creating file");
				map.createNewFile();
            } catch (IOException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
            }
		}else{
			try {
	            WorldMap wmap = new WorldMap(map);
	            System.out.println("Creating World!");
            } catch (FileNotFoundException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
            }

		}

	   }

	@Override
    public void render(GameContainer gc, StateBasedGame sb, Graphics g)
            throws SlickException {
	    // TODO Auto-generated method stub
		player.draw(0,0);
    }

Thanks.

In the render method, you only call player.draw(0,0). You never render the Grass.

<.<

   @Override
    public void render(GameContainer gc, StateBasedGame sb, Graphics g)
            throws SlickException {
       // TODO Auto-generated method stub
      for (int x = 0; x < mapWidth; x++) {
              for (int y = 0; y < mapHeight; y++) {
                        wmap.getBlockAt(x, y).draw(g);
              }
       }
      player.draw(0,0);
    }

Slick caches Images by name, so you can get away with creating a new Image instance each time you create a new Grass instance… But I think it would be better practice not to rely on that. I’d suggest sharing the same Image instance for all Grass entities:


      Image grassImage = new Image("res/grass.png");

      for(int x = 0; x < mapWidth;x++){
         for (int y = 0; y< mapHeight;y++){
            Entities.add(new GrassBlock(grassImage, x, y));
         }
      }