Resize image in JLabel

2

I have this code, which creates a window and shows a JLabel in which I put an image; the image is large and only shows me a bit. I want to know how to resize the image to be displayed in small but complete.

package pruebasgraficas;

/**
 *
 * @author -Super Mario Bross-
 */
import javax.swing.*;

public class PruebasGraficas extends JFrame{

private static JLabel et1;


    public static void main(String[] args) 
    {
        JFrame Ventana1 = new JFrame();

        Ventana1.setTitle("VENTANA 1");
        Ventana1.setSize(600, 600);
        Ventana1.setLocation(300, 100);
        Ventana1.setVisible(true);

        et1 = new JLabel();
        et1.setSize(300, 300);
        et1.setLocation(25,25);
        et1.setIcon(new ImageIcon ( "logo.jpg" ) ) ;     

        Ventana1.add(et1);

        Ventana1.setLayout(null);
        Ventana1.repaint();
        Ventana1.revalidate();

    }

}
    
asked by Abner 19.03.2017 в 03:43
source

1 answer

1

ImageIcon is created by default with the size it finds in the JLabel so that it adjusts to it and it is not possible to visualize it well. You can use make a getImage() of the abstract class Image

Image img= new ImageIcon("logo.png").getImage();
ImageIcon img2=new ImageIcon(img.getScaledInstance(78, 124, Image.SCALE_SMOOTH));

et1.setIcon(img2);
    
answered by 23.03.2017 в 06:14