ASH_534 0 Newbie Poster

I have a problem with a DnD program, so when I add 2 or more images and apply a filter to one of the images (using a right click), it only appears on the last image added.

I think i have to store the image in an Array while waiting for the user to decide to use or not the right click to add a filter to one or all the imported images (via DnD). if u have any idea how to do that please help :)

Here's my code:

public class CompressedImage {
    public static StretchIcon getPhotoSI(String imageStr) {
        return new StretchIcon(imageStr);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }

        JFrame f = new JFrame("Testing");
        JLabel l = new JLabel("Drop here");
        JDesktopPane jdp = new JDesktopPane();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(1000, 600);
        f.setLocationRelativeTo(null);
        f.add(l);
        l.setHorizontalAlignment(JLabel.CENTER);
        f.setVisible(true);

        l.setTransferHandler(new TransferHandler() {
            private static final long serialVersionUID = 1L;
            public static final DataFlavor[] SUPPORTED_DATA_FLAVORS = new DataFlavor[] { DataFlavor.javaFileListFlavor,
                    DataFlavor.imageFlavor };

            @Override
            public boolean canImport(TransferHandler.TransferSupport support) {
                System.out.println("1");
                boolean canImport = false;
                for (DataFlavor flavor : SUPPORTED_DATA_FLAVORS) {
                    if (support.isDataFlavorSupported(flavor)) {
                        canImport = true;
                        break;
                    }
                }
                return canImport;
            }

            StretchIcon icon;
            Image image;
            List<?> files;
            Component component;
            Transferable t;

            public boolean importData(TransferHandler.TransferSupport support) {
                System.out.println("2");
                int openFrameCount = 0;
                final int xOffset = 30;
                final int yOffset = 30;
                JInternalFrame mb = new JInternalFrame("Frame title4" + (++openFrameCount), true, true, true);
                mb.setLocation(xOffset * openFrameCount, yOffset * openFrameCount);
                JLabel la = new JLabel();
                jdp.add(mb);
                l.add(mb);
                mb.add(la);

                class PopUpDemo extends JPopupMenu {
                    private static final long serialVersionUID = 1L;
                    JMenuItem anItem;

                    public PopUpDemo() {
                        anItem = new JMenuItem("Gray Scale");
                        add(anItem);
                        System.out.println("Right click Creadted");
                        anItem.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent evt) {
                                jMenuItem1ActionPerformed(evt);
                            }

                            private void jMenuItem1ActionPerformed(ActionEvent evt) {
                                System.out.println("filter");

                                int width = image.getWidth(la);
                                int height = image.getHeight(la);
                                for (int y = 0; y < height; y++) {
                                    for (int x = 0; x < width; x++) {
                                        int p = ((BufferedImage) image).getRGB(x, y);
                                        int a = (p >> 24) & 0xff;
                                        int r = (p >> 16) & 0xff;
                                        int g = (p >> 8) & 0xff;
                                        int b = p & 0xff;
                                        int avg = (r + g + b) / 3;
                                        p = (a << 24) | (avg << 16) | (avg << 8) | avg;
                                        ((BufferedImage) image).setRGB(x, y, p);
                                    }
                                }
                            }
                        });
                    }
                }
                class PopClickListener extends MouseAdapter {
                    public void mousePressed(MouseEvent e) {
                        if (e.isPopupTrigger())
                            doPop(e);
                        mb.requestFocusInWindow();
                        la.requestFocusInWindow();
                    }

                    public void mouseReleased(MouseEvent e) {
                        if (e.isPopupTrigger())
                            doPop(e);
                        mb.requestFocusInWindow();
                        la.requestFocusInWindow();

                    }

                    private void doPop(MouseEvent e) {
                        PopUpDemo menu = new PopUpDemo();
                        menu.show(e.getComponent(), e.getX(), e.getY());
                    }
                }
                mb.addMouseListener(new PopClickListener());

                if (canImport(support)) {
                    try {
                        t = support.getTransferable();
                        component = support.getComponent();
                        if (component instanceof JLabel) {
                            image = null;
                            if (support.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                                image = (BufferedImage) t.getTransferData(DataFlavor.imageFlavor);
                            } else if (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                                files = (List<?>) t.getTransferData(DataFlavor.javaFileListFlavor);
                                if (files.size() > 0) {
                                    System.out.println(": " + files.get(0));// path
                                    System.out.println(": " + files);// path
                                    image = ImageIO.read((File) files.get(0));

                                    // Internal Frame Size & Show
                                    if (image.getWidth(la) < 120 || image.getHeight(la) < 120) {
                                        mb.setSize(image.getWidth(la) * 2, image.getHeight(la) * 2);
                                        mb.setMinimumSize(
                                                new Dimension(image.getWidth(la) * 2, image.getHeight(la) * 2));
                                    } else if (image.getWidth(la) > 1200 || image.getHeight(la) > 1200) {
                                        mb.setSize(image.getWidth(la) / 4, image.getHeight(la) / 4);
                                        mb.setMinimumSize(new Dimension(180, 180));
                                    } else {
                                        mb.setSize(image.getWidth(la), image.getHeight(la));
                                        mb.setMinimumSize(new Dimension(180, 180));
                                    }
                                    mb.show();
                                    // End Internal Frame Size & Show

                                }
                            }
                            icon = null;
                            if (image != null) {
                                icon = new StretchIcon(image);
                            }
                            la.setIcon(icon);

                            return true;
                        }
                    } catch (Exception exp) {
                        exp.printStackTrace();
                    }
                }
                return true;
            }
        });

    }
}

(i'm still new to java and swing)
Thank you in advance.

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.