Results 1 to 11 of 11

Thread: Image Loading

  1. #1
    hobbles is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default Image Loading

    I'm having a problem with loading images, than drawing them into a tile map. I read several articles about loading images but cant seem to get it right. Right now it just flashes the whole area that the map should be at green (probably the grass) then goes blank. I get no errors or anything so I'm not sure what is going on. I used this same way to draw a map but in the if files with the drawimage it was g.setColor and after the if statements g.drawRect and that worked so this looks like it should adapt fine.

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class helloworld extends Applet
    
    {
    	int[][] map = {                                //simple map each square is 16 x 16 pixels might change it to 32
                    {1,1,1,1,1,1,1,1,1,1,1,1,1,0,1},  //the height and width is 240 
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                    {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}
       };	
          
    	int x = 0; // x,y for the array		
    	int y = 0;	
    	int xmap = 0; // x,y for drawing the array
    	int ymap = 0;
    
    	public void paint (Graphics g)
    	{
    		//Image grass = getImage(getCodeBase(),"grass.gif");  //also tried it this way
    		//Image water = getImage(getCodeBase(),"water.gif");
    		Image grass = Toolkit.getDefaultToolkit().getImage(getClass().getResource("grass.gif"));
    		Image water = Toolkit.getDefaultToolkit().getImage(getClass().getResource("water.gif"));
    
    		g.drawString("Ran",10,290); //just says if it ran or not
    			
    		while (y<=14) //draws out the map  
    		{
    				if (map[y][x] == 0) // grass
    				{
    					g.drawImage(grass, xmap, ymap, this);
    				}
    				if (map[y][x] == 1) // wall
    				{
    					g.drawImage(water, xmap, ymap, this);
    				}
    				if (map[y][x] == 2) // start
    				{
    					g.drawImage(grass, xmap, ymap, this);
    				}
    				if (map[y][x] == 3) // end
    				{
    					g.drawImage(grass, xmap, ymap, this);
    				}
    
    				x++;
    				xmap = xmap + 32;
    				
    				if (x >= 15)
    				{
    					x = 0;
    					xmap = 0;
    					y++;
    					ymap = ymap + 32;
    				}
    		}	
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Try debugging your code by adding a println() in the paint method inside the loop:

    System.out.println("y=" + y + ", x=" + x);

    And this just before starting the loop:

    System.out.println("Init y=" + y + ", x=" + x);

    This will show you what is happening in your code.

  3. #3
    hobbles is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default

    I dunno, I noticed that it printed out init y more than once and that doesn't sound right is it just because the loop is in the paint method? This is driving me crazy....

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    it printed out init y more than once
    That would show how many times the paint method was being called.
    Also it shows what the values of x and y are when the paint method is called the second and following times. What do you expect/need to values of x, xmap, ymap and y to be in paint?
    Do you ever reset them?

    You need to consider that the paint() method is called when the JVM thinks the component needs to be repainted. It can be more than once as you see with the printouts.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    A couple of recs:
    How about using nested for loops rather than while loops?

    You really don't want to be reading in image files from within a paint method, but rather should read the images in once and let the paint method be short, lean, and fast. Else your drawing will slow to a crawl.

  6. #6
    hobbles is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default

    Ok always thought paint ran once unless called again so I never thought of resetting the integers. Is there a way to draw the graphics from a different method kinda like below.
    Java Code:
    cut down to show the basic thing that im getting
    
    public void drawmap () //tried drawmap(Graphics g) it was worth a try
    {
    g.drawRect();
    }
    
    public void paint (Graphics g)
    {
    drawmap();
    
    }
    I could not find anything on it since I'm not sure what it would be called and its kinda vague. Also I know this is not the best way to make a tile map for a game but this wouldn't be that bad if I made a game with it would it? Thanks for the help so far works perfectly now :)

    Quote Originally Posted by Fubarable View Post
    A couple of recs:
    How about using nested for loops rather than while loops?

    You really don't want to be reading in image files from within a paint method, but rather should read the images in once and let the paint method be short, lean, and fast. Else your drawing will slow to a crawl.
    Ok I will work on the for loops instead of using the while. Reading the image file was out of the paint method first, but I started jumbling were it was since it wasn't working and I thought that could be the problem. Moved it out of the paint method now thanks for reminding me I would have completely forgot about it.
    Last edited by hobbles; 09-06-2010 at 06:48 PM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Is there a way to draw the graphics from a different method kinda like below.
    That doesn't solve the problem since paint calls your other method every time it is called.

    An idea: if what you are drawing does NOT change, you could create a BufferedImage, get its graphics content and the graphics object to draw on that bufferedimage to create an image that you would save. This would be done one time in an initialization method
    Then in paint draw that image with a single drawImage call.

  8. #8
    hobbles is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default

    It wasn't to solve anything everything is working I just wanted to take it out of the paint method before that area starts getting messy but you're right I should make the map a buffered image. I would want different maps or a big map with a small section showing (like pokemon) so I'm sure I could get that to work. Last thing, I made a sprite that is half water and half sand. Since the water needs to be facing out of the island of sand should I rotate the image in java or should I have an already flipped image. Which way would be the more efficient way, I would think that flipping it in paint would be the easiest way but will need to load more images. Rotating it in java sounds like it could be more efficient and I wouldn't mind learning how to do that stuff (scale, rotate, ect). Thanks for all the help again this is the last question I needed help with then I'm off to get this done.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Sorry, I don't have any experience flipping images, etc

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I wonder why you are doing this in AWT and not Swing? With Swing you could user your images to create a few ImageIcons and then populate your grid with a bunch of JLabels each holding an appropriate ImageIcon.

    Regarding the flipping of images, I know that it can be done with AffineTransforms, and since the images are small, I suppose you'll want to create four images/ImageIcons for each one to represent all four possible rotations.

  11. #11
    hobbles is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Rep Power
    0

    Default

    I barely even heard of Swing, so I will do it with AWT and will learn how to use Swing later into the game or after.

Similar Threads

  1. Problem with loading image
    By m00nchile in forum New To Java
    Replies: 5
    Last Post: 05-25-2010, 01:34 PM
  2. Problem in loading Image
    By rahulm87 in forum Java 2D
    Replies: 1
    Last Post: 09-08-2009, 04:45 PM
  3. Loading image in applet
    By syarizma in forum Advanced Java
    Replies: 0
    Last Post: 08-06-2009, 09:02 AM
  4. Rambling about image loading...
    By Zamppa in forum Java Applets
    Replies: 0
    Last Post: 04-14-2009, 03:55 PM
  5. Loading An Image Help Please!
    By shaungoater in forum Java 2D
    Replies: 2
    Last Post: 01-09-2008, 08:14 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
  •