0

I created one JFrame. and set layout shown in below code

    this.setLayout(new GridLayout());
    JPanel panel=new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.add(new PicturePanel1());
    JTabbedPane jtp=new JTabbedPane();
    jtp.addTab("show Images", panel);
    jtp.addTab("Compare Image", new JButton());
    this.add(jtp);

I created another class that draws Images from particular location.

protected void paintComponent(Graphics g) {
   super.paintComponents(g);
   Graphics2D g2 = (Graphics2D) g;
   int k = 20,y=10;
   for (int j = 0; j <= listOfFiles.length - 1; j++) {
       g2.drawImage(b[j], k, y, 100, 100, null);
      // add(new javax.swing.JCheckBox());
       k = k + 120;
       if(k>=960)
       {
           k=20;
           y=y+120;
       }
       }

Its working fine. I want to delete Images when user clicks on particular Image.

This is my output window. I want to delete Image from list.enter image description here

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
Prashant Thorat
  • 1,752
  • 11
  • 33
  • 54
  • what is b[j] and listoffiles? – Tala Jun 26 '13 at 10:43
  • 4
    Remove the image from the `b[]` and call `repaint()`. Speaking of which, this would be easier to do a) If the images were stored in a `ArrayList` or similar structure that allows deleting the n'th element. b) If the images were displayed in a `JLabel` each, to add a mouse listener. c) The labels were in a `JList`.. – Andrew Thompson Jun 26 '13 at 10:44
  • How you want to remove image ? – Makky Jun 26 '13 at 10:47
  • That screen-shot was entirely unnecessary. An SSCCE of your current code, as well as replying to my first comment, would be immeasurably more effective at arriving at a solution. – Andrew Thompson Jun 26 '13 at 13:37

2 Answers2

1

Here is an example I made (bored and tired of studying :)). It is by no means the best just a quick thing to show you an example, click the image you want to delete and then press the delete button or DEL to remove and image from the panel:

When app starts up (orange just shows focused label for hovering):

enter image description here

Image to delete is selected by clicking on the Label (Border becomes green highlighted and will remain this way until another label is clicked):

enter image description here

After delete button or key is pressed:

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class JavaApplication5 {

    public static void main(String[] args) {
        createAndShowJFrame();
    }

    public static void createAndShowJFrame() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                JFrame frame = createJFrame();
                frame.setVisible(true);

            }
        });
    }

    private static JFrame createJFrame() {
        JFrame frame = new JFrame();
        //frame.setResizable(false);//make it un-resizeable
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Test");

        ArrayList<BufferedImage> images = null;

        try {
            images = getImagesArrayList();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        final ImageViewPanel imageViewPanel = new ImageViewPanel(images);
        JScrollPane jsp = new JScrollPane(imageViewPanel);
        jsp.setPreferredSize(new Dimension(400, 400));
        frame.add(jsp);

        JPanel controlPanel = new JPanel();
        JButton addLabelButton = new JButton("Delete Selected Image");
        addLabelButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                imageViewPanel.removeFocusedImageLabel();
            }
        });
        controlPanel.add(addLabelButton);
        frame.add(controlPanel, BorderLayout.SOUTH);
        frame.pack();

        return frame;
    }

    private static ArrayList<BufferedImage> getImagesArrayList() throws Exception {
        ArrayList<BufferedImage> images = new ArrayList<>();
        images.add(resize(ImageIO.read(new URL("http://icons.iconarchive.com/icons/deleket/sleek-xp-software/256/Yahoo-Messenger-icon.png")), 100, 100));
        images.add(resize(ImageIO.read(new URL("https://upload.wikimedia.org/wikipedia/commons/6/64/Gnu_meditate_levitate.png")), 100, 100));
        return images;
    }

    public static BufferedImage resize(BufferedImage image, int width, int height) {
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
        Graphics2D g2d = (Graphics2D) bi.createGraphics();
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
        g2d.drawImage(image, 0, 0, width, height, null);
        g2d.dispose();
        return bi;
    }
}

class ImageViewPanel extends JPanel {

    JLabel NO_IMAGES = new JLabel("No Images");
    ArrayList<BufferedImage> images;
    ArrayList<MyLabel> imageLabels;

    public ImageViewPanel(ArrayList<BufferedImage> images) {

        this.images = images;
        imageLabels = new ArrayList<>();

        for (BufferedImage bi : images) {
            imageLabels.add(new MyLabel(new ImageIcon(bi), this));
        }
        layoutLabels();

        getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, true), "Delete pressed");
        getActionMap().put("Delete pressed", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                removeFocusedImageLabel();
            }
        });
    }

    void removeFocusedImageLabel() {
        if (focusedLabel == null) {
            return;
        }
        imageLabels.remove(focusedLabel);
        remove(focusedLabel);
        layoutLabels();
    }

    private void layoutLabels() {
        if (imageLabels.isEmpty()) {
            add(NO_IMAGES);
        } else {
            remove(NO_IMAGES);
            for (JLabel il : imageLabels) {
                add(il);
            }
        }
        revalidate();
        repaint();
    }
    private MyLabel focusedLabel;

    void setFocusedLabel(MyLabel labelToFocus) {
        if (focusedLabel != null) {
            focusedLabel.setBorder(null);
            focusedLabel.setClicked(false);
        }
        focusedLabel = labelToFocus;

        focusedLabel.setBorder(new LineBorder(Color.GREEN));
    }
}

class MyLabel extends JLabel {

    private final ImageViewPanel imageViewPanel;
    private boolean clicked = false;

    public MyLabel(Icon image, ImageViewPanel imageViewPanel) {
        super(image);
        this.imageViewPanel = imageViewPanel;
        initLabel();
    }

    public MyLabel(String text, ImageViewPanel imageViewPanel) {
        super(text);
        this.imageViewPanel = imageViewPanel;
        initLabel();
    }

    private void initLabel() {
        setFocusable(true);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                clicked = true;
                imageViewPanel.setFocusedLabel(MyLabel.this);
            }

            @Override
            public void mouseEntered(MouseEvent me) {
                super.mouseEntered(me);
                if (clicked) {
                    return;
                }
                setBorder(new LineBorder(Color.ORANGE));
                //call for focus mouse is over this component
                requestFocusInWindow();
            }
        });

        addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                super.focusLost(e);

                if (clicked) {
                    return;
                }

                setBorder(null);
            }
        });
    }

    public void setClicked(boolean clicked) {
        this.clicked = clicked;
    }

    public boolean isClicked() {
        return clicked;
    }
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • 1
    +1. See also [this answer](http://stackoverflow.com/a/16052085/418556) which includes 5 shapes in 4 separate colors. These are good for hot-linking in just such examples. :) – Andrew Thompson Jun 26 '13 at 14:26
  • 1
    +1 lol thanks ill remember that damn because it takes a while to find some nice sized pics (kilobyte wise) and of course something that appeals to the masses (not that any of my above pics do :P) – David Kroukamp Jun 26 '13 at 17:46
  • 1
    *"..appeals to the masses (not that any of my above pics do :P)"* That first image looks like PacMan on crystal meth. *I* like it. :) – Andrew Thompson Jun 26 '13 at 17:55
  • @AndrewThompson +1000 ... Hahahahahahahahahahaha killed me! – David Kroukamp Jun 26 '13 at 18:01
  • good example, but it uses jLable, while @Prashant Thorat used direct Image drawing –  Jun 26 '13 at 19:17
  • @user2511414 that is the not so good way of doing it IMO, depending on the purpose but because the images are being clicked etc its better to use JLabel, they are not just a static background for the panel, they are objects resembling the images chosen – David Kroukamp Jun 26 '13 at 19:25
  • @David Kroukamp I see what you mean, you are right as I said, but sometimes, some people like to do everything implicitly and we cannot blame them, but I think direct drawing in many situation (specially heavy graphical interfaces) would be better, because it has better performance, also user would have X-layout, as he wish –  Jun 26 '13 at 19:58
  • @David Kroukamp Thanx for code it removes Images from JFrame. But does not delete Images permenantly from localhost server(Tomcat). – Prashant Thorat Jun 27 '13 at 05:52
  • 1
    @PrashantThorat glad to be of help. *But does not delete Images permenantly from localhost server(Tomcat)* your question was the removal of the image from the JPanel as far as I understood, not the deletion of a file using TomCat thats a complete different question, but in practice you would query tomcat to delete the file when the `removedFocusImageLabel()` method is called for the appropriate file name – David Kroukamp Jun 27 '13 at 08:55
0

simply find the location (x, and y) and the size(width, and height) of image, then with the Graphics object (g2) fill a rectangle over the image drawn, like this

g2.setColor(this.getBackground());//set the color you want to clear the bound this point to JFrame/JPanel
g2.fillRect(x, y, width, height);

where x and y where is the location that images is located, and width and height are size of the picture