简体   繁体   English

重绘时未调用paintComponent()

[英]paintComponent() not being called when repainting

I have been working on a program that requires the painting of lines on an image when triggered by a mouse drag event, however within my code when the call to paintComponent is made through repaint() the paintComponent method doesn't execute. 我一直在研究一个程序,该程序需要在通过鼠标拖动事件触发时在图像上绘制线条,但是在我的代码中,当通过repaint()调用paintComponent时,paintComponent方法不会执行。 I have read the tutorials on swing graphics and also had some input from other programmers on this but have so far not been able to find a solution that works for me. 我已经阅读了有关摆动图形的教程,也从其他程序员那里获得了一些建议,但是到目前为止,还没有找到适合我的解决方案。

I have posted a small SSCCE to help pinpoint the area of code from within my program that isn't acting as expected in an attempt to simplify this for myself and anyone looking over my code. 我发布了一个小的SSCCE,以帮助查明程序中未按预期方式运行的代码区域,以便为自己和查看我的代码的所有人简化此过程。

Thanks in advance for anyone that takes the time to look this over for me. 在此先感谢您抽出宝贵时间为我审阅此文件。

Below are the two separate classes I am using. 以下是我正在使用的两个单独的类。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class TestGraphics {
private JLayeredPane contentPane;

public void newImage() {
    try {
        JFileChooser fileChooser = new JFileChooser(".");
        int status = fileChooser.showOpenDialog(null);

        if (status == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            System.out.println("The selected file is from the: " + selectedFile.getParent() + " Drive");
            System.out.println("Name of file: " + selectedFile.getName());
            System.out.println("Opening file");

            BufferedImage buffImage = ImageIO.read(new File(selectedFile.getAbsolutePath()));
            ImageIcon image = new ImageIcon(buffImage);
            JLabel label = new JLabel(image);
            label.setSize(label.getPreferredSize());

            label.setLocation(0, 0);

            contentPane = new JLayeredPane();
            contentPane.setBackground(Color.WHITE);
            contentPane.setOpaque(true);
            //getTabbedPane().setComponentAt(tabNum, contentPane);
            contentPane.add(label);
            contentPane.setPreferredSize(new Dimension(label.getWidth(), label.getHeight()));

            Segmentation segmentation = new Segmentation();
            segmentation.addListeners(label); //call to addListeners method

            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(contentPane);
            frame.setResizable(false);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);

        } else if(status == JFileChooser.CANCEL_OPTION) {
            JOptionPane.showMessageDialog(null, "Canceled");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            TestGraphics tg = new TestGraphics();
            tg.newImage();
        }
    });
}

}

and the other. 和另一个。

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;

public class Segmentation extends JLabel {

private static final long serialVersionUID = -1481861667880271052L;  // unique id   
private static final Color LINES_COLOR = Color.red;
public static final Color CURRENT_LINE_COLOR = new Color(255, 200, 200);
ArrayList<Line2D> lineList = new ArrayList<Line2D>();
Line2D currentLine = null;
MyMouseAdapter mouse = new MyMouseAdapter();

public void addListeners(Component component) { 
    MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
    component.addMouseListener(myMouseAdapter);
    component.addMouseMotionListener(myMouseAdapter);
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    System.out.println("repainting");

    g2.setColor(LINES_COLOR);
    for (Line2D line : lineList) {
        g2.draw(line);
    }
    if (currentLine != null) {
        g2.setColor(CURRENT_LINE_COLOR);
        g2.draw(currentLine);
    }
}

private class MyMouseAdapter extends MouseAdapter {
    Point p1 = null;

    @Override
    public void mousePressed(MouseEvent e) {
        p1 = e.getPoint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (currentLine != null) {
            currentLine = new Line2D.Double(p1, e.getPoint());
            lineList.add(currentLine);
            currentLine = null;
            p1 = null;
            System.out.println("about to repaint");
            repaint();
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (p1 != null) {
            currentLine = new Line2D.Double(p1, e.getPoint());
            repaint();
        }
    }
}

}

You never add your Segmentation instance to the component hierarchy. 您永远不会将Segmentation实例添加到组件层次结构中。 So your paintComponent will never be called. 因此,您的paintComponent将永远不会被调用。

You should have somewhere something like this: 您应该在某处有以下内容:

Segmentation segmentation = new Segmentation();
// ...
component.add(segmentation); // assuming that component is part of a visible component hierarchy

EDIT: 编辑:

You don't register the mouse listeners on the appropriate component. 您没有在适当的组件上注册鼠标侦听器。 The following code seems to work quite alright: 以下代码似乎正常运行:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class TestGraphics {
    private JLayeredPane contentPane;

    public void newImage() {
        try {
            JFileChooser fileChooser = new JFileChooser(".");
            int status = fileChooser.showOpenDialog(null);

            if (status == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                System.out.println("The selected file is from the: " + selectedFile.getParent() + " Drive");
                System.out.println("Name of file: " + selectedFile.getName());
                System.out.println("Opening file");

                BufferedImage buffImage = ImageIO.read(new File(selectedFile.getAbsolutePath()));
                ImageIcon image = new ImageIcon(buffImage);

                contentPane = new JLayeredPane();
                contentPane.setBackground(Color.WHITE);
                contentPane.setOpaque(true);
                // getTabbedPane().setComponentAt(tabNum, contentPane);
                Dimension d = new Dimension(image.getIconWidth(), image.getIconHeight());
                Segmentation segmentation = new Segmentation();
                segmentation.setIcon(image);
                segmentation.setSize(d);
                contentPane.setPreferredSize(d);
                contentPane.add(segmentation);

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(contentPane);
                frame.setResizable(false);
                frame.pack();
                frame.setLocationByPlatform(true);
                frame.setVisible(true);

            } else if (status == JFileChooser.CANCEL_OPTION) {
                JOptionPane.showMessageDialog(null, "Canceled");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class Segmentation extends JLabel {

        private static final long serialVersionUID = -1481861667880271052L; // unique id
        private static final Color LINES_COLOR = Color.red;
        public static final Color CURRENT_LINE_COLOR = new Color(255, 200, 200);
        ArrayList<Line2D> lineList = new ArrayList<Line2D>();
        Line2D currentLine = null;
        MyMouseAdapter mouse = new MyMouseAdapter();

        public Segmentation() {
            addMouseListener(mouse);
            addMouseMotionListener(mouse);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            System.out.println("repainting");

            g2.setColor(LINES_COLOR);
            for (Line2D line : lineList) {
                g2.draw(line);
            }
            if (currentLine != null) {
                g2.setColor(CURRENT_LINE_COLOR);
                g2.draw(currentLine);
            }
        }

        private class MyMouseAdapter extends MouseAdapter {
            Point p1 = null;

            @Override
            public void mousePressed(MouseEvent e) {
                p1 = e.getPoint();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (currentLine != null) {
                    currentLine = new Line2D.Double(p1, e.getPoint());
                    lineList.add(currentLine);
                    currentLine = null;
                    p1 = null;
                    System.out.println("about to repaint");
                    repaint();
                }
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                if (p1 != null) {
                    currentLine = new Line2D.Double(p1, e.getPoint());
                    repaint();
                }
            }
        }

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestGraphics tg = new TestGraphics();
                tg.newImage();
            }
        });
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM