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:
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
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
1 comments:
commentsThank you much!
Reply