Insert background image in Jpanel

Asked

Viewed 1,079 times

2

I already checked and the path is correct, but when I run nothing appears on the screen:

public class MapaInterface extends JPanel implements ActionListener {

private Image fundo;
public static Agente daenerys;
private Timer timer;

public MapaInterface() {
    String caminho = "/res/mapaGot.png";
    URL url = getClass().getResource(caminho);
    ImageIcon referencia = new ImageIcon(caminho);
    fundo = referencia.getImage();
    timer = new Timer(5, this);
    timer.start();
}

public void Paint(Graphics g) {
    Graphics2D graficos = (Graphics2D) g;
    //null = fundo estático
    graficos.drawImage(fundo, 0, 0, null);
    //atualiza a imagem
    graficos.drawImage(daenerys.getImagem(), daenerys.getPosicao().getX(), daenerys.getPosicao().getY(), this);
    g.dispose();
}

@Override
public void actionPerformed(ActionEvent e) {
    repaint();
}

public static Agente getDaenerys() {
    return daenerys;
}

public static void setDaenerys(Agente daenerys) {
    MapaInterface.daenerys = daenerys;
}

When I call the class:

public class ContainerDeJanelas extends JFrame{

    public ContainerDeJanelas(){
        add(new MapaInterface());
        setTitle("Heurística Game of Thrones");
        //tamanho da tela
        setSize(750,750);
        //usuario nao pode redimensionar a tela
        setResizable(false);
        //evento ao clicar em fechar
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //onde a janela vai aparecer (null = centro)
        setLocationRelativeTo(null);

        setVisible(true);
    }
}

main:

   ContainerDeJanelas containerDeJanelas = new ContainerDeJanelas();
  • Try using an image (url) from the internet and/or using an absolute path. I remember I’ve been through something similar. For some reason the program could not access the file; as if it did not exist. I resolved using an absolute path.

  • The problem is that the image has to be this, is an image that I created. What would be an absolute path? I have tested anyway

  • Absolute path is type "C: my folder icone.png figures". The idea of using an absolute path (or a url) is only to see if the image will appear as desired. To determine whether the problem is in the code or in the path (as I mentioned, sometimes the file may be invisible to the program; what seems to have happened to me - and I don’t know how to solve it).

  • Aaah worked out! Thank you :)

  • I closed it as a duplicate because the way to resolve it is similar to what was answered in the other question.

1 answer

0

I found a solution that consists of finding the absolute path of the image from the path from which the program is running; and adding to it the relative path of the image.

Adapting to your program, change the constructor of the Mapainterface class to:

public MapaInterface() {
    String caminhoRelativo = "/res/mapaGot.png";

    String caminhoExecucao = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

    String caminhoAbsoluto = caminhoExecucao + caminhoRelativo ;

    ImageIcon referencia = new ImageIcon(caminhoAbsoluto );
    fundo = referencia.getImage();    
    timer = new Timer(5, this);
    timer.start();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.