Priyank
Priyank

Reputation: 37

Getting wrong output in adding an image in JFrame

My problem is that when I create or draw an image in JFrame by using

public void paint(Graphics g)

{}

Method I am getting a black screen instead of the image the problem code snippet is

ImageIcon i=new ImageIcon("logo.png);
Image im=i.getImage();
public void paint(Graphics g)
{
g=getGraphics();
}

Please suggest me an alternative method or solution to my problem Thanks in advance

Upvotes: 0

Views: 54

Answers (1)

XFCC
XFCC

Reputation: 384

Would you consider using a JPanel and overriding the paintComponent method? Something like this:

BufferedImage image = ... //i'll leave this blank since there are several ways to get a bufferedimage. I'll leave an eg: ImageIO.read(new File("/path/to/image"));
        JPanel pane = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, null);
        }};

and then adding the panel to your frame. You can do the same with the container in the JFrame. The logic is simillar.

Upvotes: 2

Related Questions