JPanel Image

This is my code, it really finds the image, so this is not my concern, I am worried about how to make this image the background of the panel. I try to work with graphics, but I do not work, any ideas? you are welcome??

try {
            java.net.URL imgURL = MAINWINDOW.class.getResource(imagen);

            Image imgFondo = javax.imageio.ImageIO.read(imgURL);
            if (imgFondo != null) {
                Graphics grafica=null;
                grafica.drawImage(imgFondo, 0, 0, this);
                panel.paintComponents(grafica);
            } else {
            System.err.println("Couldn't find file: " + imagen);
            }

        } catch...
+3
source share
2 answers

. grafica null, . , , NullPointerException. Graphics , , . Swing, paintComponent , :

  public void paintComponent(Graphics grafica) {
     grafica.drawImage(imgFondo, 0, 0, this); 
  }

, , . , imgFondo , paintComponent.

+2

, JLabel, , .

, , - - .

. .

Swing Custom Painting.

0

Source: https://habr.com/ru/post/1747720/


All Articles