Results 1 to 6 of 6
- 01-26-2013, 03:13 AM #1
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
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?
- 01-26-2013, 03:45 AM #2
Senior Member
- Join Date
- Sep 2012
- Posts
- 108
- Rep Power
- 0
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?
- 01-26-2013, 03:59 AM #3
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
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.
-
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.
- 01-26-2013, 05:11 AM #5
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
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?
- 01-26-2013, 01:53 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
Image in jLabel
By abul.cse in forum AWT / SwingReplies: 1Last Post: 11-23-2012, 05:09 PM -
Using Image in JLabel? Please Help! :-)
By Alerhau in forum New To JavaReplies: 9Last Post: 09-07-2011, 07:48 PM -
How to assign Single image to each value in combo box
By sandeep43 in forum New To JavaReplies: 2Last Post: 08-08-2011, 03:47 PM -
How to Assign a Access database file directly from my code ?
By johny2011 in forum JDBCReplies: 0Last Post: 05-16-2011, 11:28 AM -
Image On a JLabel
By corbokhan in forum New To JavaReplies: 11Last Post: 11-06-2010, 01:18 AM
Bookmarks