Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Why can't I get my cards to move?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Why can't I get my cards to move?

    I have this piece of code up and running but I can't seem to get my cards to move. Is there something I am doing wrong? I was able to get them to show up front and back, and flip when I click on them. Why can't I get them to move? I thought I had the position updating. Any ideas? Thank you.

    Driver.java
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package deckofcards;
     
    import javax.swing.*;
     
    public class Driver
    {
    	public static void main(String [] args)
    	{
    		//create a frame
    		JFrame frame = new JFrame("Let's play with cards");
     
    		//create a panel, put panel in frame
    		CardPanel panel = new CardPanel();
    		frame.getContentPane().add(panel);
     
    		//make frame visible, set frame size
    		frame.setVisible(true);
    		frame.setSize(400,500);
    	}
    }

    CardPanel.java
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package deckofcards;
     
    /**
     *
     * @author Marlin
     */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    //CardPanel inherits from JPanel and implements MouseListener and MouseMotionListener interfaces
    public class CardPanel extends JPanel implements MouseListener, MouseMotionListener {
     
        Card jack, queen, king;
        int mx, my;
     
        CardPanel() {
            //create 3 cards
            jack = new Card(11, "spade");
            queen = new Card(12, "spade");
            king = new Card(13, "spade");
     
            //set their locations (coordiante in the panel for display purposes)
            jack.setLocation(100, 100);
            queen.setLocation(200, 100);
            king.setLocation(300, 100);
     
            //set the cards front image
            jack.setImageIconFront(new ImageIcon("cards/js.gif"));
            queen.setImageIconFront(new ImageIcon("cards/qs.gif"));
            king.setImageIconFront(new ImageIcon("cards/ks.gif"));
     
            //set the cards back image
            jack.setImageIconBack(new ImageIcon("cards/b.gif"));
            queen.setImageIconBack(new ImageIcon("cards/b.gif"));
            king.setImageIconBack(new ImageIcon("cards/b.gif"));
     
     
            addMouseListener(this); //To detect "mouse clicked" events
            addMouseMotionListener(this); //to detect "mouse dragged" events
            setFocusable(true);
        }
     
        public void paintComponent(Graphics g) {
            //clear screen
            super.paintComponent(g);
     
            //draw the 3 cards
            jack.draw(g, this);
            queen.draw(g, this);
            king.draw(g, this);
        }
     
        /////////MouseListener methods///////////////////
        public void mouseClicked(MouseEvent me) {
            //check if click occurred on one of the cards, if so flip card
            if (jack.contains(me.getX(), me.getY())) {
                jack.flip();
            }
            if (queen.contains(me.getX(), me.getY())) {
                queen.flip();
            }
            if (king.contains(me.getX(), me.getY())) {
                king.flip();
            }
            repaint();
        }
     
        public void mouseEntered(MouseEvent me) {
        }
     
        public void mouseExited(MouseEvent me) {
        }
     
        @Override
        public void mousePressed(MouseEvent me) {
        }
     
        @Override
        public void mouseReleased(MouseEvent me) {
     
        }
        @Override
        public void mouseDragged(MouseEvent me) {
            mx = me.getX();
            my = me.getY();
    }
     
        @Override
        public void mouseMoved(MouseEvent me) {
            mx = me.getX();
            my = me.getY();
        }
    }

    Card.java
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package deckofcards;
     
    /**
     *
     * @author Marlin
     */
    import javax.swing.*;
    import java.awt.*;
     
    //The rectangle class gives the object x and y coordiante variables, width and height variables,
    //with getWidth(), getHeight() methods, etc.  Also, there is a very useful 'contains( ... )' method
    //to determine if a point lies within a given rectangle.
    public class Card extends Rectangle {
     
        //private attributes cannot be accessed outside of the class
        private String suit;
        private int number;
        //image for the front and back of the card
        private ImageIcon front;
        private ImageIcon back;
        //is the card face up or face down?
        private boolean faceup;
     
        //constructor
        Card() {
            suit = "joker";
            number = 0;
     
            faceup = false;
        }
     
        Card(int num, String s) {
            number = num;
            suit = s;
     
            faceup = false;
        }
     
        //flip card
        public void flip() {
            faceup = !faceup;
        }
     
        //assign a front image to card
        public void setImageIconFront(ImageIcon f) {
            front = f;
     
            width = front.getIconWidth();
            height = front.getIconHeight();
        }
     
        //assign a back image to card
        public void setImageIconBack(ImageIcon b) {
            back = b;
        }
     
    //    move card to specified location
        @Override
        public void setLocation(int newX, int newY) {
            x = newX;
            y = newY;
        }
     
        //public 'get' method to get a private attribute
        public String getSuit() {
            return suit;
        }
     
        //public 'get' method for number
        public int getNumber() {
            return number;
        }
     
        // 11, 12, 13, 1 should say jack, queen, king, ace respectively
        public String toString() {
            String output = "";
     
            if (number == 1) {
                output += "ace";
            } else if (number == 11) {
                output += "jack";
            } else if (number == 12) {
                output += "queen";
            } else if (number == 13) {
                output += "king";
            } else {
                output += number;
            }
     
            output += " of " + suit + "s";
     
            return output;
        }
     
        //method that tells card how to draw itself
        public void draw(Graphics g, Component c) {
            if (faceup) {
                g.drawImage(front.getImage(), x, y, c);
            } else {
                g.drawImage(back.getImage(), x, y, c);
            }
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why can't I get my cards to move?

    What variables control the moving and location of the cards? Are the values of those variables changed by the code?
    Is repaint() called after the changes are made?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Please help with unwrap, deck, shuffle cards
    By pots in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2013, 12:46 PM
  2. Some help with an assignment based on crc cards
    By hiepa in forum Object Oriented Programming
    Replies: 20
    Last Post: October 7th, 2012, 12:17 AM
  3. Need help with Deck of Cards
    By yanksin1st in forum Java Theory & Questions
    Replies: 1
    Last Post: March 1st, 2012, 01:28 PM
  4. A deck of cards
    By glebovg in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 1st, 2012, 09:46 AM
  5. Adding cards back into deck
    By sp4ce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2011, 01:21 AM