Repaint() 方法不调用paint() &PaintComponent() 方法一一,只有paintComponent() 方法有效 [英] Repaint() method doesn't invoke paint() & paintComponent() methods one by one, only paintComponent () method is working

查看:75
本文介绍了Repaint() 方法不调用paint() &PaintComponent() 方法一一,只有paintComponent() 方法有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My application can crop images. What I want to achieve is rectangle to be drawn before image is cut according to the coordinates taken from the MouseListeners. This is my code:

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() 方法,这就是为什么到目前为止,图像已被正确裁剪但未绘制矩形.据我所知,我的 paintComponent() 方法正在运行,因为我调用 ImagePanel 类并将 MouseListeners 添加到 repaint() 方法所在的位置调用.

I'm confused as I can't figure out how to invoke paint() method, that's why as of now, the image is cropped and correctly but the rectangle is not drawn. As far as I understand my paintComponent() method is working as I invoke ImagePanel class and add MouseListeners to it where the repaint() method is called.

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

To invoke the paint() method I need to invoke ImageScreenShot class but here where the issues arise.

所以我的问题是如何调用ImageScreenShotMouseListenersrepaint()方法调用的paint()方法/code> 类?

So my question is how to invoke paint() method called by repaint() method in the MouseListeners of ImageScreenShot class?

推荐答案

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

I haven't tested your code, but at first glance I can see that:

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

  1. You're extending JPanel, that's fine, but you're overriding paint(...) method, you should not do that, you need to override paintComponent(...)

在你的第二节课上,你覆盖了 paintComponent(...) 但你没有调用

On your second class you're overriding paintComponent(...) but you're not calling

super.paintComponent(g);

这正在打破油漆链.并且可能(以及第 1 点)是导致您错误的原因.

Which, is breaking the paint chain. And probably (along with 1st point) is the cause of your error.

我应该避免吗Java Swing中set(Preferred|Maximum|Minimum)Size方法的使用?(是),需要重写getPreferredSize()并调用pack()code> 在您的应用程序中.

See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes), you need to override the getPreferredSize() and call pack() in your application.

这篇关于Repaint() 方法不调用paint() &amp;PaintComponent() 方法一一,只有paintComponent() 方法有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆