Results 1 to 20 of 24
- 11-04-2011, 01:34 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
- 11-04-2011, 01:46 AM #2
Re: If A Value of a Variable is increasing...
To detect if the value of a variable has been increased, you need to have saved the old value of the variable to compare against.if a value of a variable is increasing.
- 11-04-2011, 01:47 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
Re: If A Value of a Variable is increasing...
And how do I do that? ;)
- 11-04-2011, 01:51 AM #4
Re: If A Value of a Variable is increasing...
Create a new variable and assign it the value of the variable you want to test for changes.
- 11-04-2011, 01:54 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
Re: If A Value of a Variable is increasing...
But how can I put this into an if statement?
Let me tell you what I want to do. I've got an input handler class which implements a MouseMotionListener.
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
Now I want to basically say:
if (mouseX++) {
variable++;
}
- 11-04-2011, 02:02 AM #6
Re: If A Value of a Variable is increasing...
Save the old value of mouseX in a new variable and then test it against the new value of mouseX.
You need to think through the logic for when the value of mouseX is set and when to save it so you can detect if it has changed and when you want to test it.
- 11-04-2011, 02:05 AM #7
Re: If A Value of a Variable is increasing...
As Norm said you need to have the old value stored.
Java Code:if(newValue > oldValue) { it has increased }
- 11-04-2011, 03:57 AM #8
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
Re: If A Value of a Variable is increasing...
Yeah, but the old value of x is EQUAL to the new value of x. They increase at the same time.
How can I save an OLD version of the X variable? It just changes into the new version straight away, obviously.
- 11-04-2011, 04:11 AM #9
Re: If A Value of a Variable is increasing...
Java Code:import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner reader = new Scanner(System.in); int oldValue = 0; int newValue = 0; boolean finished = false; System.out.println("Enter a series of numbers. A negative number to stop!"); while(! finished) { System.out.print(">> "); newValue = reader.nextInt(); if(newValue < 0) { finished = true; } else { if(newValue > oldValue) { System.out.println("The number has increased"); } else { System.out.println("The number has decreased"); } oldValue = newValue; } } } }
- 11-04-2011, 04:52 AM #10
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
Re: If A Value of a Variable is increasing...
Thanks for the example, but in my case, oldValue is already EQUAL to newValue. If I move the mouse horizontally to the right then it increases the x variable's value, and if I move it to the left it decreases that value.
So how can I check if the value has been increased, decreased, or doesn't change?
- 11-04-2011, 04:56 AM #11
Re: If A Value of a Variable is increasing...
:headdesk:
When you move the mouse ONLY the new value will change. You make the comparison against the old value and after that you reset old value to new value. Exactly how I did in my example.
- 11-04-2011, 05:03 AM #12
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
Re: If A Value of a Variable is increasing...
From what I understand, what we're doing here is sort of setting like an origin point somewhere on the screen, and then using that to measure whether the mouse value increases or decreases.
But what I want here is for that point of origin to sort of follow the mouse around, so that if I move the mouse 500 pixels to the right, and then 1 pixel to the left, it will measure that 1 pixel to the left as a decrease, and those 500 pixels to the right as an increase. I'm sorry if this is hard to understand, it is honestly hard to explain!
- 11-04-2011, 05:25 AM #13
Re: If A Value of a Variable is increasing...
No it is not hard to understand FOR US!
each time the mouse moves
get current x-coord
compare with last x-coord (stored in a different variable)
log change
update last x-coord to equal current x-coord
- 11-04-2011, 05:34 AM #14
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
Re: If A Value of a Variable is increasing...
Yeah that's pretty much what I want, but how do I store the LAST x-coord? Could you give some example code?
Current x-coord = e.getMouseX();
but how can I log what that coord was say, 1 ms ago?
- 11-04-2011, 05:39 AM #15
Re: If A Value of a Variable is increasing...
If you are doing this inside the mouseMoved method then there is no concept of time. The only thing that matters is that the co-ords have changed. Surely I have provided enough hints as well as code for you to figure this out by now.
- 11-05-2011, 11:43 AM #16
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
Re: If A Value of a Variable is increasing...
Thanks a lot! It worked! Took me a while to work it out and adjust it to the MouseMotionListener. Turns out it didn't work while in the the mouseMoved method, and had to be moved out of there; the mouse coordinates wouldn't update while in there.
One other thing I was wondering about, how do I make my mouse loop the window? So when I get to the right edge of the frame, the mouse appears at the left edge, and vice versa?
- 11-05-2011, 11:54 AM #17
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
Re: If A Value of a Variable is increasing...
And also while we're on that note, is there any where to measure the speed at which the mouse moves? If so, how?
- 11-05-2011, 12:10 PM #18
Re: If A Value of a Variable is increasing...
Save the current time (See System.currentTimeMillis()) with the current mouse location. Then compute the distance moved divided by the time to move it.any where to measure the speed at which the mouse moves
- 11-05-2011, 12:33 PM #19
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
Re: If A Value of a Variable is increasing...
But how can I save time tied to the mouse location integer?
And also how do I loop the mouse when it exits the frame? So when I get to the right edge of the frame, the mouse appears at the left edge, and vice versa?
Sorry I'm asking so many questions, but you guys are awesome for helping me out!
- 11-05-2011, 01:23 PM #20
Re: If A Value of a Variable is increasing...
When you save the mouse location, save the current time also.
Then when you get a new mouse location, get the current time.
Now you have the change in location and the change in time that you can use to compute the speed.
You need to detect when you are at the edge of the frame and change the location to be at the other edge.how do I loop the mouse when it exits the frame? So when I get to the right edge of the frame, the mouse appears at the left edge,
Are you planning to use the Robot class to change the location of the cursor?
Similar Threads
-
Colour increasing and retaining animation
By monica in forum Java 2DReplies: 7Last Post: 05-15-2011, 04:56 PM -
Increasing/decreasing an int variable with boundrys
By david522 in forum New To JavaReplies: 5Last Post: 05-12-2011, 12:04 AM -
Increasing Java heap space
By davetheant in forum New To JavaReplies: 2Last Post: 02-03-2011, 02:24 AM -
increasing jvm heap space
By kizyle502 in forum Advanced JavaReplies: 1Last Post: 05-15-2010, 10:51 AM -
all maximal increasing subsequences
By hassanJava in forum Advanced JavaReplies: 3Last Post: 04-23-2008, 04:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks