aspose file tools*
The moose likes Applets and the fly likes applet to change an image when keypressed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "applet to change an image when keypressed" Watch "applet to change an image when keypressed" New topic
Author

applet to change an image when keypressed

Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
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
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

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

Joined: May 14, 2011
Posts: 28
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

Joined: May 14, 2011
Posts: 28
damnit maybe not... ;-\ ill keep trying thanks though!
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28



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

Joined: Jun 13, 2009
Posts: 2203
    
    7
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

Joined: May 14, 2011
Posts: 28
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

Joined: May 14, 2011
Posts: 28
i corrected the typos there...
Anthony Pena
Greenhorn

Joined: May 14, 2011
Posts: 28
ok so far i have this, im not sure how or where to put the code to change labels though :-\

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

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

Joined: May 14, 2011
Posts: 28
ok but where should I put this?
 
Consider Paul's rocket mass heater.
 
subject: applet to change an image when keypressed