Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I assign a different image to each keypress event in my swing sprite?  RSS feed

 
Judas Styng
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want up arrow key to change to an up facing sprite, down arrow key to change to a down facing sprite and left and right keys to do similar.  (I know how to make a sprite and import and image) I have the images for up, down, left, right, but I don't know how to assign it to each individual keypress. Also, this isn't my entire code, just my key pressed event code.
 
Campbell Ritchie
Sheriff
Posts: 54024
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know, but try going to the Java™ Tutorials and look up key bindings. That might help.
 
Rob Camick
Ranch Hand
Posts: 2762
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the question.

You have logic to change the x/y values when a specific key is pressed. Why can't you also change the image at that time?
 
Judas Styng
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:I don't understand the question.

You have logic to change the x/y values when a specific key is pressed. Why can't you also change the image at that time?

I can't change the image at the same time because I don't know how. This is my 4th week learning Java, so I am very new. Sorry, if this question seems vague or like I'm not trying.
 
Rob Camick
Ranch Hand
Posts: 2762
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are making this two difficult. Apparently you know how to you assign an image to the variable "image".

So, why can't you create 4 more variables "upImage, downImage, rightImage and leftImage" and assign an image to each of those variables.

Then in your key listener code you can add a statement like:


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!