3

当我将鼠标拖到自定义面板上时,我很难让自定义面板重新绘制自己。

基本上,我在带有以下处理程序的自定义面板上绘制自由格式线:

MouseInputAdapter mia = new MouseInputAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if(_app_split_right_buttons_radioInkBtn.isSelected()) {
                    _app_split_right_journal.StartLine();
                    _app_split_right_journal.AddLineSegment(e.getX(), e.getY());
                }
                _app_split_right_journal.repaint();
            }
            @Override
            public void mouseReleased(MouseEvent e) {
                if(_app_split_right_buttons_radioInkBtn.isSelected()) {
                    _app_split_right_journal.AddLineSegment(e.getX(), e.getY());
                    _app_split_right_journal.EndLine();
                }
                _app_split_right_journal.repaint();
            }
            @Override
            public void mouseDragged(MouseEvent e) {
                if(_app_split_right_buttons_radioInkBtn.isSelected()) {
                    _app_split_right_journal.AddLineSegment(e.getX(), e.getY());
                }
                _app_split_right_journal.repaint();
            }
        };
        _app_split_right_journal.addMouseListener(mia);
        _app_split_right_journal.addMouseMotionListener(mia);

但是,我似乎无法在拖动鼠标时重新绘制它,只有在我释放它之后(线条正确绘制)。奇怪的是,如果在我拖动的中间,我右键单击,当我拖动时,线条会突然开始绘制。

任何帮助,将不胜感激。

编辑:为清楚起见, StartLine()/AddLineSegment()/EndLine() 东西只是将点添加到 Point 对象的 ArrayList 中,并且 repaint() 函数已被覆盖以循环遍历这些点并在每个点之间绘制线段。下面的代码:

public void DrawShapes(Graphics g) {
        g.setColor(Color.black);
        for(Geometry.Shape shape : _shapeList) {
            if(shape instanceof Geometry.Line) {
                ArrayList<Point> points = ((Line) shape).GetPointList();
                Point p1 = points.get(0);
                for(int i=1; i<points.size(); i++) {
                    Point p = points.get(i);
                    g.drawLine(p1.x, p1.y, p.x, p.y);
                    p1 = p;
                }
            }
}

编辑:想通了。这是因为我正在循环浏览线条列表,但我忘记了在释放鼠标并调用 EndLine() 之前,我不会将当前正在绘制的线条添加到该列表中。我不得不在 repaint() 方法中添加单独的代码来绘制我当前正在绘制的形状。添加到 repaint() 的代码:

if(_currentShape instanceof Geometry.Line) {
            ArrayList<Point> points = ((Line)_currentShape).GetPointList();
            Point p1 = points.get(0);
            for(int i=1; i<points.size(); i++) {
                Point p = points.get(i);
                g.drawLine(p1.x, p1.y, p.x, p.y);
                p1 = p;
            }
        }
4

1 回答 1

5

这是我为另一个问题所做的示例,但它使用鼠标拖动在组件上画线...

public class PaintingExample {

    private BufferedImage bImage;
    private ImageIcon image;
    private JLabel imageLabel;
    private int xClicked = 0;
    private int yClicked = 0;
    private int xDragged = 0;
    private int yDragged = 0;
    private MouseAdapter mouseListener =
        new MouseAdapter() {
            private boolean paint = false;
            @Override
            public void mousePressed(MouseEvent me) {

                xClicked = me.getX();
                yClicked = me.getY();
                xDragged = xClicked;
                yDragged = yClicked;

                paint = true;

            }

            @Override
            public void mouseReleased(MouseEvent e) {

                xClicked = -1;
                xClicked = -1;
                xDragged = -1;
                yDragged = -1;

                paint = false;

            }

            @Override
            public void mouseMoved(MouseEvent me) {
            }

            @Override
            public void mouseDragged(MouseEvent me) {

                if (paint) {

                    xClicked = xDragged;
                    yClicked = yDragged;

                    xDragged = me.getX();
                    yDragged = me.getY();

                    xDragged = me.getX();
                    yDragged = me.getY();

                    Graphics2D g2 = bImage.createGraphics();
                    g2.setColor(Color.WHITE);
                    g2.drawLine(xClicked, yClicked, xDragged, yDragged);
                    g2.dispose();
                    imageLabel.setIcon(new ImageIcon(bImage));

                    me.getComponent().repaint();

                }

            }
        };

    public PaintingExample() {
        try {
            bImage = ImageIO.read(new URL(
                            "http://gagandeepbali.uk.to/"
                            + "gaganisonline/images/planetbackground.jpg"));
            image = new ImageIcon(bImage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void displayGUI() {
        JFrame frame = new JFrame("Painting on Image");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        imageLabel = new JLabel(image);
        imageLabel.addMouseListener(mouseListener);
        imageLabel.addMouseMotionListener(mouseListener);

        contentPane.add(imageLabel);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new PaintingExample().displayGUI();
            }
        });
    }
}
于 2012-10-04T23:03:35.843 回答