0

I trying to create a GUI as image below

enter image description here

However, when I run my file, the size of the image still remain the original size

enter image description here

How can I reduce the size of the image so I can add text beside ?

public class HomePage extends JFrame {
  public static void main(String[] args) throws IOException {
    new HomePage();
  }

  public HomePage() throws IOException {
    this.setTitle("Picture Application");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setBackground(Color.yellow);
    BufferedImage myPicture = ImageIO.read(new File("C:\\Users\\seng\\Desktop\\logo.png"));
    myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);
    JLabel picLabel = new JLabel(new ImageIcon(myPicture));
    add(picLabel);
    this.pack();
    this.setVisible(true);
  }
}
  • From what I can tell, this is the answer you're looking for: http://stackoverflow.com/questions/16343098/resize-a-picture-to-fit-a-jlabel – sahilkmr78 Jun 02 '16 at 15:47

1 Answers1

2
myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);

The above statement returns a new BufferedImage which you never reference.

The code should be:

//myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);    
Image scaled = myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);
JLabel picLabel = new JLabel(new ImageIcon(scaled));
camickr
  • 321,443
  • 19
  • 166
  • 288
  • `cannot convert from Image to BufferedImage` –  Jun 02 '16 at 16:02
  • 1
    @Seng, Ok, I had a typo. Any code we post here is not tested. Did you not understand the basic concept that you need to assign the scaled image to a variable in order to use it? Just change the statement to use an `Image` variable. Take the time to read the API when you are given a suggestion in the forum. – camickr Jun 02 '16 at 16:34