• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76055
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know, but try going to the Java™ Tutorials and look up key bindings. That might help.
 
Rancher
Posts: 3270
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3270
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic