0

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?

Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328

2 Answers2

1

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.

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
0
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
saplingPro
  • 20,769
  • 53
  • 137
  • 195