• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

applet to change an image when keypressed

 
Anthony Pena
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!

 
Paul Clapham
Sheriff
Posts: 21000
31
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?
 
Rob Camick
Ranch Hand
Posts: 2566
9
  • 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
Sheriff
Posts: 21000
31
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic