Resize Path2D shape when clicked and dragged by user

So I have a program that adds a square to a JPanel and allows the user to drag a shape around the panel. What I want to do is click on the bottom right corner of the form and resize it when the user drags it. I kind of got stuck on how to do this. I know that as the user drags it, the length and width of the rectangle will need to be recalculated so that the bottom-right corner matches the mouse. But how can I detect a click on the bottom-right edge of the rectangle? Thanks for any help.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class UMLEditor {

    public static void main(String[] args) {

        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);
        frame.setVisible(true);
    }
}

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

        setTitle("UML Editior");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        shapeList.addSquare(100, 100);
    }

    public void loadFile() {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("."));

        int r = chooser.showOpenDialog(this);
        if (r == JFileChooser.APPROVE_OPTION) {
        }
    }
}

// Shapes class, used to draw the shapes on the panel
// as well as implements the MouseListener for dragging
class Shapes extends JPanel {
    private static final long serialVersionUID = 1L;

    private List<Path2D> shapes = new ArrayList<Path2D>();
    int currentIndex;

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

    public void addSquare(int width, int height) {
        Path2D rect2 = new Path2D.Double();
        rect2.append(new Rectangle(getWidth() / 2 - width / 2, getHeight() / 2
                - height / 2, width, height), true);

        shapes.add(rect2);
        repaint();
    }

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

        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke(2));
        for (Path2D shape : shapes) {
            g2.draw(shape);
        }
    }

    class MyMouseAdapter extends MouseAdapter {
        private boolean pressed = false;
        private Point point;

        @Override
        public void mousePressed(MouseEvent e) {
            if (e.getButton() != MouseEvent.BUTTON1) {
                return;
            }
            for (int i = 0; i < shapes.size(); i++) {
                if (shapes.get(i) != null
                        && shapes.get(i).contains(e.getPoint())) {
                    currentIndex = i;
                    pressed = true;
                    this.point = e.getPoint();
                }
            }
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            if (pressed) {
                int deltaX = e.getX() - point.x;
                int deltaY = e.getY() - point.y;
                shapes.get(currentIndex).transform(
                        AffineTransform.getTranslateInstance(deltaX, deltaY));
                point = e.getPoint();
                repaint();
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            pressed = false;
        }
    }
}

      

+3


source to share


1 answer


I wrote a couple of things back that day that might be of use to you

To get started, the AreaManager (http://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/ui/shape/) This is kind of what you want as it deals with Shapes (Area, actually There is a dragger class that uses mouse drag and a resizer class that uses a mouse wheel. But this is not exactly the user interface you described.



This UI for cursor resizing and resizing based on cursor type and mouse drag is in Draggable at http://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/ui/drag/ . Draggable works with components contained in containers when layout is turned off. But it's not that hard to adapt to your goals.

+1


source







All Articles