Results 1 to 2 of 2
  1. #1
    matevos is offline Member
    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    Default Problem with drawImage

    Hi, I learning Java since 2 weeks, and today I decided to make a simply platform game.
    Its a code which getting the spritesheet.

    Java Code:
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
     
    public class LoadSprites {
     
            public static BufferedImage tiles;
     
            public LoadSprites(){
                    LoadSprites2();
            }
     
            public void LoadSprites2(){
                    try{
                            tiles = ImageIO.read(new File("res/tiles.png"));
                    }catch(Exception ex){}
            }
     
     
    }
    And this is a code which getting the 1 tile from the spritesheet and rendering this to the map.

    Java Code:
    public void render(Graphics g){
                    for(int x=0; x<15; x++){
                            for(int y=0; y<15; y++){
                                    if (Map[y].substring(x, x + 1).equals("G")){
                                            //g.fillRect(x * 32, y * 32, 32, 32);
                                            g.drawImage(LoadSprites.tiles, x * 32, y * 32, 32, 32, 32, 0, 0, 32, null);
     
                                    }
                            }
                    }
            }
    When I using the g.fillRect the map looks like this



    And it is fine.

    But when I use a drawImage and I importing from the big image 1 tile and rendering this to map it look like this:



    And as we see drawImage creating 3 big rectangles, and 1 big square. Instead of creating the tiled map like on this first image.

    What is wrong? Please help.

    Ps
    Its my double buffering method. Also I using the canvas.

    Java Code:
    public void DoubleBuffering(){
                    BufferStrategy bs = getBufferStrategy();
     
                    if(bs == null){
                            createBufferStrategy(2);
                            return;
                    }
     
                    Graphics g = bs.getDrawGraphics();
     
                    map.render(g);
                    g.dispose();
     
                    g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
                    bs.show();
            }
    Attached Thumbnails Attached Thumbnails Problem with drawImage-74715233.png  

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Problem with drawImage

    It's hard to guess with so little code. Best thing I can tell you is put in some println statements to see what's actually getting painted.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Graphics.drawImage() question
    By sakratt in forum AWT / Swing
    Replies: 0
    Last Post: 03-28-2012, 10:52 PM
  2. Does not recognize drawImage method...
    By Koba in forum New To Java
    Replies: 13
    Last Post: 11-21-2011, 04:06 AM
  3. NullPointerException on Graphics.drawImage
    By Jaeela in forum New To Java
    Replies: 5
    Last Post: 10-05-2011, 02:36 AM
  4. DrawImage with transparency ?
    By mrhid6 in forum AWT / Swing
    Replies: 2
    Last Post: 10-01-2011, 11:58 PM
  5. drawImage im 13 plz help
    By pseudohawtcakes in forum New To Java
    Replies: 3
    Last Post: 05-17-2011, 08:07 PM

Tags for this Thread

Posting Permissions

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