• Post Reply Bookmark Topic Watch Topic
  • New Topic

Moving an rectangel  RSS feed

 
Jos van Tuil
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my last post I wanted blue balls to move, i fixed that. Now i want a rectangle to move when i press keys. I cant seem to get it to work, Did i miss something?









 
Knute Snortum
Sheriff
Posts: 3974
98
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult for Beginning Java, moving to Swing.
 
Rob Camick
Ranch Hand
Posts: 2788
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Key events are dispatched to the component that has focus. By default a JPanel does not receive focus.

Don't use a KeyListener. Instead you should be using Key Bindings.

See Motion Using the Keyboard for more information and examples that compare the two approaches.
 
Jos van Tuil
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Key events are dispatched to the component that has focus. By default a JPanel does not receive focus.

Don't use a KeyListener. Instead you should be using Key Bindings.

See Motion Using the Keyboard for more information and examples that compare the two approaches.


Thank you, I will look into it.
 
Jos van Tuil
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I now have this, it works but is it a nice way to do it ?



 
Rob Camick
Ranch Hand
Posts: 2788
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but is it a nice way to do it ?


So much for listening to the suggestion.

The suggestion was to use Key Bindings.

Also, painting methods are for painting only, they should NOT do I/O.
 
Jos van Tuil
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
but is it a nice way to do it ?


So much for listening to the suggestion.

The suggestion was to use Key Bindings.

Also, painting methods are for painting only, they should NOT do I/O.


Was searching for tutorials on google and found one for this sollution using the term keybinding. Guess this is not it. back to google ^^
 
Rob Camick
Ranch Hand
Posts: 2788
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why did you search? I gave you a link with working examples.

Was there something about the link I gave you didn't understand? If so then you should ask a follow up question. Why would you ignore the link given?
 
Jos van Tuil
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Why did you search? I gave you a link with working examples.

Was there something about the link I gave you didn't understand? If so then you should ask a follow up question. Why would you ignore the link given?


I was getting confused by witch method was in witch class so I googled for java keybindings and i think i found a youtube video for it.
 
Jos van Tuil
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't seem to figure it out, i dont want it to only work, i want to get it. I dont know what the link means with delta and the move methods, are those both of the class that is supose to move ?

I also get errors on this piece of code, NetBeans doesnt know getInputMap and getActionMap

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!