我的应用程序可以裁剪图像。我想要实现的是在根据从 MouseListeners 获取的坐标切割图像之前绘制的矩形。这是我的代码:
public class ImageScreenShot extends JPanel implements MouseListener, MouseMotionListener {
ImagePanel im;
int drag_status = 0, c1, c2, c3, c4;
public int getC1() {
return c1;
}
public int getC2() {
return c2;
}
public int getC3() {
return c3;
}
public int getC4() {
return c4;
}
public void cut() {
im = new ImagePanel();
GraphicalUserInterface.getFrame().add(im);
im.addMouseMotionListener(this);
im.addMouseListener(this);
}
public void draggedScreen() throws Exception {
int w = c1 - c3;
int h = c2 - c4;
w = w * -1;
h = h * -1;
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(c1, c2, w, h));
File save_path = new File("screen1.jpg");
ImageIO.write(img, "JPG", save_path);
GraphicalUserInterface.getLabelIcon().setIcon(new ImageIcon(new ImageIcon(img).getImage().getScaledInstance(img.getWidth(),img.getHeight(),Image.SCALE_SMOOTH)));
JOptionPane.showConfirmDialog(null,"Would you like to save your cropped Pic?");
if(JOptionPane.YES_OPTION == 0){
PicChanges.getCurrentLabel();
} else {
PicChanges.getCurrentLabel();
}
System.out.println("Cropped image saved successfully.");
}
@Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
}
我很困惑,因为我不知道如何调用paint()方法,这就是为什么到目前为止,图像已正确裁剪但未绘制矩形。据我了解paintComponent(),当我调用 ImagePanel 类并将其添加MouseListeners到调用该repaint()方法的位置时,我的方法正在工作。
要调用该paint()方法,我需要调用ImageScreenShot类,但在这里出现问题。
所以我的问题是如何调用类中paint()方法调用的repaint()方法?MouseListenersImageScreenShot
慕运维8079593
繁华开满天机
相关分类