Java - How To Resize An Image In Java

Java - How To Resizing ImageIcon According To jLabel Width And Height In Java

__________________________________________________________________________

In This Java Code We Will See How To Resize In ImageIcon In Java NetBeans Programming Language.

Project Source Code:


package Java_Resize_Image;
import java.awt.Color;
import java.awt.Image;
import javax.swing.*;


public class Project extends JFrame{
    JLabel pic;
    public Project()
    {
        super("Java Resize Image");
        pic = new JLabel();
        pic.setBounds(40, 30, 500, 500);
        ImageIcon image = SetImageSize();
        pic.setIcon(image);

        add(pic);
        setLayout(null);
        setSize(600,600);
        getContentPane().setBackground(Color.decode("#bdb67b"));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }
         //create a function to resize the image 
            public ImageIcon SetImageSize(){
            ImageIcon icon = new ImageIcon("C:/Users/samsng/Desktop/img_resize.jpg");
            Image img = icon.getImage();
            Image newImg = img.getScaledInstance(pic.getWidth(), pic.getHeight(), Image.SCALE_SMOOTH);
            ImageIcon newImc = new ImageIcon(newImg);
     
            return newImc;
        }
public static void main(String[] args){
 
    new Project();
 
     }
}


ALSO SEE:

 Create A Slideshow In Java Swing





Share this

Related Posts

Previous
Next Post »

1 comments:

comments
Anonyme
23 mars 2021 à 03:01 delete

Thank you much!

Reply
avatar