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
Code :
/*
* 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
Code :
/*
* 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
Code :
/*
* 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);
}
}
}
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?