Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SLICK]Seperate class image  (Read 1585 times)
0 Members and 1 Guest are viewing this topic.
Offline cptyx

Senior Newbie





« Posted 2012-05-13 10:42:47 »

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:
1  
new GrassBlock(x,y);

In my GrassBlock I have this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
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!
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #1 - Posted 2012-05-13 10:53:13 »

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?

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline cptyx

Senior Newbie





« Reply #2 - Posted 2012-05-13 11:07:21 »

I did this:
1  
2  
3  
   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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #3 - Posted 2012-05-13 11:11:37 »

Okey, thats even much better, cause you use the x and y coords from the class now Cheesy (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...

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-05-13 11:41:03 »

Do you actually call draw(Graphics)?

Offline cptyx

Senior Newbie





« Reply #5 - Posted 2012-05-13 11:43:38 »

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)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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!
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-05-13 12:17:22 »

Well if you never somehow call the Grass's draw method anywhere......how is it ever going to show up on the screen? Smiley

Offline cptyx

Senior Newbie





« Reply #7 - Posted 2012-05-13 12:22:29 »

Not. (:

I changed some things to let it draw.

But it still doesn't show.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
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! Smiley
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #8 - Posted 2012-05-13 12:24:13 »

Not. (:

I changed some things to let it draw.

But it still doesn't show.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
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! Smiley

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

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline cptyx

Senior Newbie





« Reply #9 - Posted 2012-05-13 12:25:16 »

Here is my gameloop.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
@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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2012-05-13 12:47:26 »

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

Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #11 - Posted 2012-05-13 12:51:45 »

<.<



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
   @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);
    }

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline davedes
« Reply #12 - Posted 2012-05-13 19:01:26 »

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:
1  
2  
3  
4  
5  
6  
7  
      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));
         }
      }

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

E.R. Fleming (15 views)
2014-07-29 03:07:13

E.R. Fleming (4 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!