0

I don't understand why g.drawImage doesn't draw anything?

public class Board extends JPanel{

   Map map;

   public Board()
   {
       map = new Map();
       map.loadMap();
   }

   @Override
   public void paint(Graphics g)
   {   
       super.paint(g);
       g.drawImage(map.getGround(), 0, 0, this);
       g.drawImage(map.getWall(), 100, 100, this);

   }  

}

Map.loadMap() loads images Ground and Wall from file, functions getGround and getWall return these images. But if i move those 2 lines:

 map = new Map();
 map.loadMap();

into print function, then it works.

This code works:

public class Board extends JPanel{

   Map map;

   public Board(){}

   @Override
   public void paint(Graphics g)
   {   
       map = new Map();
       map.loadMap();

       super.paint(g);
       g.drawImage(map.getGround(), 0, 0, this);
       g.drawImage(map.getWall(), 100, 100, this);

   }

}

My map class:

public class Map {

    Scanner scanner;
    int mapHeight = 5, mapWidth = 7;
    String[] map = new String[mapHeight];
    BufferedImage ground, wall;

    public Map(){
        loadMap();
        loadGround();
        loadWall();
    }

    private void loadMap()
    {
        try{
            scanner = new Scanner(new FileInputStream("/home/bobalice/Map.txt"));

            for(int i = 0; i < mapHeight; i++)
            {
                map[i] = scanner.nextLine();
            }

        }catch(IOException e){
            System.err.println("Error: Map file doesn't exist!");
        }finally{
            if(scanner != null) scanner.close();
        }                      
    }

    private void loadGround()
    {
        try{
            ground = ImageIO.read(new File("/home/bobalice/ground.png"));
        }catch(IOException e){
            System.err.println("Error: ground.png doesn't exist!");
        }
    }


///

    private void loadWall()
    {
        try{
            wall = ImageIO.read(new File("/home/bobalice/wall.png"));
        }catch(IOException e){
            System.err.println("Error: wall.png doesn't exist!");
        }
    }

    public String[] getMap()
    {
        return map;
    }

    public BufferedImage getGround()
    {
        return ground;
    }

    public BufferedImage getWall()
    {
        return wall;
    }
camickr
  • 321,443
  • 19
  • 166
  • 288
AnorLondo
  • 37
  • 1
  • 7
  • Since the inconsistent behavior is in your `loadMap` method, it would make sense to show the code for that method in your question. – VGR May 17 '17 at 17:29
  • 2
    For any `JComponent`, override `paintComponent(Graphics)` rather than `paint(..)`. Further tips: 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson May 17 '17 at 18:01
  • 2
    1) Maybe the images are not loaded when the panel is painted. What happens if you resize the frame to force a repaint? 2) How do you add the panel to the frame. Your panel doesn't have a "preferred size" so the default could be (0, 0), depending on the layout manager being used. – camickr May 17 '17 at 18:14

0 Answers0