Search code examples
javajframepaintcomponentjdialog

Issue drawing rectangle in negative direction - Java


I'm having issues attempting to draw a rectangle in the negative direction from a point clicked on a screen. I have the following class that simulates a screen capture software like Gyazo:

class DrawSquare extends JPanel implements MouseListener, MouseMotionListener {

// Components
public JDialog frame;
public Rectangle rectangle;
public BufferedImage bufferedImage;
public Point start, end;

// Variables
public String capturedImage;

public DrawSquare(JDialog frame) {

    this.frame = frame;

    // Read in crosshair image to replace mouse icon
    Toolkit tool = Toolkit.getDefaultToolkit();
    Image newImage = getToolkit().getImage("components/cursor.png");
    Cursor cursor = tool.createCustomCursor(newImage, new Point (this.frame.getX(), this.frame.getY()), "img");

    this.frame.setCursor(cursor);
    this.frame.addMouseListener(this);
    this.frame.addMouseMotionListener(this);

}

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

    Graphics2D g2d = (Graphics2D) g.create();
    // draw background overlay
    g2d.drawImage(bufferedImage, WIDTH, 0, this);
    if (rectangle != null) {
        //g2d.setColor(new Color(225, 225, 255, 128));
        frame.setOpacity(0.6f);
        //g2d.fill(rectangle);
        System.out.println(rectangle);
        g2d.setColor(new Color(72,119,205));
        g2d.draw(rectangle);
    }
    g2d.dispose();
}

@Override
public void mouseDragged(MouseEvent e) {

    this.end = e.getPoint();
    int width = end.x - start.x;
    int height = end.y - start.y;

    rectangle.setSize(new Dimension(width, height));
    frame.validate();
    frame.repaint();
}

@Override
public void mouseMoved(MouseEvent e) {
    this.start = e.getPoint();
    frame.validate();
    frame.repaint();
}


@Override
public void mousePressed(MouseEvent e) {
    // Get the X and Y point from the mouse pressed
    rectangle = new Rectangle(start);
    System.out.println(rectangle);
    // Repaint the screen
    frame.validate();
    frame.repaint();
}

@Override
public void mouseReleased(MouseEvent e) {}

@Override
public void mouseClicked(MouseEvent e) {}

@Override
public void mouseEntered(MouseEvent e) {}

@Override
public void mouseExited(MouseEvent e) {}

}

Now the reason for the issue as mentioned already, is that when I attempt to draw the rectangle box in the opposition or negative direction of a point clicked on the screen, it doesn't draw, the rectangle information looks like this during such an attempt:

java.awt.Rectangle[x=635,y=395,width=-316,height=-194]

However, when I drag the rectnagle in the positive direction it works as it is supposed to:

enter image description here

What I'd like to know is how I can fix this using negative values for width/height, or doing it another way entirely.


Solution

  • You should actually have 2 Points - drag start Point and current drag Point.

    The rectangle is calculated:

    x=min(dragStartPoint.x, dragCurrentPoint.x)
    y=min(dragStartPoint.y, dragCurrentPoint.y)
    width=abs(dragStartPoint.x - dragCurrentPoint.x)
    height=abs(dragStartPoint.y - dragCurrentPoint.y)