gpt4 book ai didi

java - 如何让鼠标移动在 JPanel 内的多个不同 ImageIcons 上正常工作?

转载 作者:行者123 更新时间:2023-12-02 03:46:22 26 4
gpt4 key购买 nike

我目前正在尝试使用 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();

}
}

这些是问题:

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

有人可以帮忙吗?我会非常感激,因为我已经被这个问题困扰了很长时间。

最佳答案

我已经针对您的一些问题概述了有问题的代码。

When I have added several images, I can only control (move) the latest added one.

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

When clicking to remove an image, it is removed even though I don't click inside the image but close to the image. It isn't removed if I click far away from the image (so it's sort of working). It is only supposed to be removed if clicking inside the image!

您的containsXY()这里似乎很困惑:if ( this.x <= x && this.y <= y) 。提示:x = 0 且 y = 0 的 1 像素方形图像不包含 x = 50、y = 50。这导致了我解决的第一个问题的一部分。

When clicking to add an image, if I press "Cancel" I get a bunch of errors

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

The pictures are supposed to be layered on top of each other, but when I move an image around now it "erases" the older images.

您是undraw与白色搭配: g.fillRect(x,y,image.getIconWidth(), image.getIconHeight());

关于java - 如何让鼠标移动在 JPanel 内的多个不同 ImageIcons 上正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36268491/

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