BufferedImage piece missing on buttonPressed

its just too weird. there is a JPanel above a few buttons. On actionPerformed on one of them, i load an image to the JPanel. everything ok so far. The file is found, the image is rendered. immediately after, a big purple rectangle(???) is drawn over my image. The same color as the panel background, is like the piece of the image got deleted. I think is something with the repaint, but ignoring it or not, the problem persists. Even weirder, it seems to happen less often (usually only in the first time the button is clicked) if i use an image the same size of the panel (without scaling/stretching). The calls to load and display the image are the usual:


            /*concats the file path, results in something like "../Media/image.png"*/
            String selected = new String("../Media/" +h.getPictureName()+ ".png");  

            URL imageURL = getClass().getResource(selected);
            BufferedImage bim = null;
            try {
                bim = ImageIO.read(imageURL);
            } 
            catch (IOException ex) {
                ex.printStackTrace();
            }
            Graphics2D g2 = (Graphics2D)imagePanel.getGraphics();
            g2.drawImage(bim, 0, 0, imagePanel.getWidth(), imagePanel.getHeight(), null);

Any ideas are welcome, as are good exorcists contacts.

btw, im using java6, with netbeans 5.5

I believe you need to overwrite the ImagePanel’s paint(Graphics g) function…

sounds reasonable enough. To my surprise, i found a different bug.

Now, on the constructor of the JFrame that has the panel, after Netbeans´ initComponents(i am using the form editor), i add the following to the code:


imagePanel = new JPanel(){public void paint(Graphics g){
                                                                                                             g.setColor(Color.GREEN);
                                                                                                             g.fillRect(0, 0, 100, 100);
                                                                                                         };};
        imagePanel.setPreferredSize(new Dimension(300, 400));

.

later on, on the actionPerformed event, i call:


            Graphics g = imagePanel.getGraphics();
            System.out.println("imagePanel´s Dimension: " + imagePanel.getWidth() + "," + imagePanel.getHeight());
            System.out.println("g: " + g);
            //imagePanel.paint(g);

Turns out g is null, and even with imagePanel not null, does not have width or height. so i am not even calling paint yet, to see if the repaint fixed the problem from the other post.

what am i forgetting?