Please someone help me... why image panel not showing that image in the frame:

//image panel class
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class ImagePanel extends JPanel {

    private BufferedImage  img;



    public ImagePanel(BufferedImage img) {
        this.img = img;
        Dimension size = new Dimension(500,500);
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
    }

    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(img,0,0,null);
    }

}




//the code for adding the ImagePanel to the frame
try {
    this.image = ImageIO.read(chatClient.class.getResource("abc.JPG"));
} catch (IOException e) {
    e.printStackTrace();
}

this.setLayout(new FlowLayout());//!!!!!!!!!!!
this.add(new ImagePanel(image));
this.validate();

Another problem si if I try this in ImagePanel class:

//gives null pointer exception:
   Dimension size = new Dimension(img.getWidth(),img.getHeight());

It simply does nothing... really annoying. There's no big deal but I just can't see the bug.
Thanks.

Call paintComponent constructor
Use this as the 1st line of code in paintComponent method

super.paintComponent(g);

That's only necessary if there are other things on the JPanel (buttons other panels etc..)

The problem is that your image is null, as for why, I am not sure, but that is the reason that the dimension with img.getWidth(), and height is throwing a null pointer exception, and it's also the reason that draw image is doing nothing, the documentation states that if img is null nothing will happen.

as for why the image is null, I am not sure, It may be that ImageIO doesn't recognize the file type, try another image, or even changing JPG to lowercase, other than that I don't know

Thanks. That jpg had a problem... it was overwritten or something... now works.
I mark this thread as solved.
PLEASE sciwizeh*, can you give me some hints about how is better to communicate over internet (not network) between a Client - applet and Server - desktop java application. I would like to control my computer remotely by taking snapshots from server and transfering them to the client - applet. I opened this thread: http://www.daniweb.com/forums/thread241218.html but none answered. I would be very gratefull.

THANKS AGAIN.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.