Win a copy of AWS Security this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

applet to change an image when keypressed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im new to all this and having a really hard time in this class. any help and explaination would be greatly appreciated,

what im trying to do is get the image to change depending on direction, i have the images ill send em if you need them, i know im doing something wrong i just cant seem to get what though. thanks in advance for any help!

 
Marshal
Posts: 25598
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the JLabel to change in some way then you have to call one of its methods. Assigning a value to some other variable isn't going to change the JLabel.
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:If you want the JLabel to change in some way then you have to call one of its methods. Assigning a value to some other variable isn't going to change the JLabel.



ohhhhhhhhhhhhhhhhhhhhhhhhh i get it now!!! thanks so much
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
damnit maybe not... ;-\ ill keep trying thanks though!
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



tried this, and it doesnt initialize. What am i doing wrong?
imA = getImage(getCodeBase(),"aru.gif");
is there a way to change the text within the "" there?
 
Rancher
Posts: 3184
29
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You add an image to a JLabel by changing the Icon of the label. You create the label with a default Icon, but you never invoke label.setIcon(...) to change the Icon.

Also, you should NOT be reading the image every time you press a key. You should read all 4 images at the start of your program so the images are in memory.
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:You add an image to a JLabel by changing the Icon of the label. You create the label with a default Icon, but you never invoke label.setIcon(...) to change the Icon.

Also, you should NOT be reading the image every time you press a key. You should read all 4 images at the start of your program so the images are in memory.


ok so im reading the images at the top like this


now where would i put the label.setIcon? under every buttonpush?
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i corrected the typos there...
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so far i have this, im not sure how or where to put the code to change labels though :-\

 
Paul Clapham
Marshal
Posts: 25598
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Rob Camick said a while back, if you want the JLabel's icon to change then you have to call the JLabel's setIcon() method with the image you want it to change to.
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok but where should I put this?
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic