0

I tried to make slideshow - images should be showed as the background of the window, so when I am large the window it should be resized. At the start images have right original size, but when I am enlarging the window, images doesn't change their size, there still have original size. I'd like to get it using class which extends JFrame, beucase I made attempt using JPanel - then images was resizeable, but each next image didn't adjust to the original size of the image (e.g. if the image have size 400x500px then frame should have that size but when I enlarge the window manually, it should become larger). Could anyone tell me why I can't resize that images? My window in situation when I changed its' size manually

public class Browser extends JFrame
{
    private JLabel label;

    private final File dir;
    private int period, size;

    private ImageIcon img;
    private boolean end = false;
    private int n = 0;
    private File[] files;
    private String m1 = "Koniec prezentacji", m2 = "Brak obrazka";

    public Browser(String[] args)
    {
        panel = new JPanel();
        setLayout(new BorderLayout());
        dir = new File(args[0]);
        period = Integer.parseInt(args[1]) * 1000;
        size = Integer.parseInt(args[2]);

        files = dir.listFiles();
        Timer timer = new Timer(period, new ActionListener()
          {
                public void actionPerformed(ActionEvent e)
                {
                    if (n < files.length)
                    {
                        setImage(files[n].getPath());
                    }
                    else
                    {
                        end = true;
                        JLabel l = new JLabel(m1, SwingConstants.CENTER);
                        setSize(200, 200);
                        setContentPane(l);
                        ((Timer)e.getSource()).stop();
                    }
                    n++;
                }
          });
        timer.start();


        pack();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(true);
        setVisible(true);
    }

    public void setImage(String imgFileName)
    {
        img = new ImageIcon(imgFileName);
        int w = img.getIconWidth();
        int h = img.getIconHeight();
        label = new JLabel(img);
        if (w != -1 && w != 0 && h != -1 && h != 0) 
        {
          setContentPane(label);
          setSize(w, h);

        } 
        else
        {
            JLabel l = new JLabel(m2, SwingConstants.CENTER);
            setSize(200, 200);
            setContentPane(l);
        }
    }

}
  • You want to put a **panel** into your frame; and you want to make sure that this panel can be resized dynamically. The duplicated question I am pointing to should explain how to do that in detail. – GhostCat May 07 '17 at 17:42
  • Sorry, but I think, that it isn't the same thing. My frame adjust size to size of each image. But it also should adjust size to frame when I change size of the window manually - and it doesn't work (image still have default size). – Michał Bartoś May 07 '17 at 17:55
  • The point: which part of the duplicated answers did you look exactly into? – GhostCat May 07 '17 at 17:56
  • I think you would benefit from overriding `paintComponent` on a `JPanel`, then paint a `BufferedImage` instead of depending on `JLabel` to do the resizing. This way you can specify the behavior exactly how you want. See e.g. this overload of [`Graphics#drawImage`](http://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html#drawImage-java.awt.Image-int-int-int-int-java.awt.image.ImageObserver-) for a simple option. – Radiodef May 07 '17 at 18:01
  • You were told to use `pack()` to resize the frame. You can't just make the frame the size of the image since the frame contains the title bar and border. Don't keep creating a new JLabel. Just replace the Icon of the label using the `setIcon(...)` method. However, the ImageIcon does not scale so instead you will need to use the [Stretch Icon](https://tips4java.wordpress.com/2012/03/31/stretch-icon/). – camickr May 07 '17 at 18:25

0 Answers0