/
Card.java
160 lines (147 loc) · 4.16 KB
/
Card.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Represents a card, keeps Icons for front and back, handles mouse clicks
*
* Assignment: MP2
* Class: CS 340, Fall 2005
* TA: Nitin Jindal
* System: jEdit, jdk-1.5.0.4, Windows XP
* @author Michael Leonhard (CS account mleonhar)
* @version 22 Sep 2005
*/
import java.awt.event.*;
import javax.swing.*;
public class Card extends JLabel implements MouseListener
{
// data fields
TurnedCardManager turnedManager;
Icon faceIcon;
Icon backIcon;
boolean faceUp = false; // card is initially face down
int num; // number corresponding to the face of the card
int iconWidthHalf, iconHeightHalf; // half the dimensions of the back face icon
boolean mousePressedOnMe = false;
/**
* Constructor
* @param turnedManager object that manages list of currently turned cards
* @param face the face of the card, only one other card has this face
* @param back the back of the card, same as all other cards
* @param num number associated with face icon, used to compare cards for equality
*/
public Card(TurnedCardManager turnedManager, Icon face, Icon back, int num)
{
// initially show face down
super(back);
// save parameters
this.turnedManager = turnedManager;
this.faceIcon = face;
this.backIcon = back;
this.num = num;
// catch mouse clicks and events
this.addMouseListener(this);
// store icon dimensions for mouse click testing
this.iconWidthHalf = back.getIconWidth() / 2;
this.iconHeightHalf = back.getIconHeight() / 2;
}
/**
* Try to turn face up
*/
public void turnUp()
{
MemoryGame.dprintln("Card["+num+"].turnUp()");
// the card is already face up
if(this.faceUp) return;
// ask manager to allow turn
this.faceUp = this.turnedManager.turnUp(this);
// allowed to turn, so change the picture
if(this.faceUp) this.setIcon(this.faceIcon);
}
/**
* Turn back over, face down
*
*/
public void turnDown()
{
MemoryGame.dprintln("Card["+num+"].turnDown()");
// the card is already face down
if(!this.faceUp) return;
// change the picture
this.setIcon(this.backIcon);
// the card is now down
this.faceUp = false;
}
/**
* return the number identifying the kind of card (the face)
*/
public int getNum() { return num; }
/**
* Check if the coordinates are over the icon
*
* @param x X coordinate
* @param y Y coordinate
* @return true if coordinates are over icon, otherwise false
*/
private boolean overIcon(int x, int y)
{
// calculate the distance from the center of the label
int distX = Math.abs(x - (this.getWidth() / 2));
int distY = Math.abs(y - (this.getHeight() / 2));
// outside icon region
if(distX > this.iconWidthHalf || distY > this.iconHeightHalf )
return false;
// inside icon region
return true;
}
/** Methods to implement MouseListener interface **********************/
/**
* Invoked when the mouse button has been clicked (pressed and released) on a component.
*
* @param e object holding information about the button click
*/
public void mouseClicked(MouseEvent e)
{
// over icon, so try to turn up the card
if(overIcon(e.getX(), e.getY())) this.turnUp();
}
/**
* Invoked when a mouse button has been pressed on a component.
*
* @param e object holding information about the button press
*/
public void mousePressed(MouseEvent e)
{
// over icon, so remember this is a mouse press
if(overIcon(e.getX(), e.getY())) this.mousePressedOnMe = true;
}
/**
* Invoked when a mouse button has been released on a component.
*
* @param e object holding information about the button release
*/
public void mouseReleased(MouseEvent e)
{
// previous press was over icon
if(this.mousePressedOnMe)
{
// mouse is no longer pressed
this.mousePressedOnMe = false;
// it was a click, so treat it as one
this.mouseClicked(e);
}
}
/**
* Invoked when the mouse enters a component.
*
* @param e object holding information about the mouse pointer
*/
public void mouseEntered(MouseEvent e) {}
/**
* Invoked when the mouse exits a component.
*
* @param e object holding information about the mouse pointer
*/
public void mouseExited(MouseEvent e)
{
// forget any previous mouse press
this.mousePressedOnMe = false;
}
}