如何在mouseDragged事件中访问ImageIcons [英] How to access ImageIcons in my mouseDragged event

查看:165
本文介绍了如何在mouseDragged事件中访问ImageIcons的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图弄清楚如何在我的事件中访问使用paintComponent绘制的不同图像(在分配中不允许使用JLabel)。

I'm trying to figure out how to access my different images, drawn with paintComponent, (using JLabels is not allowed in the assignment) in my events.

拖动时,我只想用鼠标拖动移动一个图像,我似乎无法使用e.getSource()访问当前图像。

When dragging, I only want one image to move with my mouse drag and I cant seem to access "current image" with e.getSource().

我的 paintComponent 此时会同时移动所有(3)张图片。

My paintComponent will move all (3) images at the same time at the moment.

我的问题是:如何使用mouseDragged获取单个ImageIcon?

My question is: how to get hold of a single ImageIcon with my mouseDragged?

public class PhotoPanel extends JPanel implements MouseListener, MouseMotionListener {

private java.util.List<ImageIcon> myList = new ArrayList<>();
private int mx, my;

private ImageIcon image1 = new ImageIcon("src/resources/gira.gif");
private ImageIcon image2 = new ImageIcon("src/resources/stru.gif");
private ImageIcon image3 = new ImageIcon("src/resources/back.gif"); 

public PhotoPanel()
{
    setBackground(Color.GREEN);

    myList.add(image1);
    myList.add(image2);
    myList.add(image3);

    //Is this a problematic way of doin it?
    addMouseMotionListener(this);

}

public void paintComponent (Graphics g) {

    super.paintComponent(g);

    for (ImageIcon i : myList)
    {
       g.drawImage(i.getImage(), mx, my, this);
    }    
}


@Override
public void mouseDragged(MouseEvent e) {

    //if(e.getSource == image1)
    //{
    //    Manipulate single picture, but not working this way  
    //}
    mx = e.getX();
    my = e.getY();

    repaint();
    }    
}


推荐答案


我正试图弄清楚如何访问我的不同图像,使用paintComponent绘制,(在作业中不允许使用JLabel)

I'm trying to figure out how to access my different images, drawn with paintComponent, (using JLabels is not allowed in the assignment) in my events

由于您无法使用JLabel,并且您想要选择当前图像。您将不得不遍历图像列表添加检查选择哪一个。

Since you can't use JLabels and if you want to get the current image being selected. You will have to iterate through the list of images add check which one is selected.

目前您保留一个ImageIcon列表,并且没有直接的方法来获取边界用于检查选择的ImageIcon。

Currently you keep a list of ImageIcon, and there is no direct means to get the bounds of ImageIcon to check for selection.

如果我是你,我会在当前的ImageIcon中添加一个属性(边界),以方便我们检查图像是否被点击通过鼠标:

If I were you, I will add a property (bounds) to the current ImageIcon to facilitate us to check whether an image is being clicked by the mouse:

class MyImages extends Rectangle
{
    private ImageIcon image;  //personally, I prefer to use BufferedImage here

    public MyImages(int x, int y, int width, int height){
        setBounds(x, y, width, height);
    }

    //getters & setters for image not shown

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

    //Check if current image is selected:
    public boolean isSelected(int xCoord, int yCoord){
        return (this.contains(xCoord, yCoord))
    }
}






在您的PhotoPanel课程中:

//Crate a list of MyImage instead of ImageIcon
ArrayList<MyImage> myList = new ArrayList<MyImage>();
MyImage selectedImage;






在你的MouseMotionListener类:

@Override 
public void mousePressed(MouseEvent e){

    //To get the image which is selected:
    for(MyImage img : myList)
        if(img.isSelected(e.getX(), e.getY())){  //if mouse clicks on this image
            selectedImage = img;    
            break;
        }
}

@Override
public void mouseDragged(MouseEvent e){
    if(selectedImage != null){
        selectedImage .setLocation(e.getX()-(pieceWH/2), e.getY()-(pieceWH/2));
        repaint();
    }
}

我维护一个实例调用 selectedImage ,在鼠标拖动时,我们将仅更改 selectedImage 的位置。因此,只有最后选择的图像才会移动。

I maintain an instance call the selectedImage, and on mouse drag, we will change the location of selectedImage only. Hence only the last selected image will move.

paintComponent(g)方法,你可以使用 .draw(g)如果你创建了一个自定义的Image类,如 MyImage

In your paintComponent(g) method, you could just use .draw(g) if you created a customized Image class like MyImage:

@Override
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    for (MyImage i : myList)
        i.draw(g);
}

这是我过去使用相同技术所做的曲折拼图:

This is a jig-saw puzzle I did with the same technique in the past:

这篇关于如何在mouseDragged事件中访问ImageIcons的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆