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.