0

I'm trying to print a single image in a JFrame using JLabel with ImageIcon inside of it(as a part of slide-show like programme). Is it possible to rescale the image to fit the frame in real time?

(When you stretch the window the image should follow by rescaling, not necessarily keeping the proportions right).

Here's my code:

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;


public class Main {
public static void main(String[] args) {
    String path = args[0];
    String time = args[1];
    String size = args[2];

    JLabel jl = new JLabel();
    ImageIcon ii = null;
    JFrame jf = new JFrame();
    jf.setDefaultCloseOperation(3);

    File target = new File(path);
    if (target.isDirectory()) {
        File[] files = target.listFiles();
        for (File file : files) {
            if (is_Image(file)) {


                try {
                    jl.setIcon(null);
                    ii = new ImageIcon(ImageIO.read(file));
                    jl = new JLabel(ii);
                    jl.setIcon(ii);
                    jf.add(jl);
                    jf.repaint();
                    jf.pack();
                    jf.setVisible(true);
                    try {
                        Thread.currentThread().sleep(Long.parseLong(time));
                    } catch (InterruptedException e) {

                    }


                } catch (IOException e) {

                }

            } else {
                jl.setIcon(null);
                jl = new JLabel("Not an image");
                jl.setFont(new Font("Serif", Font.PLAIN, size.charAt(0)));
                jl.setHorizontalAlignment(SwingConstants.CENTER);
                jl.setVerticalAlignment(SwingConstants.CENTER);
                jf.add(jl);
                jf.repaint();
                jf.pack();
                jf.setVisible(true);
                try {
                    Thread.currentThread().sleep(Long.parseLong(time));
                } catch (InterruptedException e) {

                }
                jl.setText(null);
            }

        }

    } else {
        try {
            ii = new ImageIcon(ImageIO.read(target));
        } catch (IOException e) {
            e.printStackTrace();
        }
        jl = new JLabel(ii);
        jl.setIcon(ii);
        jf.add(jl);
        jf.repaint();
        jf.pack();
        jf.setVisible(true);
        try {
            Thread.currentThread().sleep(Long.parseLong(time));
        } catch (InterruptedException e) {

        }
    }

    jl = new JLabel("End of presentation");
    jl.setFont(new Font("Serif", Font.PLAIN, size.charAt(0)));
    jl.setHorizontalAlignment(SwingConstants.CENTER);
    jl.setVerticalAlignment(SwingConstants.CENTER);
    jf.add(jl);
    jf.repaint();
    jf.pack();


}


public static boolean is_Image(File file) {
    try {
        return ImageIO.read(file) != null;
    } catch (Exception e) {
        return false;
    }

}
}
halfer
  • 19,824
  • 17
  • 99
  • 186
nfsu
  • 113
  • 2
  • 10
  • 1
    Check out the [Stretch Icon](https://tips4java.wordpress.com/2012/03/31/stretch-icon/) The other approach would be to custom paint the Icon on a panel and add a ComponentListener to the panel and dynamically repaint on a componentSized event. – camickr May 16 '18 at 14:37
  • 1
    The above is very sub-optimal code. 1) It uses 'magic numbers' for frame close operations & font names. 2) It reads a file just to check if it can be loaded, then immediately discards the file content. 3) It swallows exceptions. 4) It freezes the EDT. 5) It needlessly recreates components. 6) It constructs and shows the GUI off the EDT. 7) .. I really could not be bothered going further with it. It'd be best to toss this mess out completely and start again once you understand the meaning behind the 6 reasons listed. – Andrew Thompson May 16 '18 at 14:41
  • Possible duplicate [Scale the ImageIcon automatically to label size](https://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14553003#14553003); possible duplicate [Resizing icon to fit on JButton in Java?](https://stackoverflow.com/questions/25798156/resizing-icon-to-fit-on-jbutton-in-java/25798462#25798462) – MadProgrammer May 16 '18 at 20:24

0 Answers0