【问题标题】:How do I get mouse movements work properly for several different ImageIcons inside a JPanel?如何让 JPanel 内的几个不同 ImageIcon 的鼠标移动正常工作?
【发布时间】:2016-03-28 18:23:32
【问题描述】:

我目前正在尝试使用 Swing 编写我的第一个非常简单的图像处理程序。应该可以浏览并添加图像作为 ImageIcons 并在屏幕上移动这些图像。照片应在添加时相互叠加,单击图像应将其删除。

程序运行,但我遇到以下功能问题。

这是我的代码:

import javax.swing.*;
import java.awt.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

class Image {
    private int x = 80;
    private int y = 80;
    private String filepath;
    private ImageIcon image;

    public Image(int x, int y, String filepath){
        this.x = x;
        this.y = y;
        this.filepath = filepath;
        image = new ImageIcon(filepath);
    }

    public Image(String filepath){
        this.filepath = filepath;
        image = new ImageIcon(filepath);
    }

    public void draw(Graphics g){
        g.drawImage(image.getImage(), x, y, null);

    }

    public void undraw (Graphics g, Color c ){
        g.setColor(c);
        g.fillRect(x,y,image.getIconWidth(), image.getIconHeight());

    }

    public boolean containsXY (int x, int y){

        if ( this.x <= x && this.y <= y){
            return true;
        }

        return false;
    }

    public void move (Graphics g, int x, int y) {
        undraw(g, Color.WHITE);
        this.x = x;
        this.y = y;

        draw(g);
    }

}


class PaintSurface extends JPanel implements MouseListener, MouseMotionListener {

    private int x, y;
    private JButton browse;
    private Collection <Image> images = new ArrayList<Image>();
    private final JFileChooser fc = new JFileChooser();
    private Image selected;

    public PaintSurface(JButton b){
        browse = b;
        addMouseListener(this);
        addMouseMotionListener(this);
        browse.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF", "jpg", "gif");
                fc.setFileFilter(filter);
                fc.showOpenDialog(browse);
                buttonPressed(fc);

            }
        });
    }

    public void paintComponent (Graphics g){
        super.paintComponent(g);

        for (Image i: images){
            i.draw(g);

        }

    }

    public void addImage(Image i){
        images.add(i);

        Graphics g = getGraphics();
        i.draw(g);

    }


    public void buttonPressed(JFileChooser fc){
        File selectedFile = fc.getSelectedFile();

        Image i = new Image(x, y, selectedFile.getAbsolutePath());

        addImage(i);
        repaint();
    }

    public Image findImage(int x, int y){
        for (Image i: images){
            if (i.containsXY(this.x, this.y)){
                return i;

            }

        }
        return null;
    }

    public boolean removeImage (Image i){

        Graphics g = getGraphics();
        i.undraw(g, Color.WHITE);

        return images.remove(i);
    }

    public void moveImage (Image i, int x, int y) { //

        i.move(getGraphics(), x, y);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        x = e.getX();
        y = e.getY();

        Image i = findImage(x, y);

        if (i != null) {
            removeImage(i);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();

        selected = findImage(x,y);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();

        if (selected != null) {
            moveImage(selected,x,y);
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {

    }
}


class GUI extends JFrame {

    public GUI(){
        super("ImageApp");

        JLabel instruction = new JLabel("Clicking anywhere on the screen will set the location for the next added image.");
        JButton browse = new JButton("Add image");
        JPanel panel1 = new JPanel();
        JPanel panel2 = new PaintSurface(browse);
        panel1.setLayout(null);
        panel2.setBackground(Color.WHITE);
        instruction.setAlignmentX(CENTER_ALIGNMENT);

        getContentPane().setBackground(Color.WHITE);
        getContentPane().add("North", instruction );
        getContentPane().add("South", browse);


        add(panel1);
        add(panel2);
        setBounds(300,0,800,800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);

    }

}

public class PhotoApp extends GUI {

    public static void main (String[] args){
        GUI PhotoApplication = new GUI();

    }
}

这些是问题:

  • 当我添加了多个图像时,鼠标功能突然停止工作,我无法移动任何图像。他们不知何故被“卡住”了。
  • 当我添加了多张图片时,我只能控制(移动)最新添加的一张。
  • 单击以删除图像时,即使我没有单击图像内部而是靠近图像,它也会被删除。如果我点击远离图像的地方,它不会被删除(所以它有点工作)。只有在图片内部点击才会被移除!
  • 点击添加图片时,如果我按“取消”,我会收到一堆错误
  • 图片应该是相互叠加的,但是当我现在移动图片时,它会“擦除”旧图片。

有人可以帮忙吗?我会很感激,因为我已经被困了很长时间。

【问题讨论】:

  • 你不需要把同样的问题列出两次。
  • 在您的 buttonPressed() 方法中,您创建一个新图像,并传递 x 和 y。他们的价值观是什么?你从来没有设置它们
  • @FredK 但我确实将它们设置为 x 和 y,在 mouseClicked 方法中设置为最新的 mouseclick 值?
  • @OfeliavanAnalhard 你知道你可以接受解决你问题的答案吗?

标签: java swing jpanel


【解决方案1】:

我已经为你的一些问题概述了有问题的代码。

当我添加了几张图片时,我只能控制(移动)最近添加的一张。

您只返回一个:public Image findImage(int x, int y)。并且:

单击删除图像时,即使我没有单击图像内部而是靠近图像,它也会被删除。如果我点击远离图像的地方,它不会被删除(所以它有点工作)。只有在图片内部点击才会被移除!

您的containsXY() 似乎在这里搞砸了:if ( this.x &lt;= x &amp;&amp; this.y &lt;= y)。提示:x = 0 和 y = 0 的 1 像素方形图像不包含 x = 50, y = 50。这导致了我解决的第一个问题的一部分。

点击添加图片时,如果我按“取消”,我会收到一堆错误

如果您不选择一个,它将不会返回文件: File selectedFile = fc.getSelectedFile();

这些图片应该是相互叠加的,但是当我现在移动一张图片时,它会“擦除”旧的图片。

你是undrawWHITE: g.fillRect(x,y,image.getIconWidth(), image.getIconHeight());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多