• Post Reply Bookmark Topic Watch Topic
  • New Topic

Some help needed to find an event stopping condition

 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ! I know i'm supposed to keep it short and concise and avoid full code pasting, but in this case I didn't see how I could easily avoid it.
The thing is, I would like my event to stop at a certain point. What happens with the program is that it generates a 20x20 square in the middle of the window and it randomly moves one unit in any direction (diagonals included). I'd like it to stop when it 'touches', or reaches any edge of my window.
I tried simply implementing the following :

but then, the box doesn't even start moving. The full code is underneath, I included the class that draws my component into the whole. I would like to know where I'd include my stopping code, and considering mine seems wrong, could you help me define a right one? I didn't how else to get the box's X and Y coordinates than by using getX() and getY().
Thanks a lot for the help ! x) Besides the stopping condition the program works fine.

 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone? its just a question of knowing where to insert my stopping condition, and possibly correct the one I tried.
Thanks =)
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that your problem lies in the fact that you check the position of the component, not the box. The component gets created at 0,0 and never moves. You should check the position at which the box is drawn instead (which is stored in the RectangleComponent.box field). I'm just inferring from your code, I didn't run it myself, so I might be wrong, but I feel pretty sure about it.

You didn't specify where you tried to put the condition. I'd say putting it right after your switch statement should work. Of course, you should test the Y coordinate too if you want to limit the position into a square.
 
Victor M. Pereira
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, after reading the api and the actual implementation. I believe your code has two problems.

1) when you apply your translate function, there is a chance that your square may fall into a negetive pixel. And,

2) your code moves it by 1 pixel. The change is too small for a human eye to detect it. Try something bigger like 50.
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot to Martin for pointing out I was testing the component instead of the actual box and thanks for showing me how to reference it properly (its part of those subtleties I don't know too well yet).
But, alas, it's still not "stopping". It doesn't seem to have any trouble 'wandering' off outside my window size. should I maybe be using something else than JFrame? something which doesn't have any infinite canvas?
and for my stopping condition, I used the following :



which I inserted at line 55, right after "t.start()". I can't refer to "t" before line 54 where its being defined.


to Martin, before making such a statement and typed in such a way (in a way that sounds 'true), I'd suggest you first assume to be wrong and double check yourself before making a mistake. this is called critical thinking. because, one pixel is more than visible with the human eye. The eye is MUCH more discerning that most people are aware. If you draw for example a straight line, and offset it by one pixel somewhere, the eye will notice. whether you notice it consciously is a separate matter, and is maybe what you were referring to. In this case changing my setting to 50 pixels would make my square jump in and out of my window, making it even harder to stop on time, since then its coordinates would jump from say, (10, 310) to (10, 360), where would the program make it stop? before the jump out the window or after?
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jake Mauveand wrote:... for my stopping condition, I used the following :



which I inserted at line 55, right after "t.start()". I can't refer to "t" before line 54 where its being defined.

At that place the condition is evaluated only once, before any real movement occurs. You need to put it into the actionPerformed method, so that it gets evaluated every time the timer ticks and the box is moved. Various arrangements are possible, the easiest for your code would probably be to declare the variable t at the class level; that way it will be accessible in the inner class too. At the same time you could name it timer instead of t, for example.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jake Mauve wrote:to Martin, before making such a statement and typed in such a way (in a way that sounds 'true), I'd suggest you first assume to be wrong and double check yourself before making a mistake. this is called critical thinking. because, one pixel is more than visible with the human eye.

And addressing responses to the person who made the original statement is called 'paying attention to detail'.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also, you really do want a much larger
number than 1, so that any newly discovered
condition can be easily tested.

if left at 1, it could take 10 minutes to get
near a border, to see if it worked OK.
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please stop focusing on the 1 .. it works fine and took me now just under 30 secs to (finally) see the box stop. I wouldn't give a point to the previous remark because, I'm doing this for research purposes on randomness. I need it to be at the smallest unit possible. There's one much more important variable to play around with without risking affecting the precision I'm trying to work with : the timer. All I need is to make the event happen many more times in a given time interval.

and thanks to Martin for giving me the final piece of the puzzle (defining 'private static Timer t;' at class level^^ felt a bit dumb not having thought of it, but I still lack the necessary know how to juggle around with anything) and where exactly (for the second time ;)) it be working !
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> please stop focusing on the 1 ..

tunnel vision.

test the problem correction at 50, if it works OK change it back to 1.

it's not rocket science.
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do have one more problem, and I make a point of not making a new thread (), although I'm defining my JFrame as 350x350, there's seem to be a discrepancy in the window that pops up (as in point (350,350) is outside the bounds of my window) I remember there's a reason for this, but that the fix isn't a super easy one. Should I use something else than JFrame for display?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
give RectangleComponent a preferredSize of 350,350
then use frame.pack();
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes awesome, now that works too ! ^^ I know I've had the same problem before.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!