I am unable to understand the following:
  • In g.drawRect, p1.x is subtracted from p.x
  • In case RECTANGLE, why g.drawRect is being called twice.


Please help.

Java Code:
public void mouseDragged(MouseEvent ev) {
    Graphics2D g = (Graphics2D)getGraphics();
    g.setXORMode(Color.white);
    Point p1 = (Point)points.get(pointIndex-1);
    switch (shapeType) {
      case RECTANGLE:
        if (p != null) g.drawRect(p1.x, p1.y, p.x-p1.x, p.y-p1.y);
        p = ev.getPoint();
        g.drawRect(p1.x, p1.y, p.x-p1.x, p.y-p1.y);
        break;