• 14
• 12
• 9
• 10
• 13

# Negative width/height

This topic is 2675 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to simulate the effect of when you for example click and drag you mouse, the system creates a transparent rectangle that for example selects object and such. I am partly successful. It works if the user drags the mouse in a positive region relative to the mouse position clicked. But it fails if the mouse goes into a negative region relative to the first mouse clicked position.

Maybe some code :
public void mouseClicked(MouseEvent e) { conditionalDispatch(e); _selectingRegion.mouseClicked(e);}

the selectingRegion.mouseCilcked is effectively the following:
public void mouseClicked(java.awt.event.MouseEvent e){        updateSelectingRegion(e);        rect.setLocation(e.getX(), e.getY());        } private void updateSelectingRegion(MouseEvent e){        rect.setDraw(true);        rect.setTransparency(true, 255/2);        rect.setPointOfClicked(e.getPoint()); //relative point clicked    }

now everytime the user drags his mouse when clicked, all that happens is the following:
 public void mouseDragged(java.awt.event.MouseEvent e){        double dx = e.getX() - rect.getPointOfClicked().getX();        double dy = e.getY() - rect.getPointOfClicked().getY();        updateSelectingRegion(e);        rect.expand(dx, dy);// setSize(getWidth()+dx,getHeight()+dy)    }

so as you see, this is only valid for positive width and height. I need it to work for negative as well. How would you suggest to fix this problem. Thanks.

##### Share on other sites
Keep the rectangle separate from your mouse positions.

Set the corners from min/max values. E.g.,
upper-left-hand corner = point( min(currentClick.x,origPointClicked.x),min(currentClick.y,origPointClicked.y) );

Do similarly for lower-right-hand corner using max values.

##### Share on other sites
But java.rectangle.2d doesn't have any mechanism to set its corner points?

##### Share on other sites
So far this is the algorithm I have, but it doesn't cover all cases:
 public void add(double newx, double newy){ 	double x1 = Math.min(getMinX(), newx);        double y1 = Math.min(getMinY(), newy);	                double x2 = Math.max(getMaxX(), newx);	double y2 = Math.max(getMaxY(), newy);                        if(getMinX() < newx && newx < x2) x2 = newx;        if(getMinY() < newy && newy < y2) y2 = newy;                setRect(x1, y1, x2 - x1, y2 - y1);        }

for example, it flips on the x of y axis, but after flipping it, it doesn't work if I try to re-flip it again. Any help. My brain is dead.

##### Share on other sites
Just in case, this is in Java, and java doesn't allow negative width/height, and

##### Share on other sites
I'm not familiar with java. However, you should have the original X,Y values for the initial mouseclick and the new X,Y values for the current drag position. Just compare them and use the minimum values for the upper-lefthand corner, and max values for lower righthand corner.
int minX, minY, maxX, maxY;if( originalX < newX ) minX = originalX;else minX = newX;if( originalX > newX ) maxX = originalX;else maxX = newx;// similar for minY and maxYSetRectangle( minX, minY, maxX, maxY);

You'll have to translate that to java.

##### Share on other sites
width and height can by definition never be negative.
just because your origin is higher than end point will not make the lengths negative.
See Buckeye's post.

##### Share on other sites
That is what i originally did, specifically this :
public void add(double nx, double ny){     double minX = Math.min(getMinX(),nx);     double minY = Math.min(getMinY(),ny);     double maxX = Math.max(getMaxX(),nx);     double maxY = Math.max(getMaxY(),ny);     setRect(minX,minY,maxX-minX,maxY-minY);    }

It expands on either side, for example if I drag the rectangle right side forward, its width increases, and if I drag the left side of the rectangle, its width
increases appropriately, similar with height. The only problem is that it doesn't
decrease.

##### Share on other sites
Just to show you guys what I mean, try running this jar file .

1) Create a rectangle object
2) Hold the Control Key
3) Draw the edges of the rectangle

notice how they don't decrease. This is really bothering. I'm really going crazy.