• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why does one if statement work but not another?

 
Ranch Hand
Posts: 51
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic