• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does one if statement work but not another?  RSS feed

 
John Corkrend
Ranch Hand
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have this simple program where a shape moves around the screen if you press the keys WASD. Now I wanted to set up walls on the edge of the screen so that the shape couldn't move out of view. I got that too work but I tried it 2 different times in 2 different parts of the program. One if statement worked and the other didn't even though it was the same code, and I am wondering why.

The if statement that didn't work is in comments near the bottom, the one that did work is just above that. The only difference between the two was where I placed them in the program which I realize does make a big difference.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, note that ItDoesntWorkIsUseless(←click). You need to TellTheDetails(←click).

Aside from that, two things jump out at me:

1. The one that works has different code than the one that doesn't. In the working one, you always repaint, and then conditionally update y. In the one that doesn't work, you only repaint conditionally, and you do so after updating y.

2. I don't see you ever calling that walls() method, so from the code you posted, there's no way the second if will ever even be executed, so "working" or "not working" is not even an issue.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Jeff. I tried it out, and it seems to work if you replace lines 61 - 63 with a call to walls(), even though this is calling repaint() twice in the case where y == 28. If you replaced lines 59 - 63 with a call to walls(), then the repaint only gets called when y == 28, plus you only call repaint() from a paint(), so really the whole painting process just ceases to function.

How about calling walls() from your KeyAdapter, after all the ifs have run? Then after that call repaint(). You shouldn't have to call repaint() again from anywhere else.
 
John Corkrend
Ranch Hand
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks for your help, I'm still new to java and simple things like these still slip my mind.
I just have one question about this. Does it matter where I call the walls() method as long as it's in the program. And I'm not quite clear on the whole repaint thing and when it needs to be called, or where it needs to be called.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's been a long time since I've done GUI programing, but I believe the sequence is: change the model (x and y in your case), then call repaint. The screen paints happen on a schedule, and repaint just set a flag that says refreshes are waiting. That's why you don't call your paint directly. That's also why calling repaint() twice in a row doesn't affect behavior. Calling repaint from with the paint method is a bad practice, and if that's the only way to get your program working, it probably means you're doing something wrong somewhere else.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!