Clicking and dragging a Path2D line in JPanel

So, I am drawing a line using a Path2D and doing this:

Path2D linePath = new Path2D.Double();
linePath.moveTo(startingX, startingY);
linePath.lineTo(endingX, endingY);
repaint();

What I am trying to do is click-and-drag the line around. For right now, I don’t want to change the line’s structure (length, etc), I just want to move the line around. I know you can use Path2D.contains(e.getPoint) for enclosed shapes, but this doesn’t work for lines. Any ideas? Thanks

Full Code

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Path2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Editor {

    public static void main(String[] args) {
        new Editor();
    }

    public Editor() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager
                            .getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException
                        | IllegalAccessException
                        | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new UMLWindow();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setBounds(30, 30, 1000, 700);
                frame.getContentPane().setBackground(Color.white);
                frame.setVisible(true);
                frame.setLocationRelativeTo(null);
            }
        });
    }

    public static class UMLWindow extends JFrame {

        Shapes shapeList = new Shapes();
        Panel panel;

        private static final long serialVersionUID = 1L;

        public UMLWindow() {
            addMenus();
            panel = new Panel();
        }

        public void addMenus() {

            getContentPane().add(shapeList);

            setSize(300, 200);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

            JMenuItem lineMenuItem = new JMenuItem("New Line");
            lineMenuItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    System.out.println("adding line");
                    shapeList.addLine();
                }
            });

            JMenuBar menubar = new JMenuBar();

            menubar.add(lineMenuItem);

            setJMenuBar(menubar);

            // shapeList.addLine();
        }
    }

    // Shapes class, used to draw the shapes on the panel
    // as well as implements the MouseListener for dragging
    public static class Shapes extends JPanel {

        private static final long serialVersionUID = 1L;

        private List<Path2D> lines = new ArrayList<Path2D>();
        private Boolean drawing = false;
        private Point lineStartingPoint = new Point();
        private Point lineEndingPoint = new Point();
        private Path2D.Double linePath;

        public Shapes() {
            MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
            addMouseListener(myMouseAdapter);
            addMouseMotionListener(myMouseAdapter);
        }

        public void addLine() {
            drawing = true;
            repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2 = (Graphics2D) g;
            g2.setStroke(new BasicStroke(2));
            if (drawing) {
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setStroke(new BasicStroke(2));
                g2.drawLine(lineStartingPoint.x, lineStartingPoint.y,
                        lineEndingPoint.x, lineEndingPoint.y);
            }
            for (Path2D shape : lines) {
                g2.setColor(Color.BLACK);
                g2.draw(shape);
            }
        }

        class MyMouseAdapter extends MouseAdapter {

            @Override
            public void mousePressed(MouseEvent e) {
                if (drawing) {
                    lineStartingPoint = e.getPoint();
                    lineEndingPoint = lineStartingPoint;
                }
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                if (drawing) {
                    lineEndingPoint = e.getPoint();
                    repaint();
                    System.out.println(lines.size());
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (drawing) {
                    drawLine(e);
                    drawing = false;
                }
            }

            public void drawLine(MouseEvent e) {
                drawing = false;
                lineEndingPoint = e.getPoint();
                linePath = new Path2D.Double();
                linePath.moveTo(lineStartingPoint.getX(),
                        lineStartingPoint.getY());
                linePath.lineTo(lineEndingPoint.getX(), lineEndingPoint.getY());
                lines.add(linePath);
                repaint();
            }

        }
    }
}


Download clicking.and.dragging.a.path2d.line.in.jpanel.zip
Direct Link


Download


Download clicking.and.dragging.a.path2d.line.in.jpanel.zip
Mediafire


Download


Download clicking.and.dragging.a.path2d.line.in.jpanel.zip
Microsoft SkyDrive


Download

Both comments and pings are currently closed.

Comments are closed.