0

私は、マウスのドラッグイベントによってトリガーされたときに画像に線を描く必要があるプログラムに取り組んでいますが、コード内でrepaint()を介してpaintComponentを呼び出すと、paintComponentメソッドが実行されません。私はスインググラフィックスのチュートリアルを読み、これについて他のプログラマーからの意見もありましたが、これまでのところ、自分に合った解決策を見つけることができませんでした。

私自身と私のコードを見ている人のためにこれを単純化するために、プログラム内から期待どおりに機能していないコードの領域を特定するのに役立つ小さなSSCCEを投稿しました。

私のためにこれを見るのに時間を割く人に前もって感謝します。

以下は私が使用している2つの別々のクラスです。

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();
        }
    });
}

}

と他。

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();
        }
    }
}

}
4

1 に答える 1

4

Segmentationインスタンスをコンポーネント階層に追加することはありません。だからあなたpaintComponentは決して呼ばれることはありません。

あなたはこのようなものをどこかに持っているはずです:

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

編集:

適切なコンポーネントにマウスリスナーを登録していません。次のコードは問題なく機能しているようです。

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();
            }
        });
    }

}
于 2013-02-10T16:22:55.673 に答える