Results 1 to 6 of 6
  1. #1
    itsdavid is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Best way to assign an image file to a JLabel?

    Exctly what the title says. Right now I have a character who's icon alternates (to provide the illusion of running for example), what I have done is I have separate JLabels for each image, with their visibility set to false, and my character changes his using using
    player.setIcon(runL3.getIcon());

    Seems like there must be a more efficient way of doing this so maybe someone could help me out?

  2. #2
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: Best way to assign an image file to a JLabel?

    This is something I am just starting to work on myself (today in fact). What code do you have so far?

  3. #3
    itsdavid is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Re: Best way to assign an image file to a JLabel?

    Basically I have 8 images my character needs to cycle through. So what I have right now are 8 JLabels, each with their icon set to one of the images, but their visibility set to false.
    Then I have a counter which cycles through the image which my main character displays, he is the code for that:

    if(keys[37]){ //left arrow
    lcount++;
    if(lcount>=1&&lcount<=3){player.setIcon(runL1.getI con());}
    if(lcount>=4&&lcount<=6){player.setIcon(runL2.getI con());}
    if(lcount>=7&&lcount<=9){player.setIcon(runL1.getI con());}
    if(lcount>=10&&lcount<=12){player.setIcon(runL3.ge tIcon());}
    if(lcount==13){lcount = 0;}

    I have the same thing going on the right side with a variable name rcount.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Best way to assign an image file to a JLabel?

    What does your getIcon(...) method do? If it reads in an image from a file or resource, then this is inefficient, and you'll be better served reading in the image once and storing it in a variable or collection.

  5. #5
    itsdavid is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Re: Best way to assign an image file to a JLabel?

    It's not a method I created. It is part of the JLabel class, basically an easy way to give an object an image I guess. Can you elaborate on the "storing the image in a variable" part?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Best way to assign an image file to a JLabel?

    Fubarable wasn't questioning the setIcon( ... ) method but your getIcon( ... ) method is suspicious ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Image in jLabel
    By abul.cse in forum AWT / Swing
    Replies: 1
    Last Post: 11-23-2012, 05:09 PM
  2. Using Image in JLabel? Please Help! :-)
    By Alerhau in forum New To Java
    Replies: 9
    Last Post: 09-07-2011, 07:48 PM
  3. How to assign Single image to each value in combo box
    By sandeep43 in forum New To Java
    Replies: 2
    Last Post: 08-08-2011, 03:47 PM
  4. Replies: 0
    Last Post: 05-16-2011, 11:28 AM
  5. Image On a JLabel
    By corbokhan in forum New To Java
    Replies: 11
    Last Post: 11-06-2010, 01:18 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •