Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

applet to change an image when keypressed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
damnit maybe not... ;-\ ill keep trying thanks though!
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i corrected the typos there...
 
Anthony Pena
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok but where should I put this?
 
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic