gpt4 book ai didi

java - 如何在我的 mouseDragged 事件中访问 ImageIcons

转载 作者:行者123 更新时间:2023-11-29 07:35:44 25 4
gpt4 key购买 nike

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

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

我的 paintComponent 将同时移动所有 (3) 个图像。

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

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

最佳答案

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

因为你不能使用 JLabels 并且如果你想获得当前图像被选中。您将不得不遍历图像列表并检查选择了哪一个。

目前您保留了一个 ImageIcon 列表,没有直接方法获取 ImageIcon 的边界以检查选择。

如果我是你,我会给当前的ImageIcon添加一个属性(bounds),方便我们判断图片是否被鼠标点击:

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 的位置。因此只有最后选择的图像会移动。


在您的 paintComponent(g) 方法中,如果您创建了一个自定义的 Image 类,例如 MyImage,您可以只使用 .draw(g) :

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

这是我以前用同样的技巧做的拼图游戏:

enter image description here

关于java - 如何在我的 mouseDragged 事件中访问 ImageIcons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36158406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com