Suhail Gupta
Suhail Gupta

Reputation: 23226

error adding image object

The following code produces error:

ImageIcon i=new ImageIcon("logo.png");
Image scaleImage=i.getImage().getScaledInstance(10,10,Image.SCALE_DEFAULT);
mainPanel.add(scaleImage);

The error is cannot find method add(Image). Why it is giving this error?

Upvotes: 0

Views: 484

Answers (2)

saplingPro
saplingPro

Reputation: 21329

ImageIcon i=new ImageIcon("logo.png");
Image scaleImage=i.getImage().getScaledInstance(70,70,Image.SCALE_DEFAULT);
ImageIcon ii=new ImageIcon(scaleImage);
JLabel pic=new JLabel(ii);
mainP.add(pic);  // now you can add

Upvotes: 0

Bozho
Bozho

Reputation: 597106

You can't do it like that. JPanel does not accept Image as parameter (that's what the error tells you).

You have two options:

  • draw the image in the panel instead. The solution uses the paintComponent(..) method.
  • use a JLabel and an ImageIcon

The answers to this question will show you how to do it either way.

Upvotes: 1

Related Questions