Sign in to follow this  
Concentrate

Negative width/height

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 maxY
SetRectangle( minX, minY, maxX, maxY);

You'll have to translate that to java.

Share this post


Link to post
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 this post


Link to 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 this post


Link to post
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.
It seems simple but i'm having brain fart so bad. Please please help.

Share this post


Link to post
Share on other sites
One last time: you need to use the original X,Y coordinates for every calc until the mouse button is released.

If your getMinX(), getMinY(), etc., functions don't return the original X,Y coordinates where the user first clicked the button, then your code is wrong.

Share this post


Link to post
Share on other sites
Lets break it down...

A Rect has left, top, right, bottom variables.

if you move the mouse left, modify rect.left.
if you move the mouse up, modify rect.top.
if you move the mouse right, modify rect.right.
if you move the mouse down, modify rect.bottom.

if you need the lengths;
width = rect.right - rect.left
height = rect.bottom - rect.top


Best way to get unstuck from a problem is to leave the computer and get
a piece of paper and just think about it.
What is causing the problem?
Why is it a problem?
How can I solve it?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this