Results 1 to 4 of 4
- 07-22-2010, 10:32 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 2
- Rep Power
- 0
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
-
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.
- 07-23-2010, 09:23 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 2
- Rep Power
- 0
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.
-
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
-
Question Card Layout, Card Management
By lrichil in forum AWT / SwingReplies: 1Last Post: 04-22-2010, 10:11 AM -
Memory card game using JOption... Need help flipping cards...more info inside, thanks
By moe123 in forum New To JavaReplies: 1Last Post: 12-02-2009, 03:39 PM -
Finding the most repeated names in a list
By jboy in forum New To JavaReplies: 2Last Post: 09-17-2009, 03:08 PM -
Remove repeated code
By FraggleBoDiddly in forum New To JavaReplies: 6Last Post: 10-26-2008, 02:28 PM -
Enabling/disabling menu depending on clipboard content availability
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks