java draw line as the mouse is moved

2019-01-18 07:58发布

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.

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?

3条回答
太酷不给撩
2楼-- · 2019-01-18 08:26

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); 
}
查看更多
Animai°情兽
3楼-- · 2019-01-18 08:30

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:

public void mouseEntered(MouseEvent mouse){ }   
public void mouseExited(MouseEvent mouse){ }
public void mousePressed(MouseEvent mouse){ }
public void mouseReleased(MouseEvent mouse){ }
查看更多
唯我独甜
4楼-- · 2019-01-18 08:39
    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..

查看更多
登录 后发表回答