• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use an if-statement, boolean, and keypressed to act like a button.  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I'm trying to have the drawQuilt function be drawn one time, each time I press any key and let go of it. I want it to act like a button of sorts, however each time I press a key, I just have an array of quilts being drawn until I release the key.




Thanks!
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alicia, welcome to the Ranch!

I'm not sure if it's the cause of your problem, but line 15 doesn't do what you think.



You probably think that's comparing the variable "draw" to false, but in fact it assigns false to the "draw" variable (and returns false to the if-statement). Which almost certainly it isn't what you want.

What you want, instead, is



Or better still, make lines 12 through 17 be an if-else construct.
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also... line 16 doesn't do anything, since that call to drawQuilt goes through the loop zero times. So you could leave lines 15 through 17 out entirely.

But then you have no code which ever sets "draw" to false, which doesn't look right to me. However I don't see any code which ever sets "keyPressed" to false, either. So given your original problem description that makes me think I don't see the whole code there.
 
Alicia Li-Ying
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul!

How would I go about setting keyPressed to false then? Would I have to use another if-statement and assign it a boolean variable?

Thanks!
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dunno. You haven't posted any code which sets keyPressed to true, so it's pretty hard to suggest where you should set it to false.

However if you only want the draw() method to be called once when a key is pressed, then maybe you don't need the keyPressed variable at all. Maybe you should just call draw() without bothering with that variable at all. But that's just speculation because I don't see some other code which could be important.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this supposed to be a Swing application? If it is I am not convinced you are drawing correctly, but we need to see more details to know about that.

And welcome to the Ranch (again).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!