Results 1 to 4 of 4
  1. #1
    luckylandie is offline Member
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Default repeated enabling disabling icon (card flipping)

    I'm using a gif icon on a label to display a deck of cards. I have the app to where I can have the shuffled deck either face up or face down and then be
    able to drag 5 cards from the top of the deck. If the 5 dragged cards are face
    down I can double click on them and they flip face up and if they are initially
    face up double clicking them they will flip face down. This is my first swing project so I am at a lost of why they only respond one time. What I want to be able to do is after the first flipping be able to double click and have them flip back.

    Java Code:
    public class IconCardLabelMouseAdapter extends MouseInputAdapter {
        ....    
        public void mouseClicked(MouseEvent e) {
            Icon disabledIcon = new ImageIcon("C:/Programming2009/MyNetBeansProjects/Poker/FiveCardStudB4/cards/b.gif");
            JLabel lbl = (JLabel)e.getSource();
            
            if (e.getClickCount() == 2) {
               if(Deck.isUp(lbl.getName())) {
    
                  lbl.setDisabledIcon(null);//turn card up
                  lbl.setEnabled(true);
                  
               }
               else {
                   lbl.setEnabled(false); //turn card down
                   lbl.setDisabledIcon(disabledIcon);
               }
            }
            
        }
        
        public class Deck {
            private static JLabel[] iconCardLabels;
            .....
            
            public void addDragability() {
                for(int i = 0; i < iconCardLabels.length; i++) {
                IconCardLabelMouseAdapter iclma = new CardLabelMouseAdapter    
                                                                (iconCardLabels[i]);
                iconCardLabels[i].addMouseListener(iclma);
                iconCardLabels[i].addMouseMotionListener(iclma);
            }
    
            public void faceDownDeck() {
                Icon disabledIcon = new ImageIcon("C:/Programming2009/MyNetBeansProjects/Poker/FiveCardStudB4/cards/b.gif");
                for(int i = 0; i < iconCardLabels.length; i++) {
                    iconCardLabels[i].setEnabled(false);
                    iconCardLabels[i].setDisabledIcon(disabledIcon);
                    iconCardLabels[i].setName(iconCardLabels[i].getName()   
                            + "," + "up");            
                }
            }
    
            public void faceUpDeck() {
                for(int i = 0; i < iconCardLabels.length; i++) {
                     iconCardLabels[i].setDisabledIcon(null);
                     iconCardLabels[i].setEnabled(true);
                     iconCardLabels[i].setName(iconCardLabels[i].getName() + ","  
                          + "down");
                }
            }
    
            public static boolean isUp(String name) {
                boolean up = false;
                String results[] = name.split(",");
    
               if(results[results.length-1].equals("up")) {
                   up = true;
               }
               return up;
            }
            .....
        }
    Last edited by Fubarable; 07-22-2010 at 10:47 PM. Reason: code tags added

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

    Default

    It's hard to know what's wrong with code that we can't compile, but I notice that when you flip the card display, I don't see that you change the isUp status of the card in the Deck (or do you, and I'm just not seeing it?):

    This is your current code:
    Java Code:
            if (e.getClickCount() == 2) {
               if(Deck.isUp(lbl.getName())) {
                  lbl.setDisabledIcon(null);//turn card up
                  lbl.setEnabled(true);
               }
               else {
                   lbl.setEnabled(false); //turn card down
                   lbl.setDisabledIcon(disabledIcon);
               }


    and what I'm looking for is something like this:
    Java Code:
            if (e.getClickCount() == 2) {
               if(Deck.isUp(lbl.getName())) {
    
                  // does Deck have a setter method to match the "getter" method isUp?
                  Deck.setUp(lbl.getName(), true);
    
                  lbl.setDisabledIcon(null);//turn card up
                  lbl.setEnabled(true);
               }
               else {
    
                  Deck.setUp(lbl.getName(), false);
    
                  lbl.setEnabled(false); //turn card down
                  lbl.setDisabledIcon(disabledIcon);
               }

    Also, you want to get rid of anything static in your code above as it doesn't belong, except for the main method of course.

    Also, I've added code tags to your post above to help make the code more easily readable. If you post more code in the future, you may wish to have a look at the link in my signature that shows how to use these things.

    Best of luck and welcome to the forum!
    Last edited by Fubarable; 07-22-2010 at 10:54 PM.

  3. #3
    luckylandie is offline Member
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks that was so obvious once you pointed it out. I got the cards repeadly flipping. But I will have to work quite a while to get rid of the static. Thanks
    for clueing me in on that.

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

    Default

    Glad you've got the flipping working out. I'm sure that you'll be able to work out the static issues without too much trouble. Thanks for reporting back to us with an update. Please let us know how this project progresses.

Similar Threads

  1. Question Card Layout, Card Management
    By lrichil in forum AWT / Swing
    Replies: 1
    Last Post: 04-22-2010, 10:11 AM
  2. Replies: 1
    Last Post: 12-02-2009, 03:39 PM
  3. Finding the most repeated names in a list
    By jboy in forum New To Java
    Replies: 2
    Last Post: 09-17-2009, 03:08 PM
  4. Remove repeated code
    By FraggleBoDiddly in forum New To Java
    Replies: 6
    Last Post: 10-26-2008, 02:28 PM
  5. Replies: 0
    Last Post: 07-07-2008, 04:35 PM

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
  •