Search code examples
javauser-interfacedrag-and-droppaintshapes

Drag and drop filled shapes vs unfilled shapes [Homework]


I'm making a ms paint style program for a homework assignment. In this homework assignment you're supposed to be able to right click on a shape and move it around the screen. Filled shapes can be right clicked anywhere inside the shape, where as unfilled shapes should be right clicked and selected on the border of the shape.

The problem I'm having is, when I originally start the program,draw and unfilled shape, and try to drag it, it WILL drag, but once I drop it again it behaves correctly and becomes undraggable, and all future unfilled shapes act accordingly.

TLDR: I want all filled shapes to be draggable, and all unfilled shapes to be undraggable. Unfilled shapes (for some reason) ARE draggable if it is the first shape placed. After an unfilled shape is dragged ONCE, it behaves correctly.

Here is the related code:

private boolean isFilled = false;
private JCheckBox fill = new JCheckBox("Filled");
private int indexToMove;

paint component

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.clearRect(0, 0, getWidth(), getHeight());

for (int i = 0; i < shapes.size(); i++) {
    shapes.get(i).draw(g);
}

repaint();

}

ActionPerformed on my fill button

else if (e.getSource() == fill) {
    if (fill.isSelected()) {
    isFilled = true;
    } else {
    isFilled = false;
    }
}

THIS IS THE PART WHERE I START DRAGGING AND DROPPING

MOUSE PRESSED

public void mousePressed(MouseEvent e) {

if (SwingUtilities.isRightMouseButton(e)) {
    for (int i = 0; i < shapes.size(); i++) {
    if (shapes.get(i).isSelected(e.getX(), e.getY())) {
        System.out.println("Shape contained in: " + i);
        indexToMove = i;
    }
    }
}
}

MOUSE DRAGGED

public void mouseDragged(MouseEvent e) {

if (SwingUtilities.isRightMouseButton(e)) {
    if (indexToMove >= 0) {
    shapes.get(indexToMove).move(e.getX(), e.getY());
    }
}
}

MOUSE RELEASED

public void mouseReleased(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
    indexToMove = -1;
}
}

HERE IS THE RELATED SHAPE CLASS CODE

public class MyRectangle extends Rectangle implements MyShape {
private Color color;
private boolean filled = false;
private int x, y, dx, dy, height, width;

public MyRectangle(Color color, boolean filled, int x, int y, int endX, int endY) {
this.color = color;
this.filled = filled;
this.x = x;
this.y = y;
this.dx = endX;
this.dy = endY;

}

@Override
public void draw(Graphics g) {

...

if (!filled) {

    g.drawRect(x, y, width, height);
}

if (filled) {
    g.fillRect(x, y, width, height);
}
}

THIS IS WHERE I CHECK IF IT IS FILLED/UNFILLED THE SYSTEM OUT ONLY HAPPENS AFTER I DRAG AND DROP THE UNFILLED SHAPE ONE TIME

@Override
public boolean isSelected(int clickX, int clickY) {
boolean selected = false;
if(filled == false){
    System.out.println("unfilled shape, do not move");
    selected = false;
}

else if (filled == true) {
    if ((clickX > x) && (clickX < dx) && (clickY > y) && (clickY < dy)) {

    selected = true;
    } else {
    selected = false;
    }
}


return selected;
}


public void move(int offsetX, int offsetY) {

int centerX = (dx + x) / 2;
int centerY = (dy + y) / 2;
System.out.println("CenterX: " + centerX + " CenterY: " + centerY);

if (offsetX > centerX) {

    offsetX = offsetX - centerX;
    x += offsetX;
    dx += offsetX;
} else if (offsetX < centerX) {

    offsetX = centerX - offsetX;
    x -= offsetX;
    dx -= offsetX;
}

if (offsetY > centerY) {

    offsetY = offsetY - centerY;

    y += offsetY;
    dy += offsetY;
}

else if (offsetY < centerY) {

    offsetY = centerY - offsetY;
    y -= offsetY;
    dy -= offsetY;
}

}

know this is quite a bit of code, I tried to chop out anything that didn't seem relevant. Let me know if you need to see more!


Solution

  • Answered my own question.

    When you right click, the indexToMove wasn't assigned to anything, there for it isn't until after the shape is moved that it will register that it shouldn't be moved.

    I gave indexToMove the value of -1, so when you start the shape won't be draggable.