Hi!

I would like to zoom in & zoom out an image in JLayeredPane. Now I have a snippet for loading a background image into JLayeredPane. How could I now access ALL content images of JLayeredPane (it could contain multiple images) and zoom them in/out? Perhaps, somebody could send me a good web-link? Thanks!

private void loadBackground() {
        JFileChooser fc = new JFileChooser("c:/");
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        ImageFilter filter = new ImageFilter();
        fc.setFileFilter(filter);

        int returnVal = fc.showDialog(this, "Load");

        if (returnVal == 0) {
            File selFile = fc.getSelectedFile();
            String pathToFile = selFile.getPath();
            try {
                final ImageIcon icon = new ImageIcon(pathToFile);
                JLabel bkg = new JLabel(icon);
                bkg.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
                MainClass.getBackgroundPane().add(bkg, new Integer(2), 0);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else
        {
            System.out.println("The file is not selected.");
        }
    }

Recommended Answers

All 2 Replies

I see you are using ImageIcons in JLabels to show the images. Here's how I would think of this:
I'd create a subclass of JLabel that has an Image and a zoom ratio as instance variables. I'd use getScaledInstance to scale the Image according to the zoom ratio in a setZoom(float ratio) method and use that to update the ImageIcon.
I could have any number of these and use an ArrayList to track them all if necessary.
getScaledInstance has some bad press, although I've found it to be OK, but heres a link to ways round that
http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html

Oh, thanks! It is very helpful. I'll try it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.