Search code examples
javaimageswingimageicon

Smaller down the image size in JPanel


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);
  }
}

Solution

  • 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));