gpt4 book ai didi

java - Repaint()方法不会一一调用paint()和paintComponent()方法,只有paintComponent()方法起作用

转载 作者:行者123 更新时间:2023-12-02 09:58:08 27 4
gpt4 key购买 nike

我的应用程序可以裁剪图像。我想要实现的是根据从 MouseListener 获取的坐标切割图像之前绘制的矩形。这是我的代码:

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) {
}

@Override
public void mousePressed(MouseEvent arg0) {
repaint();
c1 = arg0.getXOnScreen();
c2 = arg0.getYOnScreen();
}

@Override
public void mouseReleased(MouseEvent arg0) {
repaint();
if (drag_status == 1) {
c3 = arg0.getXOnScreen();
c4 = arg0.getYOnScreen();
try {
this.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.getXOnScreen();
c4 = arg0.getYOnScreen();
}

@Override
public void mouseMoved(MouseEvent arg0) {
}

@Override
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);
System.out.println("paint component");

}
}
<小时/>
public class ImagePanel extends JPanel {
private BufferedImage imageToCut;

public ImagePanel() {
Dimension size = new
Dimension(GraphicalUserInterface.getLabelIcon().getSize());
setPreferredSize(size);
setMaximumSize(size);
setMinimumSize(size);
setSize(size);
}

@Override
protected void paintComponent(Graphics g) {
g.drawImage(imageToCut, 0, 0, null);
System.out.println("painted");
}
}

我很困惑,因为我不知道如何调用 paint() 方法,这就是为什么到目前为止,图像已正确裁剪,但未绘制矩形。据我了解,当我调用 ImagePanel 类并向其添加 MouseListeners 时,我的 paintComponent() 方法正在工作,其中 repaint() 方法所在的位置已调用。

要调用 paint() 方法,我需要调用 ImageScreenShot 类,但这里出现了问题。

所以我的问题是如何调用 ImageScreenShot< 的 MouseListeners 中的 repaint() 方法调用的 paint() 方法 类?

最佳答案

我还没有测试过你的代码,但乍一看我可以看到:

  1. 您正在扩展 JPanel,这很好,但是您正在重写 paint(...) 方法,您不应该这样做,您需要覆盖 paintComponent(...)

  2. 在你的第二个类中,你重写了 paintComponent(...) 但你没有调用

    super.paintComponent(g);

    这正在打破油漆链。可能(以及第一点)是您错误的原因。

  3. 参见Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (是的),您需要在应用程序中重写 getPreferredSize() 并调用 pack()

关于java - Repaint()方法不会一一调用paint()和paintComponent()方法,只有paintComponent()方法起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55844963/

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