从JFrame中删除图像

时间:2013-06-26 10:32:02

标签: java image swing

我创建了一个JFrame。并设置如下代码所示的布局

    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);

我创建了另一个从特定位置绘制图像的类。

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;
       }
       }

工作正常。我想在用户点击特定图片时删除图片。

这是我的输出窗口。我想从列表中删除图片。enter image description here

2 个答案:

答案 0 :(得分:1)

这是我做的一个例子(厌倦了厌倦研究:))。这绝不是最好的只是向您展示一个示例,单击要删除的图像,然后按删除按钮或 DEL 从面板中删除和图像:

当应用启动时(橙色只显示悬停的重点标签):

enter image description here

通过单击标签选择要删除的图像(边框变为绿色突出显示并将保持这种方式,直到单击另一个标签):

enter image description here

按下删除按钮或键后:

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;
    }
}

答案 1 :(得分:0)

只需找到图像的位置(x和y)和大小(宽度和高度),然后用Graphics对象(g2)在绘制的图像上填充一个矩形,就像这样

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

其中x和y其中是图像所在的位置,宽度和高度是图片的大小