Results 1 to 4 of 4
  1. #1
    mikehaas763 is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Assigning values to a variable (simple question, I believe)

    Hi,

    I'm working on a school assignment. Which I was given class which extends JLabel.

    I created a method in this class:

    Java Code:
    	/*
    	 * Swaps the current card
    	 */
    	public void swapCards()
    	{
    		ImageIcon tempIcon = (ImageIcon) this.getIcon();
    		this.setIcon(alternateImage);
    		alternateImage = tempIcon;
    	}
    This works, it does exactly what I want it to do. However, I was wondering if there was an easier/shorter/cleaner way of doing this?

    I know the following code does not actually work the way it does in the previous code, but I was thinking something like this:
    Java Code:
    	/*
    	 * Swaps the current card
    	 */
    	public void swapCards()
    	{
    		this.setIcon(alternateImage = (ImageIcon) this.getIcon());
    	}
    This doesn't work because it assigns the returned value from this.getIcon() to alternateImage then passes that as a parameter to this.setIcon.

    So, is my first example already the best way to do this?

    P.S. let me know if my code is too out of context and I will explain what it is used for better.

    Thanks,
    Mike
    Last edited by mikehaas763; 05-04-2012 at 03:29 AM.

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

    Default Re: Assigning values to a variable (simple question, I believe)

    I'm not sure how you would define "best" in this circumstance. Your code works, and that may be all you need to do here.

    One possible variation is that you could use an array of icon and in int field to represent an index into this array that indicates which icon is currently being displayed. This works well if you have more than 2 icons to swap since you could increment the int, "mod" it by the array size to be sure that it doesn't get larger than the array, and then use it to get the next Icon in the array. But this doesn't work any better than your code if you're sure to only want to swap 2 icons.
    Last edited by Fubarable; 05-04-2012 at 04:00 AM.

  3. #3
    mikehaas763 is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Assigning values to a variable (simple question, I believe)

    Thanks for your reply. I actually like that idea and may implement it in future projects when I wish to use an array of a Vector or something similar. However, in this project it's always going to be one of two ImageIcons so an array would defeat the purpose of wanting to clean it up.

    Thank you!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Assigning values to a variable (simple question, I believe)

    Looks like another candidate for CardLayout to me.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 3
    Last Post: 11-23-2010, 03:19 PM
  2. Replies: 0
    Last Post: 11-23-2010, 01:32 PM
  3. Assigning values to an object
    By camper2 in forum New To Java
    Replies: 4
    Last Post: 04-05-2009, 04:13 AM
  4. declaring fields without assigning values to them
    By diggitydoggz in forum New To Java
    Replies: 12
    Last Post: 01-03-2009, 09:22 PM
  5. Replies: 1
    Last Post: 10-02-2008, 04:06 AM

Posting Permissions

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