Search code examples
javalineshapesmouseclick-eventmousemotionlistener

Dragging lines in Java


I created two lines in the middle of the screen which is a positive sign, I would like to drag the positive sign only when I click on it, but it only works when I click anywhere on the screen, I want the sign to be dragged only when I click on it and not when I click on anywhere else on the screen. I would like anyone to help me with what I can do to make this work. This is the code below

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;

public class Main extends JPanel {
    public static void main(String args[]) throws Exception {
        JFrame f = new JFrame("Shapes");
        f.setSize(600, 600);
        f.setLocation(300, 300);

        f.setResizable(true);
        JPanel p = new JPanel() {
            Point pointStart = null;
            Point pointEnd = null;
            int x2 = 250;
            int y2 = 175;


            private Shape lineMp = null;
            private Shape lineMn = null;
            {

                lineMn = new Line2D.Double(235,175,265,175);
                lineMp = new Line2D.Double(250,160,250,190);

                Point newPoint = new Point();

                MouseAdapter mouseAdapter = new MouseAdapter() {
                    private Point prevPoint;
                    @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 shapeMn = (Line2D) lineMn;
                        Line2D shapeMp = (Line2D) lineMp;


                        int nx2 = (int) (shapeMn.getX2() - dx);
                        int ny2 = (int) (shapeMn.getY2() - dy);

                        int px2 = (int) (shapeMp.getX2() - dx);
                        int py2 = (int) (shapeMp.getY2() - dy);

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

                        int px1 = (int) (shapeMp.getX1() - dx);
                        int py1 = (int) (shapeMp.getY1() - dy);

                        Point startPointMn = new Point(x1, y1);
                        Point endPointMn = new Point(nx2, ny2);
                        Point startPointMp = new Point(px1, py1);
                        Point endPointMp = new Point(px2, py2);

                        Point endPoint = new Point(x2, y2);
                        if (shapeMn != null) {
                            shapeMn.setLine(startPointMn, endPointMn);
                            prevPoint = e.getPoint();
                            repaint();
                        }
                        if (shapeMp != null) {
                            shapeMp.setLine(startPointMp, endPointMp);
                            prevPoint = e.getPoint();
                            repaint();
                        }
                    }
                    @Override
                    public void mouseReleased(MouseEvent e) {
                        repaint();
                    }
                };
                addMouseListener(mouseAdapter);
                addMouseMotionListener(mouseAdapter);
            }

            public void paint(Graphics g) {
                super.paint(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setStroke(new java.awt.BasicStroke(2));
                g2d.drawRect(100,100,300,150);
            }

            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setPaint(Color.BLUE);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                if (lineMn != null){
                    g2d.draw(lineMn);
                    g2d.draw(lineMp);
                    }
            }};
        f.add(p);
        p.setLayout(null);
        f.setVisible(true);

    }
}

Solution

  • There's a couple of ways you "might" do this, but for me, I'd try and combine the two lines into a single shape, this would then allow you to calculate their bonding box and then do a simple hit test

    For example...

    @Override
    public void mouseDragged(MouseEvent e) {
    
        GeneralPath gp = new GeneralPath();
        gp.append(lineMn, false);
        gp.append(lineMp, false);
    
        Point p = e.getPoint();
    
        if (!gp.getBounds().contains(p)) {
            return;
        }
    

    Now, note, "this" implementation is not very efficient. Instead, I'd combine the lines into a single shape early on and continue to move it instead.