Tecnically, i wanna put an image that is located on my proyect
I have this JPanel
*/
package vista;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.ImageIcon;
/**
*
* @author harri
*/
public class scene1 extends javax.swing.JPanel {
String ruta = "src/backgrounds/backgroundmenu.png";
ImageIcon fot = new ImageIcon(ruta);
Icon icono = new ImageIcon(fot.getImage().getScaledInstance(this.backgroundscene1.getWidth(), this.backgroundscene1.getHeight(), Image.SCALE_DEFAULT));
public scene1() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
backgroundscene1 = new javax.swing.JLabel();
setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
backgroundscene1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/backgrounds/backgroundmenu.png"))); // NOI18N
add(backgroundscene1, new org.netbeans.lib.awtextra.AbsoluteConstraints(-3, 2, 1286, 745));
}// </editor-fold>
// Variables declaration - do not modify
public javax.swing.JLabel backgroundscene1;
// End of variables declaration
}
an i have a method that add this Jpnale to another one in a frame, everything is okay because i tested and works, but the problems start when i wanna use this:
ImageIcon fot = new ImageIcon(ruta);
Icon icono = new ImageIcon(fot.getImage().getScaledInstance(this.backgroundscene1.getWidth(), this.backgroundscene1.getHeight(), Image.SCALE_DEFAULT));
Im getting Nullpointerexception.
So, basically, how can i put an image on a Jpanel and resize it?