java draw line as the mouse is moved

后端 未结 3 851
面向向阳花
面向向阳花 2020-12-10 00:00

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 locatio

相关标签:
3条回答
  • 2020-12-10 00:36

    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); 
    }
    
    0 讨论(0)
  • 2020-12-10 00:45
        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..

    0 讨论(0)
  • 2020-12-10 00:52

    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){ }
    
    0 讨论(0)
提交回复
热议问题