1

So here's my code.. I can't make it work I have a created Panel made with palette in netbeans, it also has a couple of Labels and Buttons. I need to set a background to the panel and everything over it. What's wrong?

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class PanelPrincipal extends javax.swing.JPanel {

class ImagePanel extends JPanel {

    private Image img;
    public ImagePanel(String img) {
        this(new ImageIcon(img).getImage());
    }

    public ImagePanel(Image img) {
        this.img = img;
        Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
    } 
public void paintComponent(Graphics g){
    g.drawImage(img,0,0, null); 
}
}
        /**
         * Creates new form PanelPrincipal
         */
    public PanelPrincipal(VentanaPrincipal ventanaPrincipal) {
        initComponents();
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Jun 15 '14 at 07:13
  • My first concern would be not calling super.paintComponent, the other would be knowing where the images are – MadProgrammer Jun 15 '14 at 07:14
  • `public void paintComponent(Graphics g){ g.drawImage(img,0,0, null); ..` should be `public void paintComponent(Graphics g){ super.paintComponent(g); g.drawImage(img,0,0, this); ..` – Andrew Thompson Jun 15 '14 at 07:14
  • 1) `setLayout(null);` not necessary when adding no components, not workable when adding components. 2) By the time of deployment, that image will likely become an [tag:embedded-resource]. That being the case, it must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. – Andrew Thompson Jun 15 '14 at 07:15
  • May be the directory structure is wrong. Please have a look at this [answer](http://stackoverflow.com/a/9866659/1057230), hopefully it might be able to guide you :-) – nIcE cOw Jun 15 '14 at 10:30

0 Answers0