问题描述
我编写了一个具有用户剪切图像能力的应用程序.当前,下面的代码将图像剪切到绘制矩形上方一点(其目的是为了显示用户实际上要剪切图像的坐标).
I write an application with the ability for the user to cut images.Currently, the code below cut the image a little bit above the drawn rectangle(whose purpose is to show with which coordinates the user wants to cut the image, actually.)
public class ScreenCaptureRectangle extends JFrame implements MouseListener, MouseMotionListener {
int drag_status = 0, c1, c2, c3, c4;
public void cut() {
ImagePanel im = new ImagePanel(PicChanges.getNewImage());
JScrollPane scrollPane = new JScrollPane(im);
add(scrollPane);
setSize(500, 400);
setVisible(true);
im.addMouseListener(this);
im.addMouseMotionListener(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);
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) {
}
@Override
public void mousePressed(MouseEvent arg0) {
repaint();
c1 = arg0.getX();
c2 = arg0.getY();
}
@Override
public void mouseReleased(MouseEvent arg0) {
repaint();
if (drag_status == 1) {
c3 = arg0.getX();
c4 = arg0.getY();
try {
repaint();
draggedScreen();
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Issue is under construction");
}
}
@Override
public void mouseDragged(MouseEvent arg0) {
repaint();
drag_status = 1;
c3 = arg0.getX();
c4 = arg0.getY();
}
@Override
public void mouseMoved(MouseEvent arg0) {
}
public void paint(Graphics g) {
super.paint(g);
int w = c1 - c3;
int h = c2 - c4;
w = w * -1;
h = h * -1;
if (w < 0)
w = w * -1;
g.setColor(Color.RED);
g.drawRect(c1, c2, w, h);
}
public class ImagePanel extends JPanel {
private BufferedImage imageToCut;
public ImagePanel(BufferedImage img) {
this.imageToCut = img;
Dimension size = new Dimension(imageToCut.getWidth(null), imageToCut.getHeight(null));
setPreferredSize(size);
setMaximumSize(size);
setMinimumSize(size);
setSize(size);
}
@Override
protected void paintComponent(Graphics g) {
g.drawImage(imageToCut, 0, 0, null);
}
}
到目前为止,当用户以这种方式裁剪图像时:
他得到了:
我的目标是获得以下结果:
我使用鼠标侦听器来获取绘制的矩形的坐标,然后使用这些坐标来剪切图像.现在,图像被剪切为不正确的高度(如我所见).如果有人可以告诉我代码中有什么错误,我将不胜感激.谢谢!
I use mouse listeners so to get the coordinates of the drawn rectangle and then using these coordinates cut the image. Now the image is cut with incorrect height(as I see it). I would appreciate it if anyone could tell me what can be wrong in the code?Thanks!
推荐答案
感谢我收到的建议,现在我没有为用户单击剪切"按钮后要剪切的照片打开单独的框架,我只是添加了JFrame的MouseListeners,更加方便和用户友好.根据MouseListeners的坐标,照片被正确裁剪了:
Thanks to the recommendations I received, now I don't open a separate frame for a photo to cut after a user clicks on the Cut button, I just add MouseListeners to JFrame, which is more convenient and user-friendly. The photo is cut correctly, according to the coordinates from the MouseListeners:
public class ImageScreenShot extends JFrame implements MouseListener, MouseMotionListener {
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() {
GraphicalUserInterface.getFrame().addMouseMotionListener(this);
GraphicalUserInterface.getFrame().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) {
/**Need to implement some code*/
} else {
/**Need to implement some code*/
}
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) {
}
@Override
public void mousePressed(MouseEvent arg0) {
c1 = arg0.getXOnScreen();
c2 = arg0.getYOnScreen();
}
@Override
public void mouseReleased(MouseEvent arg0) {
if (drag_status == 1) {
c3 = arg0.getXOnScreen();
c4 = arg0.getYOnScreen();
try {
draggedScreen();
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Issue is under construction");
}
}
@Override
public void mouseDragged(MouseEvent arg0) {
drag_status = 1;
c3 = arg0.getXOnScreen();
c4 = arg0.getYOnScreen();
}
@Override
public void mouseMoved(MouseEvent arg0) {
}
这篇关于为什么不根据paintComponent方法中的矩形绘制BufferedImage(它的高度计算错误)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!