java在鼠标移动时画线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9711938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:32:23  来源:igfitidea点击:

java draw line as the mouse is moved

javamouseeventawtdrawgraphics2d

提问by trs

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?

如何实现这一点,以便在移动时重新绘制线条,而无需重新绘制可能已在该矩形区域中绘制的其他内容?

回答by all_by_grace

The MouseListener interface is your friend for this. You can just implement mousePressed and mouseReleased functions. The MouseListener interface has the following methods that you can play around with:

MouseListener 接口是您的朋友。您可以只实现 mousePressed 和 mouseReleased 函数。MouseListener 接口有以下方法供您使用:

public void mouseEntered(MouseEvent mouse){ }   
public void mouseExited(MouseEvent mouse){ }
public void mousePressed(MouseEvent mouse){ }
public void mouseReleased(MouseEvent mouse){ }

回答by Java42

Try this...Draw a red line on the screen as the mouse is moved (dragged).

试试这个...随着鼠标移动(拖动)在屏幕上画一条红线。

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

回答by Raj Trivedi

    JFrame frame = new JFrame("Lines");

    frame.add(new JComponent() {
        private Shape line = null;
        {
            line = new Line2D.Double(100, 100, 200, 200);
            prevPoint = new Point();
            newPoint = new Point();

            MouseAdapter mouseAdapter = new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    prevPoint = e.getPoint();
                    System.out.println("Prev Point=" + prevPoint.toString());
                    repaint();
                }

                @Override
                public void mouseDragged(MouseEvent e) {
                    int dx = 0;
                    int dy = 0;

                    dx = (int) (prevPoint.x - e.getPoint().getX());
                    dy = (int) (prevPoint.y - e.getPoint().getY());

                    Line2D shape = (Line2D) line;

                    int x1 = (int) (shape.getX1() - dx);
                    int y1 = (int) (shape.getY1() - dy);

                    int x2 = (int) (shape.getX2() - dx);
                    int y2 = (int) (shape.getY2() - dy);

                    Point startPoint = new Point(x1, y1);
                    Point endPoint = new Point(x2, y2);

                    if (shape != null) {
                        shape.setLine(startPoint, endPoint);
                        prevPoint = e.getPoint();
                        repaint();
                    }
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    repaint();
                }

            };
            addMouseListener(mouseAdapter);
            addMouseMotionListener(mouseAdapter);
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setPaint(Color.BLUE);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            if (line != null) {
                g2d.draw(line);
            }
        }
    });
    frame.setSize(650, 400);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

This will move the line as the mouse is moved.. Hope this helps..

这将随着鼠标移动而移动线条..希望这会有所帮助..