简体   繁体   English

Java Swing 用鼠标点击和拖动画线

[英]Java Swing draw lines with mouse click and drag

I want to bring back a question that was asked before: java draw line as the mouse is moved我想带回之前问过的一个问题: java draw line as the mouse is moved

"I would like to add a feature to my application which allows the user to draw a straight line by clicking the mouse at the start location and releasing it at the end location. The line should move as the mouse moves until it is finally released; similar to the way that a line can be drawn using the Microsoft Paint application. “我想在我的应用程序中添加一个功能,允许用户通过在开始位置单击鼠标并在结束位置释放它来绘制一条直线。直线应该随着鼠标移动而移动,直到它最终被释放;类似于使用 Microsoft Paint 应用程序绘制线条的方式。

How can implement this so that the line is repainted as it moves without repainting other things that may already be drawn in that rectangular area?"如何实现这一点,以便在不重新绘制可能已经在该矩形区域中绘制的其他内容的情况下,在移动时重新绘制线条?”

Question is: How can I draw multiple lines with the old lines still there?问题是:如何在旧线仍然存在的情况下绘制多条线?

This is the code that works for me, but the previous line gets erased as soon as you draw a new one:这是对我有用的代码,但是只要你画一个新的代码,上一行就会被删除:

public static void main(String args[]) throws Exception {
    JFrame f = new JFrame("Draw a Red Line");
    f.setSize(300, 300);
    f.setLocation(300, 300);
    f.setResizable(false);
    JPanel p = new JPanel() {
        Point pointStart = null;
        Point pointEnd   = null;
        {
            addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    pointStart = e.getPoint();
                }

                public void mouseReleased(MouseEvent e) {
                    pointStart = null;
                }
            });
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseMoved(MouseEvent e) {
                    pointEnd = e.getPoint();
                }

                public void mouseDragged(MouseEvent e) {
                    pointEnd = e.getPoint();
                    repaint();
                }
            });
        }
        public void paint(Graphics g) {
            super.paint(g);
            if (pointStart != null) {
                g.setColor(Color.RED);
                g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
            }
        }
    };
    f.add(p);
    f.setVisible(true); 
}

This is the code that works for me, but the previous line gets erased as soon as you draw a new one: 这是适合我的代码,但是一旦你绘制一个新代码就会删除前一行:

There are two common approaches: 有两种常见的方法:

  1. Keep an ArrayList of objects to paint. 保持要绘制的对象的ArrayList。 Then the paintComponent() method repaints all the objects each time the component needs to repaint itself 然后每次组件需要重绘自身时,paintComponent()方法重新绘制所有对象

  2. Paint onto a BufferImage and then just paint the BufferedImage. 绘制到BufferImage上,然后绘制BufferedImage。

Check out Custom Painting Approaches for a working example of both of these approaches. 查看自定义绘画方法 ,了解这两种方法的工作示例。

Just set in mouseRelesed只需在 mouseRelesed 中设置

pointStart = e.getPoint instead of pointStart = null.

there is no need for any lists or things like that when you set it to null it starts from the beginning and deletes the last one当您将它设置为 null 时,不需要任何列表或类似的东西,它会从头开始并删除最后一个

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

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