-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.java
More file actions
105 lines (88 loc) · 2.8 KB
/
Card.java
File metadata and controls
105 lines (88 loc) · 2.8 KB
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
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.net.URL;
import java.io.IOException;
public class Card extends JPanel {
public final String suit;
public final int value;
private BufferedImage front;
private BufferedImage back;
private boolean faceDown;
private boolean isBlack;
// Constructor
public Card(String suit, int value) {
// Construct a card
this.value = value;
this.suit = suit;
// Start the card out facing up
this.faceDown = false;
try {
// Load the images for the front and back of the card
// Have to use getClass().getResource() because otherwise it won't work out of a
// .jar file.
URL path = getClass().getResource("./cards/" + this.toString() + ".png");
front = ImageIO.read(path);
URL backPath = getClass().getResource("./cards/back.png");
back = ImageIO.read(backPath);
// Sets the size of the card to the size of the .png
setBounds(0, 0, front.getWidth(), front.getHeight());
} catch (IOException e) {
// Do nothing
e.printStackTrace();
}
setSize(100, 145);
setPreferredSize(new Dimension(100, 145));
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
BufferedImage showCard = front;
if (faceDown) {
showCard = back;
}
g.drawImage(showCard, 0, 0, this.getWidth(), this.getHeight(), null);
}
public String getSuit() {
return this.suit;
}
public String getValAsString() {
// Turns the value of the card into a string
if (this.value == 14) {
return "Ace";
} else if (this.value == 13) {
return "King";
} else if (this.value == 12) {
return "Queen";
} else if (this.value == 11) {
return "Jack";
} else {
return Integer.toString(this.value);
}
}
public int getValAsInt() {
// Returns the integer value of the card
return this.value;
}
public void fliptoFront() {
this.faceDown = false;
}
public void flipToBack() {
this.faceDown = true;
}
public boolean getColor()
{
if(suit == "hearts" || suit == "diamonds")
return false;
else
return true;
}
public String toString() { //The first card that is added to the stack is blank rather than having a particular suit
if(this.suit == "blank")
return "blank";
else
return this.getValAsString() + " of " + this.suit;
}
}