I am creating a program where I can draw different shapes onto a drawing canvas. I want to add a button that, after clicked, allows me to select a shape and then drag/translate it to a different spot on the canvas. What is the basic concept behind implementing this? When I click on the shape to select it, I want to somehow add a border around it to show that it is selected.
Okay, I tried this and it kind of works. Except, I am able to select multiple shapes and the border that shows its selected does not appear around the shape until after I change radio buttons to do something else. Then the selection appears. I am not sure how to fix this? I tried adding a boolean to fix being able to select multiple shapes, but it isn’t working. boolean isSelected is originally set to false. I don’t understand why the border color/width doesn’t appear automatically on the mouse click.
public void mousePressed(MouseEvent e) {
if ((_buttonNumber == 0) || (isSelected)) {
for(int i=0; i<_storedShapes.size(); i++) {
_storedShapes.elementAt(i).contains(e.getPoint());
_storedShapes.elementAt(i).setBorderWidth(10);
_storedShapes.elementAt(i).setBorderColor(Color.BLACK);
isSelected = true;
}
}
else {
isSelected = false;
}
Break down this overall project into small steps and solve each one individually and in isolation. First figure out how to select a Shape among several Shapes. I’ve usually done this by holding my Shapes in an ArrayList, then in a MouseListener’s
mousePressedmethod, iterating through the list and checking to see if any Shapecontains(...)the mouse’s Point.For Dragging, you’ll use a MouseMotionListener (the two listener’s can be combined in one class that extends MouseAdapter) and then move the selected shape in the
mouseDraggedmethod.